Add default value for arrays
Previously, a property holding an array did not have a proper default value in the meta-data even though the related field was initialized properly. An explicit support for arrays has been added. The "defaultValue" now holds the default value for singular properties or an array of values for array-based properties. If the value is initalized with an empty array, the default value is an empty array as well. Closes gh-1996
This commit is contained in:
@@ -43,6 +43,7 @@ import org.springframework.boot.configurationsample.specific.InnerClassPropertie
|
||||
import org.springframework.boot.configurationsample.specific.InnerClassRootConfig;
|
||||
import org.springframework.boot.configurationsample.specific.SimplePojo;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@@ -77,7 +78,7 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
containsProperty("simple.the-name", String.class)
|
||||
.fromSource(SimpleProperties.class)
|
||||
.withDescription("The name of this simple properties.")
|
||||
.withDefaultValue("boot").withDeprecated());
|
||||
.withDefaultValue(is("boot")).withDeprecated());
|
||||
assertThat(
|
||||
metadata,
|
||||
containsProperty("simple.flag", Boolean.class)
|
||||
|
||||
@@ -66,7 +66,7 @@ public class ConfigurationMetadataMatchers {
|
||||
|
||||
private final String description;
|
||||
|
||||
private final Object defaultValue;
|
||||
private final Matcher<?> defaultValue;
|
||||
|
||||
private final boolean deprecated;
|
||||
|
||||
@@ -75,7 +75,7 @@ public class ConfigurationMetadataMatchers {
|
||||
}
|
||||
|
||||
public ContainsItemMatcher(ItemType itemType, String name, String type,
|
||||
Class<?> sourceType, String description, Object defaultValue,
|
||||
Class<?> sourceType, String description, Matcher<?> defaultValue,
|
||||
boolean deprecated) {
|
||||
this.itemType = itemType;
|
||||
this.name = name;
|
||||
@@ -101,7 +101,7 @@ public class ConfigurationMetadataMatchers {
|
||||
return false;
|
||||
}
|
||||
if (this.defaultValue != null
|
||||
&& !this.defaultValue.equals(itemMetadata.getDefaultValue())) {
|
||||
&& !this.defaultValue.matches(itemMetadata.getDefaultValue())) {
|
||||
return false;
|
||||
}
|
||||
if (this.description != null
|
||||
@@ -169,7 +169,7 @@ public class ConfigurationMetadataMatchers {
|
||||
this.sourceType, description, this.defaultValue, this.deprecated);
|
||||
}
|
||||
|
||||
public ContainsItemMatcher withDefaultValue(Object defaultValue) {
|
||||
public ContainsItemMatcher withDefaultValue(Matcher<?> defaultValue) {
|
||||
return new ContainsItemMatcher(this.itemType, this.name, this.type,
|
||||
this.sourceType, this.description, defaultValue, this.deprecated);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ import static org.junit.Assert.assertThat;
|
||||
* Abstract base class for {@link FieldValuesParser} tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public abstract class AbstractFieldValuesProcessorTests {
|
||||
|
||||
@@ -77,6 +78,12 @@ public abstract class AbstractFieldValuesProcessorTests {
|
||||
assertThat(values.get("objectNone"), nullValue());
|
||||
assertThat(values.get("objectConst"), equalToObject("c"));
|
||||
assertThat(values.get("objectInstance"), nullValue());
|
||||
assertThat(values.get("stringArray"), equalToObject(new Object[] {"FOO", "BAR"}));
|
||||
assertThat(values.get("stringArrayNone"), nullValue());
|
||||
assertThat(values.get("stringEmptyArray"), equalToObject(new Object[0]));
|
||||
assertThat(values.get("stringArrayConst"), equalToObject(new Object[]{"OK", "KO"}));
|
||||
assertThat(values.get("stringArrayConstElements"), equalToObject(new Object[] {"c"}));
|
||||
assertThat(values.get("integerArray"), equalToObject(new Object[] {42, 24}));
|
||||
}
|
||||
|
||||
private Matcher<Object> equalToObject(Object object) {
|
||||
|
||||
@@ -23,6 +23,9 @@ import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.array;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup;
|
||||
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty;
|
||||
@@ -31,6 +34,7 @@ import static org.springframework.boot.configurationprocessor.ConfigurationMetad
|
||||
* Tests for {@link JsonMarshaller}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JsonMarshallerTests {
|
||||
|
||||
@@ -45,6 +49,10 @@ public class JsonMarshallerTests {
|
||||
false));
|
||||
metadata.add(ItemMetadata.newProperty("d", null, null, null, null, null, true,
|
||||
false));
|
||||
metadata.add(ItemMetadata.newProperty("e", null, null, null, null, null, new String[]{"y", "n"},
|
||||
false));
|
||||
metadata.add(ItemMetadata.newProperty("f", null, null, null, null, null, new Boolean[]{true, false},
|
||||
false));
|
||||
metadata.add(ItemMetadata.newGroup("d", null, null, null));
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
JsonMarshaller marshaller = new JsonMarshaller();
|
||||
@@ -53,10 +61,12 @@ public class JsonMarshallerTests {
|
||||
outputStream.toByteArray()));
|
||||
assertThat(read,
|
||||
containsProperty("a.b", StringBuffer.class).fromSource(InputStream.class)
|
||||
.withDescription("desc").withDefaultValue("x").withDeprecated());
|
||||
.withDescription("desc").withDefaultValue(is("x")).withDeprecated());
|
||||
assertThat(read, containsProperty("b.c.d"));
|
||||
assertThat(read, containsProperty("c").withDefaultValue(123));
|
||||
assertThat(read, containsProperty("d").withDefaultValue(true));
|
||||
assertThat(read, containsProperty("c").withDefaultValue(is(123)));
|
||||
assertThat(read, containsProperty("d").withDefaultValue(is(true)));
|
||||
assertThat(read, containsProperty("e").withDefaultValue(is(array(equalTo("y"), equalTo("n")))));
|
||||
assertThat(read, containsProperty("f").withDefaultValue(is(array(equalTo(true), equalTo(false)))));
|
||||
assertThat(read, containsGroup("d"));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.boot.configurationsample.ConfigurationProperties;
|
||||
* Sample object containing fields with initial values.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@ConfigurationProperties
|
||||
@@ -37,6 +38,8 @@ public class FieldValues {
|
||||
|
||||
private static final Integer INTEGER_OBJ_CONST = 4;
|
||||
|
||||
private static final String[] STRING_ARRAY_CONST = new String[] {"OK", "KO"};
|
||||
|
||||
private String string = "1";
|
||||
|
||||
private String stringNone;
|
||||
@@ -75,4 +78,16 @@ public class FieldValues {
|
||||
|
||||
private Object objectInstance = new StringBuffer();
|
||||
|
||||
private String[] stringArray = new String[] {"FOO", "BAR"};
|
||||
|
||||
private String[] stringArrayNone;
|
||||
|
||||
private String[] stringEmptyArray = new String[0];
|
||||
|
||||
private String[] stringArrayConst = STRING_ARRAY_CONST;
|
||||
|
||||
private String[] stringArrayConstElements = new String[] { STRING_CONST };
|
||||
|
||||
private Integer[] integerArray = new Integer[] {42, 24};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user