@ -22,11 +22,14 @@ import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.support.DefaultListableBeanFactory ;
import org.springframework.beans.factory.support.DefaultListableBeanFactory ;
import org.springframework.beans.factory.support.GenericBeanDefinition ;
import org.springframework.beans.factory.support.GenericBeanDefinition ;
import org.springframework.boot.context.properties.scan.ConfigurationPropertiesScanConfiguration ;
import org.springframework.boot.context.properties.scan.invalid.c.InvalidConfiguration ;
import org.springframework.boot.context.properties.scan.invalid.d.OtherInvalidConfiguration ;
import org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration ;
import org.springframework.core.type.AnnotationMetadata ;
import org.springframework.core.type.AnnotationMetadata ;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory ;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
/ * *
/ * *
* Tests for { @link ConfigurationPropertiesScanRegistrar } .
* Tests for { @link ConfigurationPropertiesScanRegistrar } .
@ -46,11 +49,11 @@ public class ConfigurationPropertiesScanRegistrarTests {
getAnnotationMetadata ( ConfigurationPropertiesScanConfiguration . class ) ,
getAnnotationMetadata ( ConfigurationPropertiesScanConfiguration . class ) ,
this . beanFactory ) ;
this . beanFactory ) ;
BeanDefinition bingDefinition = this . beanFactory . getBeanDefinition (
BeanDefinition bingDefinition = this . beanFactory . getBeanDefinition (
"bing-org.springframework.boot.context.properties.scan. ConfigurationPropertiesScanConfiguration$BingProperties") ;
"bing-org.springframework.boot.context.properties.scan. valid. ConfigurationPropertiesScanConfiguration$BingProperties") ;
BeanDefinition fooDefinition = this . beanFactory . getBeanDefinition (
BeanDefinition fooDefinition = this . beanFactory . getBeanDefinition (
"foo-org.springframework.boot.context.properties.scan. ConfigurationPropertiesScanConfiguration$FooProperties") ;
"foo-org.springframework.boot.context.properties.scan. valid. ConfigurationPropertiesScanConfiguration$FooProperties") ;
BeanDefinition barDefinition = this . beanFactory . getBeanDefinition (
BeanDefinition barDefinition = this . beanFactory . getBeanDefinition (
"bar-org.springframework.boot.context.properties.scan. ConfigurationPropertiesScanConfiguration$BarProperties") ;
"bar-org.springframework.boot.context.properties.scan. valid. ConfigurationPropertiesScanConfiguration$BarProperties") ;
assertThat ( bingDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( bingDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( fooDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( fooDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( barDefinition )
assertThat ( barDefinition )
@ -66,7 +69,7 @@ public class ConfigurationPropertiesScanRegistrarTests {
ConfigurationPropertiesScanConfiguration . TestConfiguration . class ) ,
ConfigurationPropertiesScanConfiguration . TestConfiguration . class ) ,
beanFactory ) ;
beanFactory ) ;
BeanDefinition fooDefinition = beanFactory . getBeanDefinition (
BeanDefinition fooDefinition = beanFactory . getBeanDefinition (
"foo-org.springframework.boot.context.properties.scan. ConfigurationPropertiesScanConfiguration$FooProperties") ;
"foo-org.springframework.boot.context.properties.scan. valid. ConfigurationPropertiesScanConfiguration$FooProperties") ;
assertThat ( fooDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( fooDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
}
}
@ -79,19 +82,53 @@ public class ConfigurationPropertiesScanRegistrarTests {
ConfigurationPropertiesScanConfiguration . DifferentPackageConfiguration . class ) ,
ConfigurationPropertiesScanConfiguration . DifferentPackageConfiguration . class ) ,
beanFactory ) ;
beanFactory ) ;
assertThat ( beanFactory . containsBeanDefinition (
assertThat ( beanFactory . containsBeanDefinition (
"foo-org.springframework.boot.context.properties.scan. ConfigurationPropertiesScanConfiguration$FooProperties") )
"foo-org.springframework.boot.context.properties.scan. valid. ConfigurationPropertiesScanConfiguration$FooProperties") )
. isFalse ( ) ;
. isFalse ( ) ;
BeanDefinition aDefinition = beanFactory . getBeanDefinition (
BeanDefinition aDefinition = beanFactory . getBeanDefinition (
"a-org.springframework.boot.context.properties.scan. a.AScanConfiguration$AProperties") ;
"a-org.springframework.boot.context.properties.scan. valid. a.AScanConfiguration$AProperties") ;
BeanDefinition bDefinition = beanFactory . getBeanDefinition (
BeanDefinition bDefinition = beanFactory . getBeanDefinition (
"b-org.springframework.boot.context.properties.scan. b.BScanConfiguration$BProperties") ;
"b-org.springframework.boot.context.properties.scan. valid. b.BScanConfiguration$BProperties") ;
assertThat ( aDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( aDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( bDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
assertThat ( bDefinition ) . isExactlyInstanceOf ( GenericBeanDefinition . class ) ;
}
}
@Test
public void scanWhenComponentAnnotationPresentShouldThrowException ( ) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory ( ) ;
beanFactory . setAllowBeanDefinitionOverriding ( false ) ;
assertThatExceptionOfType ( InvalidConfigurationPropertiesException . class )
. isThrownBy ( ( ) - > this . registrar . registerBeanDefinitions (
getAnnotationMetadata ( InvalidScanConfiguration . class ) ,
beanFactory ) )
. withMessageContaining (
"Found @Component and @ConfigurationProperties on org.springframework.boot.context.properties.scan.invalid.c.InvalidConfiguration$MyProperties." ) ;
}
@Test
public void scanWhenOtherComponentAnnotationPresentShouldThrowException ( ) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory ( ) ;
beanFactory . setAllowBeanDefinitionOverriding ( false ) ;
assertThatExceptionOfType ( InvalidConfigurationPropertiesException . class )
. isThrownBy ( ( ) - > this . registrar . registerBeanDefinitions (
getAnnotationMetadata ( OtherInvalidScanConfiguration . class ) ,
beanFactory ) )
. withMessageContaining (
"Found @RestController and @ConfigurationProperties on org.springframework.boot.context.properties.scan.invalid.d.OtherInvalidConfiguration$MyControllerProperties." ) ;
}
private AnnotationMetadata getAnnotationMetadata ( Class < ? > source ) throws IOException {
private AnnotationMetadata getAnnotationMetadata ( Class < ? > source ) throws IOException {
return new SimpleMetadataReaderFactory ( ) . getMetadataReader ( source . getName ( ) )
return new SimpleMetadataReaderFactory ( ) . getMetadataReader ( source . getName ( ) )
. getAnnotationMetadata ( ) ;
. getAnnotationMetadata ( ) ;
}
}
@ConfigurationPropertiesScan ( basePackageClasses = InvalidConfiguration . class )
static class InvalidScanConfiguration {
}
@ConfigurationPropertiesScan ( basePackageClasses = OtherInvalidConfiguration . class )
static class OtherInvalidScanConfiguration {
}
}
}