@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 2020 the original author or authors .
* Copyright 2012 - 2022 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 .
@ -16,8 +16,6 @@
@@ -16,8 +16,6 @@
package org.springframework.boot.configurationprocessor ;
import java.io.File ;
import java.io.FileWriter ;
import java.io.IOException ;
import java.util.Arrays ;
import java.util.Collections ;
@ -37,15 +35,16 @@ import org.springframework.boot.configurationprocessor.metadata.TestJsonConverte
@@ -37,15 +35,16 @@ import org.springframework.boot.configurationprocessor.metadata.TestJsonConverte
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty ;
import org.springframework.boot.configurationsample.simple.SimpleProperties ;
import org.springframework.boot.configurationsample.specific.SimpleConflictingProperties ;
import org.springframework.util.FileCopyUtils ;
import org.springframework.core.test.tools.CompilationException ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatIllegalState Exception ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
/ * *
* Metadata generation tests for merging additional metadata .
*
* @author Stephane Nicoll
* @author Scott Frederick
* /
class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@ -53,8 +52,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -53,8 +52,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergingOfAdditionalProperty ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( null , "foo" , "java.lang.String" ,
AdditionalMetadata . class . getName ( ) , null , null , null , null ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple.comparator" ) ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "foo" , String . class ) . fromSource ( AdditionalMetadata . class ) ) ;
}
@ -63,8 +62,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -63,8 +62,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergingOfAdditionalPropertyMatchingGroup ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( null , "simple" , "java.lang.String" , null , null , null , null ,
null ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withGroup ( "simple" ) . fromSource ( SimpleProperties . class ) ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple" , String . class ) ) ;
}
@ -72,8 +71,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -72,8 +71,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergeExistingPropertyDefaultValue ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "simple" , "flag" , null , null , null , null , true , null ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple.flag" , Boolean . class ) . fromSource ( SimpleProperties . class )
. withDescription ( "A simple flag." ) . withDeprecation ( null , null ) . withDefaultValue ( true ) ) ;
assertThat ( metadata . getItems ( ) ) . hasSize ( 4 ) ;
@ -83,8 +82,9 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -83,8 +82,9 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergeExistingPropertyWithSeveralCandidates ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "simple" , "flag" , Boolean . class . getName ( ) , null , null , null ,
true , null ) ;
writeAdditionalMetadata ( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class , SimpleConflictingProperties . class ) ;
String additionalMetadata = buildAdditionalMetadata ( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ,
SimpleConflictingProperties . class ) ;
assertThat ( metadata . getItems ( ) ) . hasSize ( 6 ) ;
List < ItemMetadata > items = metadata . getItems ( ) . stream ( ) . filter ( ( item ) - > item . getName ( ) . equals ( "simple.flag" ) )
. collect ( Collectors . toList ( ) ) ;
@ -107,8 +107,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -107,8 +107,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergeExistingPropertyDescription ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "simple" , "comparator" , null , null , null , "A nice comparator." ,
null , null ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple.comparator" , "java.util.Comparator<?>" )
. fromSource ( SimpleProperties . class ) . withDescription ( "A nice comparator." ) ) ;
assertThat ( metadata . getItems ( ) ) . hasSize ( 4 ) ;
@ -118,8 +118,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -118,8 +118,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergeExistingPropertyDeprecation ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "simple" , "comparator" , null , null , null , null , null ,
new ItemDeprecation ( "Don't use this." , "simple.complex-comparator" , "error" ) ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has (
Metadata . withProperty ( "simple.comparator" , "java.util.Comparator<?>" ) . fromSource ( SimpleProperties . class )
. withDeprecation ( "Don't use this." , "simple.complex-comparator" , "error" ) ) ;
@ -130,8 +130,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -130,8 +130,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergeExistingPropertyDeprecationOverride ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "singledeprecated" , "name" , null , null , null , null , null ,
new ItemDeprecation ( "Don't use this." , "single.name" ) ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( DeprecatedSingleProperty . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , DeprecatedSingleProperty . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "singledeprecated.name" , String . class . getName ( ) )
. fromSource ( DeprecatedSingleProperty . class ) . withDeprecation ( "Don't use this." , "single.name" ) ) ;
assertThat ( metadata . getItems ( ) ) . hasSize ( 3 ) ;
@ -141,8 +141,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -141,8 +141,8 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
void mergeExistingPropertyDeprecationOverrideLevel ( ) throws Exception {
ItemMetadata property = ItemMetadata . newProperty ( "singledeprecated" , "name" , null , null , null , null , null ,
new ItemDeprecation ( null , null , "error" ) ) ;
write AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( DeprecatedSingleProperty . class ) ;
String additionalMetadata = build AdditionalMetadata( property ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , DeprecatedSingleProperty . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "singledeprecated.name" , String . class . getName ( ) )
. fromSource ( DeprecatedSingleProperty . class )
. withDeprecation ( "renamed" , "singledeprecated.new-name" , "error" ) ) ;
@ -151,17 +151,17 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -151,17 +151,17 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergeOfInvalidAdditionalMetadata ( ) throws IOException {
File additionalMetadataFile = createAdditionalMetadataFile ( ) ;
FileCopyUtils . copy ( "Hello World" , new FileWriter ( additionalMetadataFile ) ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - > compile ( SimpleProperties . class ) )
. withMessage ( "Compilation failed " ) ;
String metadata = "Hello World" ;
assertThatExceptionOfType ( CompilationException . class )
. isThrownBy ( ( ) - > compile ( metadata , SimpleProperties . class ) )
. withMessageContaining ( "Invalid additional meta-data " ) ;
}
@Test
void mergingOfSimpleHint ( ) throws Exception {
writeAdditionalHints ( ItemHint . newHint ( "simple.the-name" , new ItemHint . ValueHint ( "boot" , "Bla bla" ) ,
new ItemHint . ValueHint ( "spring" , null ) ) ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String hints = buildAdditionalHints ( ItemHint . newHint ( "simple.the-name" ,
new ItemHint . ValueHint ( "boot" , "Bla bla" ) , new ItemHint . ValueHint ( " spring" , null ) ) ) ;
ConfigurationMetadata metadata = compile ( hints , 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 ) ) ;
@ -171,8 +171,9 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -171,8 +171,9 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergingOfHintWithNonCanonicalName ( ) throws Exception {
writeAdditionalHints ( ItemHint . newHint ( "simple.theName" , new ItemHint . ValueHint ( "boot" , "Bla bla" ) ) ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String hints = buildAdditionalHints (
ItemHint . newHint ( "simple.theName" , new ItemHint . ValueHint ( "boot" , "Bla bla" ) ) ) ;
ConfigurationMetadata metadata = compile ( hints , 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 ) ) ;
@ -181,10 +182,10 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -181,10 +182,10 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergingOfHintWithProvider ( ) throws Exception {
write AdditionalHints( new ItemHint ( "simple.theName" , Collections . emptyList ( ) ,
String hints = build AdditionalHints( new ItemHint ( "simple.theName" , Collections . emptyList ( ) ,
Arrays . asList ( new ItemHint . ValueProvider ( "first" , Collections . singletonMap ( "target" , "org.foo" ) ) ,
new ItemHint . ValueProvider ( "second" , null ) ) ) ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
ConfigurationMetadata metadata = compile ( hints , 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 ) ) ;
@ -194,58 +195,48 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -194,58 +195,48 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergingOfAdditionalDeprecation ( ) throws Exception {
writePropertyDeprecation ( ItemMetadata . newProperty ( "simple" , "wrongName" , "java.lang.String" , null , null , null ,
null , new ItemDeprecation ( "Lame name." , "simple.the-name" ) ) ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
String deprecations = buildPropertyDeprecations ( ItemMetadata . newProperty ( "simple" , "wrongName" ,
"java.lang.String" , null , null , null , null , new ItemDeprecation ( "Lame name." , "simple.the-name" ) ) ) ;
ConfigurationMetadata metadata = compile ( deprecations , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple.wrong-name" , String . class ) . withDeprecation ( "Lame name." ,
"simple.the-name" ) ) ;
}
@Test
void mergingOfAdditionalMetadata ( ) throws Exception {
File metaInfDirectory = new File ( getCompiler ( ) . getOutputLocation ( ) , "META-INF" ) ;
metaInfDirectory . mkdirs ( ) ;
File additionalMetadataFile = new File ( metaInfDirectory , "additional-spring-configuration-metadata.json" ) ;
additionalMetadataFile . createNewFile ( ) ;
JSONObject property = new JSONObject ( ) ;
property . put ( "name" , "foo" ) ;
property . put ( "type" , "java.lang.String" ) ;
property . put ( "sourceType" , AdditionalMetadata . class . getName ( ) ) ;
JSONArray properties = new JSONArray ( ) ;
properties . put ( property ) ;
JSONObject additionalMetadata = new JSONObject ( ) ;
additionalMetadata . put ( "properties" , properties ) ;
FileWriter writer = new FileWriter ( additionalMetadataFile ) ;
writer . append ( additionalMetadata . toString ( 2 ) ) ;
writer . flush ( ) ;
writer . close ( ) ;
ConfigurationMetadata metadata = compile ( SimpleProperties . class ) ;
JSONObject json = new JSONObject ( ) ;
json . put ( "properties" , properties ) ;
String additionalMetadata = json . toString ( ) ;
ConfigurationMetadata metadata = compile ( additionalMetadata , SimpleProperties . class ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "simple.comparator" ) ) ;
assertThat ( metadata ) . has ( Metadata . withProperty ( "foo" , String . class ) . fromSource ( AdditionalMetadata . class ) ) ;
}
private void write AdditionalMetadata( ItemMetadata . . . metadata ) throws Exception {
private String build AdditionalMetadata( ItemMetadata . . . metadata ) throws Exception {
TestJsonConverter converter = new TestJsonConverter ( ) ;
File additionalMetadataFile = createAdditionalMetadataFile ( ) ;
JSONObject additionalMetadata = new JSONObject ( ) ;
JSONArray properties = new JSONArray ( ) ;
for ( ItemMetadata itemMetadata : metadata ) {
properties . put ( converter . toJsonObject ( itemMetadata ) ) ;
}
additionalMetadata . put ( "properties" , properties ) ;
writeMetadata ( additionalMetadataFile , additionalMetadata ) ;
return additionalMetadata . toString ( ) ;
}
private void write AdditionalHints( ItemHint . . . hints ) throws Exception {
private String build AdditionalHints( ItemHint . . . hints ) throws Exception {
TestJsonConverter converter = new TestJsonConverter ( ) ;
File additionalMetadataFile = createAdditionalMetadataFile ( ) ;
JSONObject additionalMetadata = new JSONObject ( ) ;
additionalMetadata . put ( "hints" , converter . toJsonArray ( Arrays . asList ( hints ) ) ) ;
writeMetadata ( additionalMetadataFile , additionalMetadata ) ;
return additionalMetadata . toString ( ) ;
}
private void writePropertyDeprecation ( ItemMetadata . . . items ) throws Exception {
File additionalMetadataFile = createAdditionalMetadataFile ( ) ;
private String buildPropertyDeprecations ( ItemMetadata . . . items ) throws Exception {
JSONArray propertiesArray = new JSONArray ( ) ;
for ( ItemMetadata item : items ) {
JSONObject jsonObject = new JSONObject ( ) ;
@ -269,21 +260,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@@ -269,21 +260,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
}
JSONObject additionalMetadata = new JSONObject ( ) ;
additionalMetadata . put ( "properties" , propertiesArray ) ;
writeMetadata ( additionalMetadataFile , additionalMetadata ) ;
}
private File createAdditionalMetadataFile ( ) throws IOException {
File metaInfDirectory = new File ( getCompiler ( ) . getOutputLocation ( ) , "META-INF" ) ;
metaInfDirectory . mkdirs ( ) ;
File additionalMetadataFile = new File ( metaInfDirectory , "additional-spring-configuration-metadata.json" ) ;
additionalMetadataFile . createNewFile ( ) ;
return additionalMetadataFile ;
}
private void writeMetadata ( File metadataFile , JSONObject metadata ) throws Exception {
try ( FileWriter writer = new FileWriter ( metadataFile ) ) {
writer . append ( metadata . toString ( 2 ) ) ;
}
return additionalMetadata . toString ( ) ;
}
static class AdditionalMetadata {