Skip to content

Commit 9689827

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 #618.
1 parent 460d102 commit 9689827

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

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

+19-21
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import java.io.InputStream;
2121
import java.io.Reader;
2222
import java.util.Properties;
23+
import java.util.function.Function;
2324

2425
import org.springframework.beans.factory.FactoryBean;
2526
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 used to create a default {@link PropertiesBuilder} instance.
4350
*
@@ -162,25 +169,16 @@ public PropertiesBuilder(@NonNull PropertiesBuilder builder) {
162169
this(builder.build());
163170
}
164171

165-
/**
166-
* @inheritDoc
167-
*/
168172
@Override
169-
public Properties getObject() throws Exception {
173+
public @NonNull Properties getObject() throws Exception {
170174
return build();
171175
}
172176

173-
/**
174-
* @inheritDoc
175-
*/
176177
@Override
177-
public Class<?> getObjectType() {
178+
public @NonNull Class<?> getObjectType() {
178179
return this.properties != null ? this.properties.getClass() : Properties.class;
179180
}
180181

181-
/**
182-
* @inheritDoc
183-
*/
184182
@Override
185183
public boolean isSingleton() {
186184
return true;
@@ -194,7 +192,7 @@ public boolean isSingleton() {
194192
* @return a reference to this {@link PropertiesBuilder}.
195193
* @see java.util.Properties
196194
*/
197-
public PropertiesBuilder add(Properties properties) {
195+
public @NonNull PropertiesBuilder add(@Nullable Properties properties) {
198196

199197
if (!CollectionUtils.isEmpty(properties)) {
200198
this.properties.putAll(properties);
@@ -211,7 +209,7 @@ public PropertiesBuilder add(Properties properties) {
211209
* @return a reference to this {@link PropertiesBuilder}.
212210
* @see org.springframework.data.gemfire.util.PropertiesBuilder
213211
*/
214-
public PropertiesBuilder add(PropertiesBuilder builder) {
212+
public @NonNull PropertiesBuilder add(@Nullable PropertiesBuilder builder) {
215213
return builder != null ? add(builder.build()) : this;
216214
}
217215

@@ -223,8 +221,8 @@ public PropertiesBuilder add(PropertiesBuilder builder) {
223221
* @return a reference to this {@link PropertiesBuilder}.
224222
* @see #setProperty(String, String)
225223
*/
226-
public PropertiesBuilder setProperty(String name, Object value) {
227-
return value != null ? setProperty(name, value.toString()) : this;
224+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @NonNull Object value) {
225+
return value != null ? setProperty(name, TO_STRING_FUNCTION.apply(value)) : this;
228226
}
229227

230228
/**
@@ -237,7 +235,7 @@ public PropertiesBuilder setProperty(String name, Object value) {
237235
* @see org.springframework.util.StringUtils#arrayToCommaDelimitedString(Object[])
238236
* @see #setProperty(String, String)
239237
*/
240-
public PropertiesBuilder setProperty(String name, Object[] values) {
238+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @Nullable Object[] values) {
241239
return !ObjectUtils.isEmpty(values) ? setProperty(name, StringUtils.arrayToCommaDelimitedString(values)) : this;
242240
}
243241

@@ -252,7 +250,7 @@ public PropertiesBuilder setProperty(String name, Object[] values) {
252250
* @throws IllegalArgumentException if the property name is not specified.
253251
* @see java.util.Properties#setProperty(String, String)
254252
*/
255-
public PropertiesBuilder setProperty(String name, String value) {
253+
public @NonNull PropertiesBuilder setProperty(@NonNull String name, @NonNull String value) {
256254

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

@@ -275,7 +273,7 @@ public PropertiesBuilder setProperty(String name, String value) {
275273
* @return a reference to this {@link PropertiesBuilder}.
276274
* @see #setProperty(String, Object)
277275
*/
278-
public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value, T defaultValue) {
276+
public @NonNull <T> PropertiesBuilder setPropertyIfNotDefault(@NonNull String name, Object value, T defaultValue) {
279277
return defaultValue == null || !defaultValue.equals(value) ? setProperty(name, value) : this;
280278
}
281279

@@ -286,7 +284,7 @@ public <T> PropertiesBuilder setPropertyIfNotDefault(String name, Object value,
286284
* @return a reference to this {@link PropertiesBuilder}.
287285
* @throws IllegalArgumentException if the property name is not specified.
288286
*/
289-
public PropertiesBuilder unsetProperty(String name) {
287+
public @NonNull PropertiesBuilder unsetProperty(@NonNull String name) {
290288

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

@@ -303,7 +301,7 @@ public PropertiesBuilder unsetProperty(String name) {
303301
* @param value {@link String} value for the property being set.
304302
* @return a boolean value indicating whether the given {@link String} value is a valid {@link Properties} value.
305303
*/
306-
protected boolean isValuable(String value) {
304+
protected boolean isValuable(@Nullable String value) {
307305
return StringUtils.hasText(value) && !"null".equalsIgnoreCase(value.trim());
308306
}
309307

@@ -313,7 +311,7 @@ protected boolean isValuable(String value) {
313311
* @return the {@link Properties} object built by this builder.
314312
* @see java.util.Properties
315313
*/
316-
public Properties build() {
314+
public @NonNull Properties build() {
317315
return this.properties;
318316
}
319317
}

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)