Skip to content

Commit 2f54e7a

Browse files
committed
Refactor PropertiesBuilder to handle property value to String conversion.
Specifically, handle Class type's by converting a Class object to the Class's name using Class.getName() rather than Class.toString(). Closes spring-projects#618.
1 parent 875bea8 commit 2f54e7a

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/util/PropertiesBuilder.java

+20-25
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
1817
package org.springframework.data.gemfire.util;
1918

2019
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.io.Reader;
2322
import java.util.Properties;
23+
import java.util.function.Function;
2424

2525
import org.springframework.beans.factory.FactoryBean;
26+
import org.springframework.lang.NonNull;
27+
import org.springframework.lang.Nullable;
2628
import org.springframework.util.Assert;
2729
import org.springframework.util.ObjectUtils;
2830
import org.springframework.util.StringUtils;
@@ -38,6 +40,11 @@
3840
@SuppressWarnings("unused")
3941
public class PropertiesBuilder implements FactoryBean<Properties> {
4042

43+
private static final Function<Object, String> TO_STRING_FUNCTION = target ->
44+
target instanceof Class ? ((Class<?>) target).getName()
45+
: target != null ? target.toString()
46+
: String.valueOf(target);
47+
4148
/**
4249
* Factory method to create a default {@link PropertiesBuilder} instance.
4350
*
@@ -160,28 +167,16 @@ public PropertiesBuilder(PropertiesBuilder builder) {
160167
this(builder.build());
161168
}
162169

163-
/*
164-
* (non-Javadoc)
165-
* @see org.springframework.beans.factory.FactoryBean#getObject()
166-
*/
167170
@Override
168-
public Properties getObject() throws Exception {
171+
public @NonNull Properties getObject() throws Exception {
169172
return build();
170173
}
171174

172-
/*
173-
* (non-Javadoc)
174-
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
175-
*/
176175
@Override
177-
public Class<?> getObjectType() {
176+
public @NonNull Class<?> getObjectType() {
178177
return this.properties != null ? this.properties.getClass() : Properties.class;
179178
}
180179

181-
/*
182-
* (non-Javadoc)
183-
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
184-
*/
185180
@Override
186181
public boolean isSingleton() {
187182
return true;
@@ -195,7 +190,7 @@ public boolean isSingleton() {
195190
* @return a reference to this {@link PropertiesBuilder}.
196191
* @see java.util.Properties
197192
*/
198-
public PropertiesBuilder add(Properties properties) {
193+
public @NonNull PropertiesBuilder add(@Nullable Properties properties) {
199194

200195
if (!CollectionUtils.isEmpty(properties)) {
201196
this.properties.putAll(properties);
@@ -212,7 +207,7 @@ public PropertiesBuilder add(Properties properties) {
212207
* @return a reference to this {@link PropertiesBuilder}.
213208
* @see org.springframework.data.gemfire.util.PropertiesBuilder
214209
*/
215-
public PropertiesBuilder add(PropertiesBuilder builder) {
210+
public @NonNull PropertiesBuilder add(@Nullable PropertiesBuilder builder) {
216211
return builder != null ? add(builder.build()) : this;
217212
}
218213

@@ -224,8 +219,8 @@ public PropertiesBuilder add(PropertiesBuilder builder) {
224219
* @return a reference to this {@link PropertiesBuilder}.
225220
* @see #setProperty(String, String)
226221
*/
227-
public PropertiesBuilder setProperty(String name, Object value) {
228-
return value != null ? setProperty(name, value.toString()) : this;
222+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @NonNull Object value) {
223+
return value != null ? setProperty(name, TO_STRING_FUNCTION.apply(value)) : this;
229224
}
230225

231226
/**
@@ -238,7 +233,7 @@ public PropertiesBuilder setProperty(String name, Object value) {
238233
* @see org.springframework.util.StringUtils#arrayToCommaDelimitedString(Object[])
239234
* @see #setProperty(String, String)
240235
*/
241-
public PropertiesBuilder setProperty(String name, Object[] values) {
236+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @Nullable Object[] values) {
242237
return !ObjectUtils.isEmpty(values) ? setProperty(name, StringUtils.arrayToCommaDelimitedString(values)) : this;
243238
}
244239

@@ -253,7 +248,7 @@ public PropertiesBuilder setProperty(String name, Object[] values) {
253248
* @throws IllegalArgumentException if the property name is not specified.
254249
* @see java.util.Properties#setProperty(String, String)
255250
*/
256-
public PropertiesBuilder setProperty(String name, String value) {
251+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @NonNull String value) {
257252

258253
Assert.hasText(name, String.format("Name [%s] must be specified", name));
259254

@@ -276,7 +271,7 @@ public PropertiesBuilder setProperty(String name, String value) {
276271
* @return a reference to this {@link PropertiesBuilder}.
277272
* @see #setProperty(String, Object)
278273
*/
279-
public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value, T defaultValue) {
274+
public @NonNull <T> PropertiesBuilder setPropertyIfNotDefault(@NonNull String name, Object value, T defaultValue) {
280275
return defaultValue == null || !defaultValue.equals(value) ? setProperty(name, value) : this;
281276
}
282277

@@ -287,7 +282,7 @@ public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value,
287282
* @return a reference to this {@link PropertiesBuilder}.
288283
* @throws IllegalArgumentException if the property name is not specified.
289284
*/
290-
public PropertiesBuilder unsetProperty(String name) {
285+
public @NonNull PropertiesBuilder unsetProperty(@NonNull String name) {
291286

292287
Assert.hasText(name, String.format("Name [%s] mut be specified", name));
293288

@@ -304,7 +299,7 @@ public PropertiesBuilder unsetProperty(String name) {
304299
* @param value {@link String} value for the property being set.
305300
* @return a boolean value indicating whether the given {@link String} value is a valid {@link Properties} value.
306301
*/
307-
protected boolean isValuable(String value) {
302+
protected boolean isValuable(@Nullable String value) {
308303
return StringUtils.hasText(value) && !"null".equalsIgnoreCase(value.trim());
309304
}
310305

@@ -314,7 +309,7 @@ protected boolean isValuable(String value) {
314309
* @return the {@link Properties} object built by this builder.
315310
* @see java.util.Properties
316311
*/
317-
public Properties build() {
312+
public @NonNull Properties build() {
318313
return this.properties;
319314
}
320315
}

spring-data-geode/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderTests.java renamed to spring-data-geode/src/test/java/org/springframework/data/gemfire/util/PropertiesBuilderUnitTests.java

+38-21
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
import java.io.IOException;
2424
import java.io.StringReader;
2525
import java.io.StringWriter;
26+
import java.math.BigDecimal;
2627
import java.util.Properties;
2728

2829
import org.junit.Test;
2930

3031
/**
31-
* Test suite of test cases testing the contract and functionality of the {@link PropertiesBuilder} class.
32+
* Unit Tests for {@link PropertiesBuilder}
3233
*
3334
* @author John Blum
3435
* @see java.util.Properties
36+
* @see org.junit.Test
3537
* @see org.springframework.data.gemfire.util.PropertiesBuilder
3638
* @since 1.9.0
3739
*/
38-
public class PropertiesBuilderTests {
40+
public class PropertiesBuilderUnitTests {
3941

4042
private Properties singletonProperties(String name, String value) {
4143
Properties properties = new Properties();
@@ -44,7 +46,7 @@ private Properties singletonProperties(String name, String value) {
4446
}
4547

4648
@Test
47-
public void constructDefaultPropertiesBuilder() throws Exception {
49+
public void constructPropertiesBuilder() throws Exception {
4850

4951
PropertiesBuilder builder = new PropertiesBuilder();
5052

@@ -208,7 +210,7 @@ public void setObjectPropertyValuesIsSuccessful() {
208210
.build();
209211

210212
assertThat(properties).isNotNull();
211-
assertThat(properties.size()).isEqualTo(5);
213+
assertThat(properties).hasSize(5);
212214
assertThat(properties.getProperty("boolean")).isEqualTo(Boolean.TRUE.toString());
213215
assertThat(properties.getProperty("character")).isEqualTo("A");
214216
assertThat(properties.getProperty("integer")).isEqualTo("1");
@@ -224,11 +226,40 @@ public void setObjectArrayPropertyValueIsSuccessful() {
224226
.build();
225227

226228
assertThat(properties).isNotNull();
227-
assertThat(properties.size()).isEqualTo(1);
229+
assertThat(properties).hasSize(1);
228230
assertThat(properties.containsKey("numbers")).isTrue();
229231
assertThat(properties.getProperty("numbers")).isEqualTo("one,two,three");
230232
}
231233

234+
@Test
235+
public void setClassPropertyValueIsClassName() {
236+
237+
Properties properties = PropertiesBuilder.create()
238+
.setProperty("type", BigDecimal.class)
239+
.build();
240+
241+
assertThat(properties).isNotNull();
242+
assertThat(properties).hasSize(1);
243+
assertThat(properties).containsKey("type");
244+
assertThat(properties.get("type")).isEqualTo(BigDecimal.class.getName());
245+
}
246+
247+
@Test
248+
public void setStringPropertyValuesIsSuccessful() {
249+
250+
Properties properties = PropertiesBuilder.create()
251+
.setProperty("one", "1")
252+
.setProperty("two", "2")
253+
.build();
254+
255+
assertThat(properties).isNotNull();
256+
assertThat(properties).hasSize(2);
257+
assertThat(properties.containsKey("one")).isTrue();
258+
assertThat(properties.containsKey("two")).isTrue();
259+
assertThat(properties.getProperty("one")).isEqualTo("1");
260+
assertThat(properties.getProperty("two")).isEqualTo("2");
261+
}
262+
232263
@Test
233264
public void setPropertyIgnoresNullObjectValue() {
234265

@@ -270,22 +301,6 @@ public void setPropertyIgnoresNullObjectArray() {
270301
assertThat(properties.isEmpty()).isTrue();
271302
}
272303

273-
@Test
274-
public void setStringPropertyValuesIsSuccessful() {
275-
276-
Properties properties = PropertiesBuilder.create()
277-
.setProperty("one", "1")
278-
.setProperty("two", "2")
279-
.build();
280-
281-
assertThat(properties).isNotNull();
282-
assertThat(properties.size()).isEqualTo(2);
283-
assertThat(properties.containsKey("one")).isTrue();
284-
assertThat(properties.containsKey("two")).isTrue();
285-
assertThat(properties.getProperty("one")).isEqualTo("1");
286-
assertThat(properties.getProperty("two")).isEqualTo("2");
287-
}
288-
289304
@Test
290305
public void unsetPropertyIsSuccessful() {
291306

@@ -299,7 +314,9 @@ public void unsetPropertyIsSuccessful() {
299314

300315
@Test
301316
public void stringLiteralIsValuable() {
317+
302318
assertThat(PropertiesBuilder.create().isValuable("test")).isTrue();
319+
assertThat(PropertiesBuilder.create().isValuable("mock")).isTrue();
303320
}
304321

305322
@Test

0 commit comments

Comments
 (0)