Use AssertJ in spring-boot-tools

See gh-5083
This commit is contained in:
Phillip Webb
2016-02-06 14:53:00 -08:00
parent 7f9358f4d8
commit 00cfe1d054
37 changed files with 1326 additions and 1402 deletions

View File

@@ -63,16 +63,7 @@ import org.springframework.boot.configurationsample.specific.InnerClassRootConfi
import org.springframework.boot.configurationsample.specific.SimplePojo;
import org.springframework.util.FileCopyUtils;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsHint;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigurationMetadataAnnotationProcessor}.
@@ -100,80 +91,88 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Test
public void notAnnotated() throws Exception {
ConfigurationMetadata metadata = compile(NotAnnotated.class);
assertThat("No config metadata file should have been generated when "
+ "no metadata is discovered", metadata.getItems(), empty());
assertThat(metadata.getItems()).isEmpty();
}
@Test
public void simpleProperties() throws Exception {
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata, containsGroup("simple").fromSource(SimpleProperties.class));
assertThat(metadata,
containsProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue(is("boot")).withDeprecation(null, null));
assertThat(metadata,
containsProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class)
.withDescription("A simple flag.").withDeprecation(null, null));
assertThat(metadata, containsProperty("simple.comparator"));
assertThat(metadata, not(containsProperty("simple.counter")));
assertThat(metadata, not(containsProperty("simple.size")));
assertThat(metadata)
.has(Metadata.withGroup("simple").fromSource(SimpleProperties.class));
assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot").withDeprecation(null, null));
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class).withDescription("A simple flag.")
.withDeprecation(null, null));
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).doesNotHave(Metadata.withProperty("simple.counter"));
assertThat(metadata).doesNotHave(Metadata.withProperty("simple.size"));
}
@Test
public void simplePrefixValueProperties() throws Exception {
ConfigurationMetadata metadata = compile(SimplePrefixValueProperties.class);
assertThat(metadata,
containsGroup("simple").fromSource(SimplePrefixValueProperties.class));
assertThat(metadata, containsProperty("simple.name", String.class)
assertThat(metadata).has(Metadata.withGroup("simple")
.fromSource(SimplePrefixValueProperties.class));
assertThat(metadata).has(Metadata.withProperty("simple.name", String.class)
.fromSource(SimplePrefixValueProperties.class));
}
@Test
public void simpleTypeProperties() throws Exception {
ConfigurationMetadata metadata = compile(SimpleTypeProperties.class);
assertThat(metadata,
containsGroup("simple.type").fromSource(SimpleTypeProperties.class));
assertThat(metadata, containsProperty("simple.type.my-string", String.class));
assertThat(metadata, containsProperty("simple.type.my-byte", Byte.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-byte", Byte.class));
assertThat(metadata, containsProperty("simple.type.my-char", Character.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-char", Character.class));
assertThat(metadata, containsProperty("simple.type.my-boolean", Boolean.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-boolean", Boolean.class));
assertThat(metadata, containsProperty("simple.type.my-short", Short.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-short", Short.class));
assertThat(metadata, containsProperty("simple.type.my-integer", Integer.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-integer", Integer.class));
assertThat(metadata, containsProperty("simple.type.my-long", Long.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-long", Long.class));
assertThat(metadata, containsProperty("simple.type.my-double", Double.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-double", Double.class));
assertThat(metadata, containsProperty("simple.type.my-float", Float.class));
assertThat(metadata,
containsProperty("simple.type.my-primitive-float", Float.class));
assertThat(metadata.getItems().size(), equalTo(18));
assertThat(metadata).has(
Metadata.withGroup("simple.type").fromSource(SimpleTypeProperties.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-string", String.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-byte", Byte.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-primitive-byte", Byte.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-char", Character.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-char", Character.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-boolean", Boolean.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-boolean", Boolean.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-short", Short.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-short", Short.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-integer", Integer.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-integer", Integer.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-long", Long.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-primitive-long", Long.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-double", Double.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-double", Double.class));
assertThat(metadata)
.has(Metadata.withProperty("simple.type.my-float", Float.class));
assertThat(metadata).has(
Metadata.withProperty("simple.type.my-primitive-float", Float.class));
assertThat(metadata.getItems().size()).isEqualTo(18);
}
@Test
public void hierarchicalProperties() throws Exception {
ConfigurationMetadata metadata = compile(HierarchicalProperties.class);
assertThat(metadata,
containsGroup("hierarchical").fromSource(HierarchicalProperties.class));
assertThat(metadata, containsProperty("hierarchical.first", String.class)
assertThat(metadata).has(Metadata.withGroup("hierarchical")
.fromSource(HierarchicalProperties.class));
assertThat(metadata, containsProperty("hierarchical.second", String.class)
assertThat(metadata).has(Metadata.withProperty("hierarchical.first", String.class)
.fromSource(HierarchicalProperties.class));
assertThat(metadata, containsProperty("hierarchical.third", String.class)
assertThat(metadata)
.has(Metadata.withProperty("hierarchical.second", String.class)
.fromSource(HierarchicalProperties.class));
assertThat(metadata).has(Metadata.withProperty("hierarchical.third", String.class)
.fromSource(HierarchicalProperties.class));
}
@@ -182,152 +181,157 @@ public class ConfigurationMetadataAnnotationProcessorTests {
public void deprecatedProperties() throws Exception {
Class<?> type = org.springframework.boot.configurationsample.simple.DeprecatedProperties.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata, containsGroup("deprecated").fromSource(type));
assertThat(metadata, containsProperty("deprecated.name", String.class)
.fromSource(type).withDeprecation(null, null));
assertThat(metadata, containsProperty("deprecated.description", String.class)
assertThat(metadata).has(Metadata.withGroup("deprecated").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("deprecated.name", String.class)
.fromSource(type).withDeprecation(null, null));
assertThat(metadata)
.has(Metadata.withProperty("deprecated.description", String.class)
.fromSource(type).withDeprecation(null, null));
}
@Test
public void singleDeprecatedProperty() throws Exception {
Class<?> type = DeprecatedSingleProperty.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata, containsGroup("singledeprecated").fromSource(type));
assertThat(metadata, containsProperty("singledeprecated.new-name", String.class)
.fromSource(type));
assertThat(metadata,
containsProperty("singledeprecated.name", String.class).fromSource(type)
.withDeprecation("renamed", "singledeprecated.new-name"));
assertThat(metadata).has(Metadata.withGroup("singledeprecated").fromSource(type));
assertThat(metadata)
.has(Metadata.withProperty("singledeprecated.new-name", String.class)
.fromSource(type));
assertThat(metadata).has(Metadata
.withProperty("singledeprecated.name", String.class).fromSource(type)
.withDeprecation("renamed", "singledeprecated.new-name"));
}
@Test
public void deprecatedOnUnrelatedSetter() throws Exception {
Class<?> type = DeprecatedUnrelatedMethodPojo.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata, containsGroup("not.deprecated").fromSource(type));
assertThat(metadata, containsProperty("not.deprecated.counter", Integer.class)
.withNoDeprecation().fromSource(type));
assertThat(metadata, containsProperty("not.deprecated.flag", Boolean.class)
.withNoDeprecation().fromSource(type));
assertThat(metadata).has(Metadata.withGroup("not.deprecated").fromSource(type));
assertThat(metadata)
.has(Metadata.withProperty("not.deprecated.counter", Integer.class)
.withNoDeprecation().fromSource(type));
assertThat(metadata)
.has(Metadata.withProperty("not.deprecated.flag", Boolean.class)
.withNoDeprecation().fromSource(type));
}
@Test
public void boxingOnSetter() throws IOException {
Class<?> type = BoxingPojo.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata, containsGroup("boxing").fromSource(type));
assertThat(metadata,
containsProperty("boxing.flag", Boolean.class).fromSource(type));
assertThat(metadata,
containsProperty("boxing.counter", Integer.class).fromSource(type));
assertThat(metadata).has(Metadata.withGroup("boxing").fromSource(type));
assertThat(metadata).has(
Metadata.withProperty("boxing.flag", Boolean.class).fromSource(type));
assertThat(metadata).has(
Metadata.withProperty("boxing.counter", Integer.class).fromSource(type));
}
@Test
public void parseCollectionConfig() throws Exception {
ConfigurationMetadata metadata = compile(SimpleCollectionProperties.class);
// getter and setter
assertThat(metadata, containsProperty("collection.integers-to-names",
assertThat(metadata).has(Metadata.withProperty("collection.integers-to-names",
"java.util.Map<java.lang.Integer,java.lang.String>"));
assertThat(metadata, containsProperty("collection.longs",
assertThat(metadata).has(Metadata.withProperty("collection.longs",
"java.util.Collection<java.lang.Long>"));
assertThat(metadata,
containsProperty("collection.floats", "java.util.List<java.lang.Float>"));
assertThat(metadata).has(Metadata.withProperty("collection.floats",
"java.util.List<java.lang.Float>"));
// getter only
assertThat(metadata, containsProperty("collection.names-to-integers",
assertThat(metadata).has(Metadata.withProperty("collection.names-to-integers",
"java.util.Map<java.lang.String,java.lang.Integer>"));
assertThat(metadata, containsProperty("collection.bytes",
assertThat(metadata).has(Metadata.withProperty("collection.bytes",
"java.util.Collection<java.lang.Byte>"));
assertThat(metadata, containsProperty("collection.doubles",
assertThat(metadata).has(Metadata.withProperty("collection.doubles",
"java.util.List<java.lang.Double>"));
}
@Test
public void simpleMethodConfig() throws Exception {
ConfigurationMetadata metadata = compile(SimpleMethodConfig.class);
assertThat(metadata, containsGroup("foo").fromSource(SimpleMethodConfig.class));
assertThat(metadata, containsProperty("foo.name", String.class)
assertThat(metadata)
.has(Metadata.withGroup("foo").fromSource(SimpleMethodConfig.class));
assertThat(metadata).has(Metadata.withProperty("foo.name", String.class)
.fromSource(SimpleMethodConfig.Foo.class));
assertThat(metadata, containsProperty("foo.flag", Boolean.class)
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
.fromSource(SimpleMethodConfig.Foo.class));
}
@Test
public void invalidMethodConfig() throws Exception {
ConfigurationMetadata metadata = compile(InvalidMethodConfig.class);
assertThat(metadata, containsProperty("something.name", String.class)
assertThat(metadata).has(Metadata.withProperty("something.name", String.class)
.fromSource(InvalidMethodConfig.class));
assertThat(metadata, not(containsProperty("invalid.name")));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("invalid.name"));
}
@Test
public void methodAndClassConfig() throws Exception {
ConfigurationMetadata metadata = compile(MethodAndClassConfig.class);
assertThat(metadata, containsProperty("conflict.name", String.class)
assertThat(metadata).has(Metadata.withProperty("conflict.name", String.class)
.fromSource(MethodAndClassConfig.Foo.class));
assertThat(metadata, containsProperty("conflict.flag", Boolean.class)
assertThat(metadata).has(Metadata.withProperty("conflict.flag", Boolean.class)
.fromSource(MethodAndClassConfig.Foo.class));
assertThat(metadata, containsProperty("conflict.value", String.class)
assertThat(metadata).has(Metadata.withProperty("conflict.value", String.class)
.fromSource(MethodAndClassConfig.class));
}
@Test
public void emptyTypeMethodConfig() throws Exception {
ConfigurationMetadata metadata = compile(EmptyTypeMethodConfig.class);
assertThat(metadata, not(containsProperty("something.foo")));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("something.foo"));
}
@Test
public void innerClassRootConfig() throws Exception {
ConfigurationMetadata metadata = compile(InnerClassRootConfig.class);
assertThat(metadata, containsProperty("config.name"));
assertThat(metadata).has(Metadata.withProperty("config.name"));
}
@Test
public void innerClassProperties() throws Exception {
ConfigurationMetadata metadata = compile(InnerClassProperties.class);
assertThat(metadata,
containsGroup("config").fromSource(InnerClassProperties.class));
assertThat(metadata,
containsGroup("config.first").ofType(InnerClassProperties.Foo.class)
assertThat(metadata)
.has(Metadata.withGroup("config").fromSource(InnerClassProperties.class));
assertThat(metadata).has(
Metadata.withGroup("config.first").ofType(InnerClassProperties.Foo.class)
.fromSource(InnerClassProperties.class));
assertThat(metadata, containsProperty("config.first.name"));
assertThat(metadata, containsProperty("config.first.bar.name"));
assertThat(metadata,
containsGroup("config.the-second", InnerClassProperties.Foo.class)
assertThat(metadata).has(Metadata.withProperty("config.first.name"));
assertThat(metadata).has(Metadata.withProperty("config.first.bar.name"));
assertThat(metadata).has(
Metadata.withGroup("config.the-second", InnerClassProperties.Foo.class)
.fromSource(InnerClassProperties.class));
assertThat(metadata, containsProperty("config.the-second.name"));
assertThat(metadata, containsProperty("config.the-second.bar.name"));
assertThat(metadata, containsGroup("config.third").ofType(SimplePojo.class)
.fromSource(InnerClassProperties.class));
assertThat(metadata, containsProperty("config.third.value"));
assertThat(metadata, containsProperty("config.fourth"));
assertThat(metadata, not(containsGroup("config.fourth")));
assertThat(metadata).has(Metadata.withProperty("config.the-second.name"));
assertThat(metadata).has(Metadata.withProperty("config.the-second.bar.name"));
assertThat(metadata).has(Metadata.withGroup("config.third")
.ofType(SimplePojo.class).fromSource(InnerClassProperties.class));
assertThat(metadata).has(Metadata.withProperty("config.third.value"));
assertThat(metadata).has(Metadata.withProperty("config.fourth"));
assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth"));
}
@Test
public void innerClassAnnotatedGetterConfig() throws Exception {
ConfigurationMetadata metadata = compile(InnerClassAnnotatedGetterConfig.class);
assertThat(metadata, containsProperty("specific.value"));
assertThat(metadata, containsProperty("foo.name"));
assertThat(metadata, not(containsProperty("specific.foo")));
assertThat(metadata).has(Metadata.withProperty("specific.value"));
assertThat(metadata).has(Metadata.withProperty("foo.name"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("specific.foo"));
}
@Test
public void builderPojo() throws IOException {
ConfigurationMetadata metadata = compile(BuilderPojo.class);
assertThat(metadata, containsProperty("builder.name"));
assertThat(metadata).has(Metadata.withProperty("builder.name"));
}
@Test
public void excludedTypesPojo() throws IOException {
ConfigurationMetadata metadata = compile(ExcludedTypesPojo.class);
assertThat(metadata, containsProperty("excluded.name"));
assertThat(metadata, not(containsProperty("excluded.class-loader")));
assertThat(metadata, not(containsProperty("excluded.data-source")));
assertThat(metadata, not(containsProperty("excluded.print-writer")));
assertThat(metadata, not(containsProperty("excluded.writer")));
assertThat(metadata, not(containsProperty("excluded.writer-array")));
assertThat(metadata).has(Metadata.withProperty("excluded.name"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.class-loader"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.data-source"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.print-writer"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.writer"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.writer-array"));
}
@Test
@@ -352,28 +356,29 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Test
public void lombokInnerClassProperties() throws Exception {
ConfigurationMetadata metadata = compile(LombokInnerClassProperties.class);
assertThat(metadata,
containsGroup("config").fromSource(LombokInnerClassProperties.class));
assertThat(metadata,
containsGroup("config.first").ofType(LombokInnerClassProperties.Foo.class)
.fromSource(LombokInnerClassProperties.class));
assertThat(metadata, containsProperty("config.first.name"));
assertThat(metadata, containsProperty("config.first.bar.name"));
assertThat(metadata,
containsGroup("config.second", LombokInnerClassProperties.Foo.class)
.fromSource(LombokInnerClassProperties.class));
assertThat(metadata, containsProperty("config.second.name"));
assertThat(metadata, containsProperty("config.second.bar.name"));
assertThat(metadata, containsGroup("config.third").ofType(SimpleLombokPojo.class)
assertThat(metadata).has(Metadata.withGroup("config")
.fromSource(LombokInnerClassProperties.class));
assertThat(metadata).has(Metadata.withGroup("config.first")
.ofType(LombokInnerClassProperties.Foo.class)
.fromSource(LombokInnerClassProperties.class));
assertThat(metadata).has(Metadata.withProperty("config.first.name"));
assertThat(metadata).has(Metadata.withProperty("config.first.bar.name"));
assertThat(metadata).has(
Metadata.withGroup("config.second", LombokInnerClassProperties.Foo.class)
.fromSource(LombokInnerClassProperties.class));
assertThat(metadata).has(Metadata.withProperty("config.second.name"));
assertThat(metadata).has(Metadata.withProperty("config.second.bar.name"));
assertThat(metadata)
.has(Metadata.withGroup("config.third").ofType(SimpleLombokPojo.class)
.fromSource(LombokInnerClassProperties.class));
// For some reason the annotation processor resolves a type for SimpleLombokPojo
// that is resolved (compiled) and the source annotations are gone. Because we
// don't see the @Data annotation anymore, no field is harvested. What is crazy is
// that a sample project works fine so this seem to be related to the unit test
// environment for some reason. assertThat(metadata,
// containsProperty("config.third.value"));
assertThat(metadata, containsProperty("config.fourth"));
assertThat(metadata, not(containsGroup("config.fourth")));
assertThat(metadata).has(Metadata.withProperty("config.fourth"));
assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth"));
}
@Test
@@ -382,8 +387,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
AdditionalMetadata.class.getName(), null, null, null, null);
writeAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata, containsProperty("simple.comparator"));
assertThat(metadata, containsProperty("foo", String.class)
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).has(Metadata.withProperty("foo", String.class)
.fromSource(AdditionalMetadata.class));
}
@@ -393,10 +398,10 @@ public class ConfigurationMetadataAnnotationProcessorTests {
null, null, true, null);
writeAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata, containsProperty("simple.flag", Boolean.class)
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class).withDescription("A simple flag.")
.withDeprecation(null, null).withDefaultValue(is(true)));
assertThat(metadata.getItems().size(), is(4));
.withDeprecation(null, null).withDefaultValue(true));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
@@ -405,11 +410,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
null, null, "A nice comparator.", null, null);
writeAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata,
containsProperty("simple.comparator", "java.util.Comparator<?>")
assertThat(metadata)
.has(Metadata.withProperty("simple.comparator", "java.util.Comparator<?>")
.fromSource(SimpleProperties.class)
.withDescription("A nice comparator."));
assertThat(metadata.getItems().size(), is(4));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
@@ -419,11 +424,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
new ItemDeprecation("Don't use this.", "simple.complex-comparator"));
writeAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata,
containsProperty("simple.comparator", "java.util.Comparator<?>")
assertThat(metadata)
.has(Metadata.withProperty("simple.comparator", "java.util.Comparator<?>")
.fromSource(SimpleProperties.class)
.withDeprecation("Don't use this.", "simple.complex-comparator"));
assertThat(metadata.getItems().size(), is(4));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
@@ -433,11 +438,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
new ItemDeprecation("Don't use this.", "single.name"));
writeAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(DeprecatedSingleProperty.class);
assertThat(metadata,
containsProperty("singledeprecated.name", String.class.getName())
assertThat(metadata).has(
Metadata.withProperty("singledeprecated.name", String.class.getName())
.fromSource(DeprecatedSingleProperty.class)
.withDeprecation("Don't use this.", "single.name"));
assertThat(metadata.getItems().size(), is(3));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
@@ -456,12 +461,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
new ItemHint.ValueHint("boot", "Bla bla"),
new ItemHint.ValueHint("spring", null)));
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata,
containsProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue(is("boot")).withDeprecation(null, null));
assertThat(metadata, containsHint("simple.the-name")
assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot").withDeprecation(null, null));
assertThat(metadata).has(Metadata.withHint("simple.the-name")
.withValue(0, "boot", "Bla bla").withValue(1, "spring", null));
}
@@ -470,13 +474,12 @@ public class ConfigurationMetadataAnnotationProcessorTests {
writeAdditionalHints(ItemHint.newHint("simple.theName",
new ItemHint.ValueHint("boot", "Bla bla")));
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata,
containsProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue(is("boot")).withDeprecation(null, null));
assertThat(metadata,
containsHint("simple.the-name").withValue(0, "boot", "Bla bla"));
assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot").withDeprecation(null, null));
assertThat(metadata).has(
Metadata.withHint("simple.the-name").withValue(0, "boot", "Bla bla"));
}
@Test
@@ -489,12 +492,11 @@ public class ConfigurationMetadataAnnotationProcessorTests {
"org.foo")),
new ItemHint.ValueProvider("second", null))));
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata,
containsProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue(is("boot")).withDeprecation(null, null));
assertThat(metadata, containsHint("simple.the-name")
assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class)
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot").withDeprecation(null, null));
assertThat(metadata).has(Metadata.withHint("simple.the-name")
.withProvider("first", "target", "org.foo").withProvider("second"));
}
@@ -504,7 +506,7 @@ public class ConfigurationMetadataAnnotationProcessorTests {
"java.lang.String", null, null, null, null,
new ItemDeprecation("Lame name.", "simple.the-name")));
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata, containsProperty("simple.wrong-name", String.class)
assertThat(metadata).has(Metadata.withProperty("simple.wrong-name", String.class)
.withDeprecation("Lame name.", "simple.the-name"));
}
@@ -527,8 +529,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
additionalMetadata.write(writer);
writer.flush();
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata, containsProperty("simple.comparator"));
assertThat(metadata, containsProperty("foo", String.class)
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).has(Metadata.withProperty("foo", String.class)
.fromSource(AdditionalMetadata.class));
}
@@ -536,29 +538,29 @@ public class ConfigurationMetadataAnnotationProcessorTests {
public void incrementalBuild() throws Exception {
TestProject project = new TestProject(this.temporaryFolder, FooProperties.class,
BarProperties.class);
assertFalse(project.getOutputFile(MetadataStore.METADATA_PATH).exists());
assertThat(project.getOutputFile(MetadataStore.METADATA_PATH).exists()).isFalse();
ConfigurationMetadata metadata = project.fullBuild();
assertTrue(project.getOutputFile(MetadataStore.METADATA_PATH).exists());
assertThat(metadata,
containsProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata,
containsProperty("bar.counter").fromSource(BarProperties.class));
assertThat(project.getOutputFile(MetadataStore.METADATA_PATH).exists()).isTrue();
assertThat(metadata).has(
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata).has(
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
metadata = project.incrementalBuild(BarProperties.class);
assertThat(metadata,
containsProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata,
containsProperty("bar.counter").fromSource(BarProperties.class));
assertThat(metadata).has(
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata).has(
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
project.addSourceCode(BarProperties.class,
BarProperties.class.getResourceAsStream("BarProperties.snippet"));
metadata = project.incrementalBuild(BarProperties.class);
assertThat(metadata, containsProperty("bar.extra"));
assertThat(metadata, containsProperty("foo.counter"));
assertThat(metadata, containsProperty("bar.counter"));
assertThat(metadata).has(Metadata.withProperty("bar.extra"));
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
project.revert(BarProperties.class);
metadata = project.incrementalBuild(BarProperties.class);
assertThat(metadata, not(containsProperty("bar.extra")));
assertThat(metadata, containsProperty("foo.counter"));
assertThat(metadata, containsProperty("bar.counter"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.extra"));
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
}
@Test
@@ -566,13 +568,13 @@ public class ConfigurationMetadataAnnotationProcessorTests {
TestProject project = new TestProject(this.temporaryFolder, FooProperties.class,
BarProperties.class);
ConfigurationMetadata metadata = project.fullBuild();
assertThat(metadata, containsProperty("foo.counter"));
assertThat(metadata, containsProperty("bar.counter"));
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
project.replaceText(BarProperties.class, "@ConfigurationProperties",
"//@ConfigurationProperties");
metadata = project.incrementalBuild(BarProperties.class);
assertThat(metadata, containsProperty("foo.counter"));
assertThat(metadata, not(containsProperty("bar.counter")));
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.counter"));
}
@Test
@@ -580,35 +582,35 @@ public class ConfigurationMetadataAnnotationProcessorTests {
TestProject project = new TestProject(this.temporaryFolder, FooProperties.class,
BarProperties.class);
ConfigurationMetadata metadata = project.fullBuild();
assertThat(metadata,
containsProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata,
containsProperty("bar.counter").fromSource(BarProperties.class));
assertThat(metadata, not(
containsProperty("bar.counter").fromSource(RenamedBarProperties.class)));
assertThat(metadata).has(
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata).has(
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
assertThat(metadata).doesNotHave(Metadata.withProperty("bar.counter")
.fromSource(RenamedBarProperties.class));
project.delete(BarProperties.class);
project.add(RenamedBarProperties.class);
metadata = project.incrementalBuild(RenamedBarProperties.class);
assertThat(metadata,
containsProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata,
not(containsProperty("bar.counter").fromSource(BarProperties.class)));
assertThat(metadata,
containsProperty("bar.counter").fromSource(RenamedBarProperties.class));
assertThat(metadata).has(
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
assertThat(metadata).doesNotHave(
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
assertThat(metadata).has(Metadata.withProperty("bar.counter")
.fromSource(RenamedBarProperties.class));
}
private void assertSimpleLombokProperties(ConfigurationMetadata metadata,
Class<?> source, String prefix) {
assertThat(metadata, containsGroup(prefix).fromSource(source));
assertThat(metadata, not(containsProperty(prefix + ".id")));
assertThat(metadata, containsProperty(prefix + ".name", String.class)
assertThat(metadata).has(Metadata.withGroup(prefix).fromSource(source));
assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".id"));
assertThat(metadata).has(Metadata.withProperty(prefix + ".name", String.class)
.fromSource(source).withDescription("Name description."));
assertThat(metadata, containsProperty(prefix + ".description"));
assertThat(metadata, containsProperty(prefix + ".counter"));
assertThat(metadata, containsProperty(prefix + ".number").fromSource(source)
.withDefaultValue(is(0)).withDeprecation(null, null));
assertThat(metadata, containsProperty(prefix + ".items"));
assertThat(metadata, not(containsProperty(prefix + ".ignored")));
assertThat(metadata).has(Metadata.withProperty(prefix + ".description"));
assertThat(metadata).has(Metadata.withProperty(prefix + ".counter"));
assertThat(metadata).has(Metadata.withProperty(prefix + ".number")
.fromSource(source).withDefaultValue(0).withDeprecation(null, null));
assertThat(metadata).has(Metadata.withProperty(prefix + ".items"));
assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".ignored"));
}
private ConfigurationMetadata compile(Class<?>... types) throws IOException {

View File

@@ -1,416 +0,0 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.collection.IsMapContaining;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemHint;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* Hamcrest {@link Matcher} to help test {@link ConfigurationMetadata}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public final class ConfigurationMetadataMatchers {
private ConfigurationMetadataMatchers() {
}
public static ContainsItemMatcher containsGroup(String name) {
return new ContainsItemMatcher(ItemType.GROUP, name);
}
public static ContainsItemMatcher containsGroup(String name, Class<?> type) {
return new ContainsItemMatcher(ItemType.GROUP, name).ofType(type);
}
public static ContainsItemMatcher containsGroup(String name, String type) {
return new ContainsItemMatcher(ItemType.GROUP, name).ofType(type);
}
public static ContainsItemMatcher containsProperty(String name) {
return new ContainsItemMatcher(ItemType.PROPERTY, name);
}
public static ContainsItemMatcher containsProperty(String name, Class<?> type) {
return new ContainsItemMatcher(ItemType.PROPERTY, name).ofType(type);
}
public static ContainsItemMatcher containsProperty(String name, String type) {
return new ContainsItemMatcher(ItemType.PROPERTY, name).ofType(type);
}
public static ContainsHintMatcher containsHint(String name) {
return new ContainsHintMatcher(name);
}
public static class ContainsItemMatcher extends BaseMatcher<ConfigurationMetadata> {
private final ItemType itemType;
private final String name;
private final String type;
private final Class<?> sourceType;
private final String description;
private final Matcher<?> defaultValue;
private final ItemDeprecation deprecation;
public ContainsItemMatcher(ItemType itemType, String name) {
this(itemType, name, null, null, null, null, null);
}
public ContainsItemMatcher(ItemType itemType, String name, String type,
Class<?> sourceType, String description, Matcher<?> defaultValue,
ItemDeprecation deprecation) {
this.itemType = itemType;
this.name = name;
this.type = type;
this.sourceType = sourceType;
this.description = description;
this.defaultValue = defaultValue;
this.deprecation = deprecation;
}
@Override
public boolean matches(Object item) {
ConfigurationMetadata metadata = (ConfigurationMetadata) item;
ItemMetadata itemMetadata = getFirstItemWithName(metadata, this.name);
if (itemMetadata == null) {
return false;
}
if (this.type != null && !this.type.equals(itemMetadata.getType())) {
return false;
}
if (this.sourceType != null
&& !this.sourceType.getName().equals(itemMetadata.getSourceType())) {
return false;
}
if (this.defaultValue != null
&& !this.defaultValue.matches(itemMetadata.getDefaultValue())) {
return false;
}
if (this.description != null
&& !this.description.equals(itemMetadata.getDescription())) {
return false;
}
if (this.deprecation == null && itemMetadata.getDeprecation() != null) {
return false;
}
if (this.deprecation != null
&& !this.deprecation.equals(itemMetadata.getDeprecation())) {
return false;
}
return true;
}
@Override
public void describeMismatch(Object item, Description description) {
ConfigurationMetadata metadata = (ConfigurationMetadata) item;
ItemMetadata property = getFirstItemWithName(metadata, this.name);
if (property == null) {
description.appendText("missing " + this.itemType.toString().toLowerCase()
+ " " + this.name);
}
else {
description
.appendText("was " + this.itemType.toString().toLowerCase() + " ")
.appendValue(property);
}
}
@Override
public void describeTo(Description description) {
description.appendText("metadata containing " + this.name);
if (this.type != null) {
description.appendText(" dataType ").appendValue(this.type);
}
if (this.sourceType != null) {
description.appendText(" sourceType ").appendValue(this.sourceType);
}
if (this.defaultValue != null) {
description.appendText(" defaultValue ").appendValue(this.defaultValue);
}
if (this.description != null) {
description.appendText(" description ").appendValue(this.description);
}
if (this.deprecation != null) {
description.appendText(" deprecation ").appendValue(this.deprecation);
}
}
public ContainsItemMatcher ofType(Class<?> dataType) {
return new ContainsItemMatcher(this.itemType, this.name, dataType.getName(),
this.sourceType, this.description, this.defaultValue,
this.deprecation);
}
public ContainsItemMatcher ofType(String dataType) {
return new ContainsItemMatcher(this.itemType, this.name, dataType,
this.sourceType, this.description, this.defaultValue,
this.deprecation);
}
public ContainsItemMatcher fromSource(Class<?> sourceType) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
sourceType, this.description, this.defaultValue, this.deprecation);
}
public ContainsItemMatcher withDescription(String description) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, description, this.defaultValue, this.deprecation);
}
public ContainsItemMatcher withDefaultValue(Matcher<?> defaultValue) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, this.description, defaultValue, this.deprecation);
}
public ContainsItemMatcher withDeprecation(String reason, String replacement) {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, this.description, this.defaultValue,
new ItemDeprecation(reason, replacement));
}
public ContainsItemMatcher withNoDeprecation() {
return new ContainsItemMatcher(this.itemType, this.name, this.type,
this.sourceType, this.description, this.defaultValue, null);
}
private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata,
String name) {
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(this.itemType) && name.equals(item.getName())) {
return item;
}
}
return null;
}
}
public static class ContainsHintMatcher extends BaseMatcher<ConfigurationMetadata> {
private final String name;
private final List<ValueHintMatcher> values;
private final List<ValueProviderMatcher> providers;
public ContainsHintMatcher(String name) {
this(name, new ArrayList<ValueHintMatcher>(),
new ArrayList<ValueProviderMatcher>());
}
public ContainsHintMatcher(String name, List<ValueHintMatcher> values,
List<ValueProviderMatcher> providers) {
this.name = name;
this.values = values;
this.providers = providers;
}
@Override
public boolean matches(Object item) {
ConfigurationMetadata metadata = (ConfigurationMetadata) item;
ItemHint itemHint = getFirstHintWithName(metadata, this.name);
if (itemHint == null) {
return false;
}
for (ValueHintMatcher value : this.values) {
if (!value.matches(itemHint)) {
return false;
}
}
for (ValueProviderMatcher provider : this.providers) {
if (!provider.matches(itemHint)) {
return false;
}
}
return true;
}
@Override
public void describeMismatch(Object item, Description description) {
ConfigurationMetadata metadata = (ConfigurationMetadata) item;
ItemHint itemHint = getFirstHintWithName(metadata, this.name);
if (itemHint == null) {
description.appendText("missing hint " + this.name);
}
else {
description.appendText("was hint ").appendValue(itemHint);
}
}
@Override
public void describeTo(Description description) {
description.appendText("hints for " + this.name);
if (this.values != null) {
description.appendText(" values ").appendValue(this.values);
}
if (this.providers != null) {
description.appendText(" providers ").appendValue(this.providers);
}
}
public ContainsHintMatcher withValue(int index, Object value,
String description) {
List<ValueHintMatcher> values = new ArrayList<ValueHintMatcher>(this.values);
values.add(new ValueHintMatcher(index, value, description));
return new ContainsHintMatcher(this.name, values, this.providers);
}
public ContainsHintMatcher withProvider(int index, String provider,
Map<String, Object> parameters) {
List<ValueProviderMatcher> providers = new ArrayList<ValueProviderMatcher>(
this.providers);
providers.add(new ValueProviderMatcher(index, provider, parameters));
return new ContainsHintMatcher(this.name, this.values, providers);
}
public ContainsHintMatcher withProvider(String provider, String key,
Object value) {
return withProvider(this.providers.size(), provider,
Collections.singletonMap(key, value));
}
public ContainsHintMatcher withProvider(String provider) {
return withProvider(this.providers.size(), provider, null);
}
private ItemHint getFirstHintWithName(ConfigurationMetadata metadata,
String name) {
for (ItemHint hint : metadata.getHints()) {
if (name.equals(hint.getName())) {
return hint;
}
}
return null;
}
}
public static class ValueHintMatcher extends BaseMatcher<ItemHint> {
private final int index;
private final Object value;
private final String description;
public ValueHintMatcher(int index, Object value, String description) {
this.index = index;
this.value = value;
this.description = description;
}
@Override
public boolean matches(Object item) {
ItemHint hint = (ItemHint) item;
if (this.index + 1 > hint.getValues().size()) {
return false;
}
ItemHint.ValueHint valueHint = hint.getValues().get(this.index);
if (this.value != null && !this.value.equals(valueHint.getValue())) {
return false;
}
if (this.description != null
&& !this.description.equals(valueHint.getDescription())) {
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("value hint at index '" + this.index + "'");
if (this.value != null) {
description.appendText(" value ").appendValue(this.value);
}
if (this.description != null) {
description.appendText(" description ").appendValue(this.description);
}
}
}
public static class ValueProviderMatcher extends BaseMatcher<ItemHint> {
private final int index;
private final String name;
private final Map<String, Object> parameters;
public ValueProviderMatcher(int index, String name,
Map<String, Object> parameters) {
this.index = index;
this.name = name;
this.parameters = parameters;
}
@Override
public boolean matches(Object item) {
ItemHint hint = (ItemHint) item;
if (this.index + 1 > hint.getProviders().size()) {
return false;
}
ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index);
if (this.name != null && !this.name.equals(valueProvider.getName())) {
return false;
}
if (this.parameters != null) {
for (Map.Entry<String, Object> entry : this.parameters.entrySet()) {
if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue())
.matches(valueProvider.getParameters())) {
return false;
}
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("value provider ");
if (this.name != null) {
description.appendText(" name ").appendValue(this.name);
}
if (this.parameters != null) {
description.appendText(" parameters ").appendValue(this.parameters);
}
}
}
}

View File

@@ -0,0 +1,401 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.assertj.core.api.Condition;
import org.hamcrest.collection.IsMapContaining;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemHint;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
import org.springframework.util.ObjectUtils;
/**
* AssertJ {@link Condition} to help test {@link ConfigurationMetadata}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public final class Metadata {
private Metadata() {
}
public static MetadataItemCondition withGroup(String name) {
return new MetadataItemCondition(ItemType.GROUP, name);
}
public static MetadataItemCondition withGroup(String name, Class<?> type) {
return new MetadataItemCondition(ItemType.GROUP, name).ofType(type);
}
public static MetadataItemCondition withGroup(String name, String type) {
return new MetadataItemCondition(ItemType.GROUP, name).ofType(type);
}
public static MetadataItemCondition withProperty(String name) {
return new MetadataItemCondition(ItemType.PROPERTY, name);
}
public static MetadataItemCondition withProperty(String name, Class<?> type) {
return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type);
}
public static MetadataItemCondition withProperty(String name, String type) {
return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type);
}
public static MetadataHintCondition withHint(String name) {
return new MetadataHintCondition(name);
}
public static class MetadataItemCondition extends Condition<ConfigurationMetadata> {
private final ItemType itemType;
private final String name;
private final String type;
private final Class<?> sourceType;
private final String description;
private final Object defaultValue;
private final ItemDeprecation deprecation;
public MetadataItemCondition(ItemType itemType, String name) {
this(itemType, name, null, null, null, null, null);
}
public MetadataItemCondition(ItemType itemType, String name, String type,
Class<?> sourceType, String description, Object defaultValue,
ItemDeprecation deprecation) {
this.itemType = itemType;
this.name = name;
this.type = type;
this.sourceType = sourceType;
this.description = description;
this.defaultValue = defaultValue;
this.deprecation = deprecation;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("an item named '" + this.name + "'");
if (this.type != null) {
description.append(" with dataType:").append(this.type);
}
if (this.sourceType != null) {
description.append(" with sourceType:").append(this.sourceType);
}
if (this.defaultValue != null) {
description.append(" with defaultValue:").append(this.defaultValue);
}
if (this.description != null) {
description.append(" with description:").append(this.description);
}
if (this.deprecation != null) {
description.append(" with deprecation:").append(this.deprecation);
}
return description.toString();
}
@Override
public boolean matches(ConfigurationMetadata value) {
ItemMetadata itemMetadata = getFirstItemWithName(value, this.name);
if (itemMetadata == null) {
return false;
}
if (this.type != null && !this.type.equals(itemMetadata.getType())) {
return false;
}
if (this.sourceType != null
&& !this.sourceType.getName().equals(itemMetadata.getSourceType())) {
return false;
}
if (this.defaultValue != null && !ObjectUtils
.nullSafeEquals(this.defaultValue, itemMetadata.getDefaultValue())) {
return false;
}
if (this.description != null
&& !this.description.equals(itemMetadata.getDescription())) {
return false;
}
if (this.deprecation == null && itemMetadata.getDeprecation() != null) {
return false;
}
if (this.deprecation != null
&& !this.deprecation.equals(itemMetadata.getDeprecation())) {
return false;
}
return true;
}
public MetadataItemCondition ofType(Class<?> dataType) {
return new MetadataItemCondition(this.itemType, this.name, dataType.getName(),
this.sourceType, this.description, this.defaultValue,
this.deprecation);
}
public MetadataItemCondition ofType(String dataType) {
return new MetadataItemCondition(this.itemType, this.name, dataType,
this.sourceType, this.description, this.defaultValue,
this.deprecation);
}
public MetadataItemCondition fromSource(Class<?> sourceType) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
sourceType, this.description, this.defaultValue, this.deprecation);
}
public MetadataItemCondition withDescription(String description) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, description, this.defaultValue, this.deprecation);
}
public MetadataItemCondition withDefaultValue(Object defaultValue) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.description, defaultValue, this.deprecation);
}
public MetadataItemCondition withDeprecation(String reason, String replacement) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.description, this.defaultValue,
new ItemDeprecation(reason, replacement));
}
public MetadataItemCondition withNoDeprecation() {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.description, this.defaultValue, null);
}
private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata,
String name) {
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(this.itemType) && name.equals(item.getName())) {
return item;
}
}
return null;
}
}
public static class MetadataHintCondition extends Condition<ConfigurationMetadata> {
private final String name;
private final List<ItemHintValueCondition> valueConditions;
private final List<ItemHintProviderCondition> providerConditions;
public MetadataHintCondition(String name) {
this.name = name;
this.valueConditions = Collections.emptyList();
this.providerConditions = Collections.emptyList();
}
public MetadataHintCondition(String name,
List<ItemHintValueCondition> valueConditions,
List<ItemHintProviderCondition> providerConditions) {
this.name = name;
this.valueConditions = valueConditions;
this.providerConditions = providerConditions;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("a hints name '" + this.name + "'");
if (!this.valueConditions.isEmpty()) {
description.append(" with values:").append(this.valueConditions);
}
if (!this.providerConditions.isEmpty()) {
description.append(" with providers:").append(this.providerConditions);
}
return description.toString();
}
@Override
public boolean matches(ConfigurationMetadata metadata) {
ItemHint itemHint = getFirstHintWithName(metadata, this.name);
if (itemHint == null) {
return false;
}
return matches(itemHint, this.valueConditions)
&& matches(itemHint, this.providerConditions);
}
private boolean matches(ItemHint itemHint,
List<? extends Condition<ItemHint>> conditions) {
for (Condition<ItemHint> condition : conditions) {
if (!condition.matches(itemHint)) {
return false;
}
}
return true;
}
private ItemHint getFirstHintWithName(ConfigurationMetadata metadata,
String name) {
for (ItemHint hint : metadata.getHints()) {
if (name.equals(hint.getName())) {
return hint;
}
}
return null;
}
public MetadataHintCondition withValue(int index, Object value,
String description) {
return new MetadataHintCondition(this.name,
add(this.valueConditions,
new ItemHintValueCondition(index, value, description)),
this.providerConditions);
}
public MetadataHintCondition withProvider(String provider) {
return withProvider(this.providerConditions.size(), provider, null);
}
public MetadataHintCondition withProvider(String provider, String key,
Object value) {
return withProvider(this.providerConditions.size(), provider,
Collections.singletonMap(key, value));
}
public MetadataHintCondition withProvider(int index, String provider,
Map<String, Object> parameters) {
return new MetadataHintCondition(this.name, this.valueConditions,
add(this.providerConditions,
new ItemHintProviderCondition(index, provider, parameters)));
}
private <T> List<T> add(List<T> items, T item) {
List<T> result = new ArrayList<T>(items);
result.add(item);
return result;
}
}
private static class ItemHintValueCondition extends Condition<ItemHint> {
private final int index;
private final Object value;
private final String description;
ItemHintValueCondition(int index, Object value, String description) {
this.index = index;
this.value = value;
this.description = description;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("value hint at index '" + this.index + "'");
if (this.value != null) {
description.append(" with value:").append(this.value);
}
if (this.description != null) {
description.append(" with description:").append(this.description);
}
return description.toString();
}
@Override
public boolean matches(ItemHint value) {
if (this.index + 1 > value.getValues().size()) {
return false;
}
ItemHint.ValueHint valueHint = value.getValues().get(this.index);
if (this.value != null && !this.value.equals(valueHint.getValue())) {
return false;
}
if (this.description != null
&& !this.description.equals(valueHint.getDescription())) {
return false;
}
return true;
}
}
private static class ItemHintProviderCondition extends Condition<ItemHint> {
private final int index;
private final String name;
private final Map<String, Object> parameters;
ItemHintProviderCondition(int index, String name,
Map<String, Object> parameters) {
this.index = index;
this.name = name;
this.parameters = parameters;
describedAs(createDescription());
}
public String createDescription() {
StringBuilder description = new StringBuilder();
description.append("value provider");
if (this.name != null) {
description.append(" with name:").append(this.name);
}
if (this.parameters != null) {
description.append(" with parameters:").append(this.parameters);
}
return description.toString();
}
@Override
public boolean matches(ItemHint hint) {
if (this.index + 1 > hint.getProviders().size()) {
return false;
}
ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index);
if (this.name != null && !this.name.equals(valueProvider.getName())) {
return false;
}
if (this.parameters != null) {
for (Map.Entry<String, Object> entry : this.parameters.entrySet()) {
if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue())
.matches(valueProvider.getParameters())) {
return false;
}
}
}
return true;
}
}
}

View File

@@ -28,6 +28,8 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
/**
* Test {@link ConfigurationMetadataAnnotationProcessor}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Andy Wilkinson

View File

@@ -29,7 +29,6 @@ import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -37,9 +36,7 @@ import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.TestCompiler;
import org.springframework.boot.configurationsample.fieldvalues.FieldValues;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Abstract base class for {@link FieldValuesParser} tests.
@@ -60,43 +57,37 @@ public abstract class AbstractFieldValuesProcessorTests {
TestCompiler compiler = new TestCompiler(this.temporaryFolder);
compiler.getTask(FieldValues.class).call(processor);
Map<String, Object> values = processor.getValues();
assertThat(values.get("string"), equalToObject("1"));
assertThat(values.get("stringNone"), nullValue());
assertThat(values.get("stringConst"), equalToObject("c"));
assertThat(values.get("bool"), equalToObject(true));
assertThat(values.get("boolNone"), equalToObject(false));
assertThat(values.get("boolConst"), equalToObject(true));
assertThat(values.get("boolObject"), equalToObject(true));
assertThat(values.get("boolObjectNone"), nullValue());
assertThat(values.get("boolObjectConst"), equalToObject(true));
assertThat(values.get("integer"), equalToObject(1));
assertThat(values.get("integerNone"), equalToObject(0));
assertThat(values.get("integerConst"), equalToObject(2));
assertThat(values.get("integerObject"), equalToObject(3));
assertThat(values.get("integerObjectNone"), nullValue());
assertThat(values.get("integerObjectConst"), equalToObject(4));
assertThat(values.get("charset"), equalToObject("US-ASCII"));
assertThat(values.get("charsetConst"), equalToObject("UTF-8"));
assertThat(values.get("mimeType"), equalToObject("text/html"));
assertThat(values.get("mimeTypeConst"), equalToObject("text/plain"));
assertThat(values.get("object"), equalToObject(123));
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 }));
assertThat(values.get("unknownArray"), nullValue());
}
private Matcher<Object> equalToObject(Object object) {
return equalTo(object);
assertThat(values.get("string")).isEqualTo("1");
assertThat(values.get("stringNone")).isNull();
assertThat(values.get("stringConst")).isEqualTo("c");
assertThat(values.get("bool")).isEqualTo(true);
assertThat(values.get("boolNone")).isEqualTo(false);
assertThat(values.get("boolConst")).isEqualTo(true);
assertThat(values.get("boolObject")).isEqualTo(true);
assertThat(values.get("boolObjectNone")).isNull();
assertThat(values.get("boolObjectConst")).isEqualTo(true);
assertThat(values.get("integer")).isEqualTo(1);
assertThat(values.get("integerNone")).isEqualTo(0);
assertThat(values.get("integerConst")).isEqualTo(2);
assertThat(values.get("integerObject")).isEqualTo(3);
assertThat(values.get("integerObjectNone")).isNull();
assertThat(values.get("integerObjectConst")).isEqualTo(4);
assertThat(values.get("charset")).isEqualTo("US-ASCII");
assertThat(values.get("charsetConst")).isEqualTo("UTF-8");
assertThat(values.get("mimeType")).isEqualTo("text/html");
assertThat(values.get("mimeTypeConst")).isEqualTo("text/plain");
assertThat(values.get("object")).isEqualTo(123);
assertThat(values.get("objectNone")).isNull();
assertThat(values.get("objectConst")).isEqualTo("c");
assertThat(values.get("objectInstance")).isNull();
assertThat(values.get("stringArray")).isEqualTo(new Object[] { "FOO", "BAR" });
assertThat(values.get("stringArrayNone")).isNull();
assertThat(values.get("stringEmptyArray")).isEqualTo(new Object[0]);
assertThat(values.get("stringArrayConst")).isEqualTo(new Object[] { "OK", "KO" });
assertThat(values.get("stringArrayConstElements"))
.isEqualTo(new Object[] { "c" });
assertThat(values.get("integerArray")).isEqualTo(new Object[] { 42, 24 });
assertThat(values.get("unknownArray")).isNull();
}
@SupportedAnnotationTypes({

View File

@@ -18,8 +18,7 @@ package org.springframework.boot.configurationprocessor.metadata;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigurationMetadata}.
@@ -30,43 +29,44 @@ public class ConfigurationMetadataTests {
@Test
public void toDashedCaseCamelCase() {
assertThat(toDashedCase("simpleCamelCase"), is("simple-camel-case"));
assertThat(toDashedCase("simpleCamelCase")).isEqualTo("simple-camel-case");
}
@Test
public void toDashedCaseWordsUnderscore() {
assertThat(toDashedCase("Word_With_underscore"), is("word_with_underscore"));
assertThat(toDashedCase("Word_With_underscore"))
.isEqualTo("word_with_underscore");
}
@Test
public void toDashedCaseWordsSeveralUnderscores() {
assertThat(toDashedCase("Word___With__underscore"),
is("word___with__underscore"));
assertThat(toDashedCase("Word___With__underscore"))
.isEqualTo("word___with__underscore");
}
@Test
public void toDashedCaseLowerCaseUnderscore() {
assertThat(toDashedCase("lower_underscore"), is("lower_underscore"));
assertThat(toDashedCase("lower_underscore")).isEqualTo("lower_underscore");
}
@Test
public void toDashedCaseUpperUnderscore() {
assertThat(toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore"));
assertThat(toDashedCase("UPPER_UNDERSCORE")).isEqualTo("upper_underscore");
}
@Test
public void toDashedCaseMultipleUnderscores() {
assertThat(toDashedCase("super___crazy"), is("super___crazy"));
assertThat(toDashedCase("super___crazy")).isEqualTo("super___crazy");
}
@Test
public void toDashedCaseUppercase() {
assertThat(toDashedCase("UPPERCASE"), is("uppercase"));
assertThat(toDashedCase("UPPERCASE")).isEqualTo("uppercase");
}
@Test
public void toDashedCaseLowercase() {
assertThat(toDashedCase("lowercase"), is("lowercase"));
assertThat(toDashedCase("lowercase")).isEqualTo("lowercase");
}
private String toDashedCase(String name) {

View File

@@ -25,11 +25,9 @@ import java.util.Collections;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsHint;
import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty;
import org.springframework.boot.configurationprocessor.Metadata;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JsonMarshaller}.
@@ -70,22 +68,21 @@ public class JsonMarshallerTests {
marshaller.write(metadata, outputStream);
ConfigurationMetadata read = marshaller
.read(new ByteArrayInputStream(outputStream.toByteArray()));
assertThat(read,
containsProperty("a.b", StringBuffer.class).fromSource(InputStream.class)
.withDescription("desc").withDefaultValue(is("x"))
.withDeprecation("Deprecation comment", "b.c.d"));
assertThat(read, containsProperty("b.c.d"));
assertThat(read, containsProperty("c").withDefaultValue(is(123)));
assertThat(read, containsProperty("d").withDefaultValue(is(true)));
assertThat(read,
containsProperty("e").withDefaultValue(is(new String[] { "y", "n" })));
assertThat(read, containsProperty("f")
.withDefaultValue(is(new boolean[] { true, false })));
assertThat(read, containsGroup("d"));
assertThat(read, containsHint("a.b"));
assertThat(read,
containsHint("c").withValue(0, 123, "hey").withValue(1, 456, null));
assertThat(read, containsHint("d").withProvider("first", "target", "foo")
assertThat(read).has(Metadata.withProperty("a.b", StringBuffer.class)
.fromSource(InputStream.class).withDescription("desc")
.withDefaultValue("x").withDeprecation("Deprecation comment", "b.c.d"));
assertThat(read).has(Metadata.withProperty("b.c.d"));
assertThat(read).has(Metadata.withProperty("c").withDefaultValue(123));
assertThat(read).has(Metadata.withProperty("d").withDefaultValue(true));
assertThat(read).has(
Metadata.withProperty("e").withDefaultValue(new String[] { "y", "n" }));
assertThat(read).has(Metadata.withProperty("f")
.withDefaultValue(new Object[] { true, false }));
assertThat(read).has(Metadata.withGroup("d"));
assertThat(read).has(Metadata.withHint("a.b"));
assertThat(read).has(
Metadata.withHint("c").withValue(0, 123, "hey").withValue(1, 456, null));
assertThat(read).has(Metadata.withHint("d").withProvider("first", "target", "foo")
.withProvider("second"));
}