Skip to content

Commit a4a19e1

Browse files
committed
Polish
This commit makes sure that the defaultValue has to be provided in assertions. If not present, no defaultValue should be generated.
1 parent 674170a commit a4a19e1

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

+49-25
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void simpleProperties() throws Exception {
122122
.withDescription("The name of this simple properties.")
123123
.withDefaultValue("boot").withDeprecation(null, null));
124124
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
125+
.withDefaultValue(false)
125126
.fromSource(SimpleProperties.class).withDescription("A simple flag.")
126127
.withDeprecation(null, null));
127128
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
@@ -148,27 +149,32 @@ public void simpleTypeProperties() throws Exception {
148149
assertThat(metadata)
149150
.has(Metadata.withProperty("simple.type.my-byte", Byte.class));
150151
assertThat(metadata)
151-
.has(Metadata.withProperty("simple.type.my-primitive-byte", Byte.class));
152+
.has(Metadata.withProperty("simple.type.my-primitive-byte", Byte.class)
153+
.withDefaultValue(0));
152154
assertThat(metadata)
153155
.has(Metadata.withProperty("simple.type.my-char", Character.class));
154156
assertThat(metadata).has(
155157
Metadata.withProperty("simple.type.my-primitive-char", Character.class));
156158
assertThat(metadata)
157159
.has(Metadata.withProperty("simple.type.my-boolean", Boolean.class));
158160
assertThat(metadata).has(
159-
Metadata.withProperty("simple.type.my-primitive-boolean", Boolean.class));
161+
Metadata.withProperty("simple.type.my-primitive-boolean", Boolean.class)
162+
.withDefaultValue(false));
160163
assertThat(metadata)
161164
.has(Metadata.withProperty("simple.type.my-short", Short.class));
162165
assertThat(metadata).has(
163-
Metadata.withProperty("simple.type.my-primitive-short", Short.class));
166+
Metadata.withProperty("simple.type.my-primitive-short", Short.class)
167+
.withDefaultValue(0));
164168
assertThat(metadata)
165169
.has(Metadata.withProperty("simple.type.my-integer", Integer.class));
166170
assertThat(metadata).has(
167-
Metadata.withProperty("simple.type.my-primitive-integer", Integer.class));
171+
Metadata.withProperty("simple.type.my-primitive-integer", Integer.class)
172+
.withDefaultValue(0));
168173
assertThat(metadata)
169174
.has(Metadata.withProperty("simple.type.my-long", Long.class));
170175
assertThat(metadata)
171-
.has(Metadata.withProperty("simple.type.my-primitive-long", Long.class));
176+
.has(Metadata.withProperty("simple.type.my-primitive-long", Long.class)
177+
.withDefaultValue(0));
172178
assertThat(metadata)
173179
.has(Metadata.withProperty("simple.type.my-double", Double.class));
174180
assertThat(metadata).has(
@@ -230,7 +236,7 @@ public void deprecatedOnUnrelatedSetter() throws Exception {
230236
.withNoDeprecation().fromSource(type));
231237
assertThat(metadata)
232238
.has(Metadata.withProperty("not.deprecated.flag", Boolean.class)
233-
.withNoDeprecation().fromSource(type));
239+
.withDefaultValue(false).withNoDeprecation().fromSource(type));
234240
}
235241

236242
@Test
@@ -239,7 +245,8 @@ public void boxingOnSetter() throws IOException {
239245
ConfigurationMetadata metadata = compile(type);
240246
assertThat(metadata).has(Metadata.withGroup("boxing").fromSource(type));
241247
assertThat(metadata).has(
242-
Metadata.withProperty("boxing.flag", Boolean.class).fromSource(type));
248+
Metadata.withProperty("boxing.flag", Boolean.class)
249+
.withDefaultValue(false).fromSource(type));
243250
assertThat(metadata).has(
244251
Metadata.withProperty("boxing.counter", Integer.class).fromSource(type));
245252
}
@@ -271,7 +278,7 @@ public void simpleMethodConfig() throws Exception {
271278
assertThat(metadata).has(Metadata.withProperty("foo.name", String.class)
272279
.fromSource(SimpleMethodConfig.Foo.class));
273280
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
274-
.fromSource(SimpleMethodConfig.Foo.class));
281+
.withDefaultValue(false).fromSource(SimpleMethodConfig.Foo.class));
275282
}
276283

277284
@Test
@@ -288,7 +295,7 @@ public void methodAndClassConfig() throws Exception {
288295
assertThat(metadata).has(Metadata.withProperty("conflict.name", String.class)
289296
.fromSource(MethodAndClassConfig.Foo.class));
290297
assertThat(metadata).has(Metadata.withProperty("conflict.flag", Boolean.class)
291-
.fromSource(MethodAndClassConfig.Foo.class));
298+
.withDefaultValue(false).fromSource(MethodAndClassConfig.Foo.class));
292299
assertThat(metadata).has(Metadata.withProperty("conflict.value", String.class)
293300
.fromSource(MethodAndClassConfig.class));
294301
}
@@ -308,6 +315,7 @@ public void deprecatedMethodConfig() throws Exception {
308315
.fromSource(DeprecatedMethodConfig.Foo.class)
309316
.withDeprecation(null, null));
310317
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
318+
.withDefaultValue(false)
311319
.fromSource(DeprecatedMethodConfig.Foo.class)
312320
.withDeprecation(null, null));
313321
}
@@ -323,6 +331,7 @@ public void deprecatedMethodConfigOnClass() throws Exception {
323331
org.springframework.boot.configurationsample.method.DeprecatedClassMethodConfig.Foo.class)
324332
.withDeprecation(null, null));
325333
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
334+
.withDefaultValue(false)
326335
.fromSource(
327336
org.springframework.boot.configurationsample.method.DeprecatedClassMethodConfig.Foo.class)
328337
.withDeprecation(null, null));
@@ -857,38 +866,49 @@ public void incrementalBuild() throws Exception {
857866
ConfigurationMetadata metadata = project.fullBuild();
858867
assertThat(project.getOutputFile(MetadataStore.METADATA_PATH).exists()).isTrue();
859868
assertThat(metadata).has(
860-
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
869+
Metadata.withProperty("foo.counter").fromSource(FooProperties.class)
870+
.withDefaultValue(0));
861871
assertThat(metadata).has(
862-
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
872+
Metadata.withProperty("bar.counter").fromSource(BarProperties.class)
873+
.withDefaultValue(0));
863874
metadata = project.incrementalBuild(BarProperties.class);
864875
assertThat(metadata).has(
865-
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
876+
Metadata.withProperty("foo.counter").fromSource(FooProperties.class)
877+
.withDefaultValue(0));
866878
assertThat(metadata).has(
867-
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
879+
Metadata.withProperty("bar.counter").fromSource(BarProperties.class)
880+
.withDefaultValue(0));
868881
project.addSourceCode(BarProperties.class,
869882
BarProperties.class.getResourceAsStream("BarProperties.snippet"));
870883
metadata = project.incrementalBuild(BarProperties.class);
871884
assertThat(metadata).has(Metadata.withProperty("bar.extra"));
872-
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
873-
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
885+
assertThat(metadata).has(Metadata.withProperty("foo.counter")
886+
.withDefaultValue(0));
887+
assertThat(metadata).has(Metadata.withProperty("bar.counter")
888+
.withDefaultValue(0));
874889
project.revert(BarProperties.class);
875890
metadata = project.incrementalBuild(BarProperties.class);
876891
assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.extra"));
877-
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
878-
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
892+
assertThat(metadata).has(Metadata.withProperty("foo.counter")
893+
.withDefaultValue(0));
894+
assertThat(metadata).has(Metadata.withProperty("bar.counter")
895+
.withDefaultValue(0));
879896
}
880897

881898
@Test
882899
public void incrementalBuildAnnotationRemoved() throws Exception {
883900
TestProject project = new TestProject(this.temporaryFolder, FooProperties.class,
884901
BarProperties.class);
885902
ConfigurationMetadata metadata = project.fullBuild();
886-
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
887-
assertThat(metadata).has(Metadata.withProperty("bar.counter"));
903+
assertThat(metadata).has(Metadata.withProperty("foo.counter")
904+
.withDefaultValue(0));
905+
assertThat(metadata).has(Metadata.withProperty("bar.counter")
906+
.withDefaultValue(0));
888907
project.replaceText(BarProperties.class, "@ConfigurationProperties",
889908
"//@ConfigurationProperties");
890909
metadata = project.incrementalBuild(BarProperties.class);
891-
assertThat(metadata).has(Metadata.withProperty("foo.counter"));
910+
assertThat(metadata).has(Metadata.withProperty("foo.counter")
911+
.withDefaultValue(0));
892912
assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.counter"));
893913
}
894914

@@ -898,20 +918,24 @@ public void incrementalBuildTypeRenamed() throws Exception {
898918
BarProperties.class);
899919
ConfigurationMetadata metadata = project.fullBuild();
900920
assertThat(metadata).has(
901-
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
921+
Metadata.withProperty("foo.counter").fromSource(FooProperties.class)
922+
.withDefaultValue(0));
902923
assertThat(metadata).has(
903-
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
924+
Metadata.withProperty("bar.counter").fromSource(BarProperties.class)
925+
.withDefaultValue(0));
904926
assertThat(metadata).doesNotHave(Metadata.withProperty("bar.counter")
905927
.fromSource(RenamedBarProperties.class));
906928
project.delete(BarProperties.class);
907929
project.add(RenamedBarProperties.class);
908930
metadata = project.incrementalBuild(RenamedBarProperties.class);
909931
assertThat(metadata).has(
910-
Metadata.withProperty("foo.counter").fromSource(FooProperties.class));
932+
Metadata.withProperty("foo.counter").fromSource(FooProperties.class)
933+
.withDefaultValue(0));
911934
assertThat(metadata).doesNotHave(
912-
Metadata.withProperty("bar.counter").fromSource(BarProperties.class));
935+
Metadata.withProperty("bar.counter").fromSource(BarProperties.class)
936+
.withDefaultValue(0));
913937
assertThat(metadata).has(Metadata.withProperty("bar.counter")
914-
.fromSource(RenamedBarProperties.class));
938+
.withDefaultValue(0).fromSource(RenamedBarProperties.class));
915939
}
916940

917941
private void assertSimpleLombokProperties(ConfigurationMetadata metadata,

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/Metadata.java

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ public boolean matches(ConfigurationMetadata value) {
151151
.nullSafeEquals(this.defaultValue, itemMetadata.getDefaultValue())) {
152152
return false;
153153
}
154+
if (this.defaultValue == null && itemMetadata.getDefaultValue() != null) {
155+
return false;
156+
}
154157
if (this.description != null
155158
&& !this.description.equals(itemMetadata.getDescription())) {
156159
return false;

0 commit comments

Comments
 (0)