@ -42,10 +42,11 @@ import static org.springframework.util.StringUtils.uncapitalize;
@@ -42,10 +42,11 @@ import static org.springframework.util.StringUtils.uncapitalize;
* @author Chris Beams
* @author Juergen Hoeller
* /
public class AnnotationConfigApplicationContextTests {
@SuppressWarnings ( "resource" )
class AnnotationConfigApplicationContextTests {
@Test
public void scanAndRefresh ( ) {
void scanAndRefresh ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . scan ( "org.springframework.context.annotation6" ) ;
context . refresh ( ) ;
@ -55,11 +56,11 @@ public class AnnotationConfigApplicationContextTests {
@@ -55,11 +56,11 @@ public class AnnotationConfigApplicationContextTests {
context . getBean ( uncapitalize ( ComponentForScanning . class . getSimpleName ( ) ) ) ;
context . getBean ( uncapitalize ( Jsr330NamedForScanning . class . getSimpleName ( ) ) ) ;
Map < String , Object > beans = context . getBeansWithAnnotation ( Configuration . class ) ;
assertThat ( beans . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( beans ) . size ( ) . isEqualTo ( 1 ) ;
}
@Test
public void registerAndRefresh ( ) {
void registerAndRefresh ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . register ( Config . class , NameConfig . class ) ;
context . refresh ( ) ;
@ -67,11 +68,11 @@ public class AnnotationConfigApplicationContextTests {
@@ -67,11 +68,11 @@ public class AnnotationConfigApplicationContextTests {
context . getBean ( "testBean" ) ;
context . getBean ( "name" ) ;
Map < String , Object > beans = context . getBeansWithAnnotation ( Configuration . class ) ;
assertThat ( beans . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( beans ) . size ( ) . isEqualTo ( 2 ) ;
}
@Test
public void getBeansWithAnnotation ( ) {
void getBeansWithAnnotation ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . register ( Config . class , NameConfig . class , UntypedFactoryBean . class ) ;
context . refresh ( ) ;
@ -79,11 +80,11 @@ public class AnnotationConfigApplicationContextTests {
@@ -79,11 +80,11 @@ public class AnnotationConfigApplicationContextTests {
context . getBean ( "testBean" ) ;
context . getBean ( "name" ) ;
Map < String , Object > beans = context . getBeansWithAnnotation ( Configuration . class ) ;
assertThat ( beans . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( beans ) . size ( ) . isEqualTo ( 2 ) ;
}
@Test
public void getBeanByType ( ) {
void getBeanByType ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( Config . class ) ;
TestBean testBean = context . getBean ( TestBean . class ) ;
assertThat ( testBean ) . isNotNull ( ) ;
@ -91,7 +92,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -91,7 +92,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void getBeanByTypeRaisesNoSuchBeanDefinitionException ( ) {
void getBeanByTypeRaisesNoSuchBeanDefinitionException ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( Config . class ) ;
// attempt to retrieve a bean that does not exist
@ -102,7 +103,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -102,7 +103,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void getBeanByTypeAmbiguityRaisesException ( ) {
void getBeanByTypeAmbiguityRaisesException ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( TwoTestBeanConfig . class ) ;
assertThatExceptionOfType ( NoSuchBeanDefinitionException . class ) . isThrownBy ( ( ) - >
context . getBean ( TestBean . class ) )
@ -116,7 +117,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -116,7 +117,7 @@ public class AnnotationConfigApplicationContextTests {
* @see org . springframework . beans . factory . support . DefaultBeanNameGenerator # generateBeanName
* /
@Test
public void defaultConfigClassBeanNameIsGeneratedProperly ( ) {
void defaultConfigClassBeanNameIsGeneratedProperly ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( Config . class ) ;
// attempt to retrieve the instance by its generated bean name
@ -129,7 +130,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -129,7 +130,7 @@ public class AnnotationConfigApplicationContextTests {
* the configuration class with bean name ' foo ' .
* /
@Test
public void explicitConfigClassBeanNameIsRespected ( ) {
void explicitConfigClassBeanNameIsRespected ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( ConfigWithCustomName . class ) ;
// attempt to retrieve the instance by its specified name
@ -138,13 +139,13 @@ public class AnnotationConfigApplicationContextTests {
@@ -138,13 +139,13 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void autowiringIsEnabledByDefault ( ) {
void autowiringIsEnabledByDefault ( ) {
ApplicationContext context = new AnnotationConfigApplicationContext ( AutowiredConfig . class ) ;
assertThat ( context . getBean ( TestBean . class ) . name ) . isEqualTo ( "foo" ) ;
}
@Test
public void nullReturningBeanPostProcessor ( ) {
void nullReturningBeanPostProcessor ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . register ( AutowiredConfig . class ) ;
context . getBeanFactory ( ) . addBeanPostProcessor ( new BeanPostProcessor ( ) {
@ -152,10 +153,6 @@ public class AnnotationConfigApplicationContextTests {
@@ -152,10 +153,6 @@ public class AnnotationConfigApplicationContextTests {
public Object postProcessBeforeInitialization ( Object bean , String beanName ) {
return ( bean instanceof TestBean ? null : bean ) ;
}
@Override
public Object postProcessAfterInitialization ( Object bean , String beanName ) {
return bean ;
}
} ) ;
context . getBeanFactory ( ) . addBeanPostProcessor ( new BeanPostProcessor ( ) {
@Override
@ -173,7 +170,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -173,7 +170,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualBeans ( ) {
void individualBeans ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . register ( BeanA . class , BeanB . class , BeanC . class ) ;
context . refresh ( ) ;
@ -184,7 +181,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -184,7 +181,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualNamedBeans ( ) {
void individualNamedBeans ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( "a" , BeanA . class ) ;
context . registerBean ( "b" , BeanB . class ) ;
@ -197,7 +194,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -197,7 +194,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualBeanWithSupplier ( ) {
void individualBeanWithSupplier ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( BeanA . class ,
( ) - > new BeanA ( context . getBean ( BeanB . class ) , context . getBean ( BeanC . class ) ) ) ;
@ -210,12 +207,14 @@ public class AnnotationConfigApplicationContextTests {
@@ -210,12 +207,14 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getBean ( BeanA . class ) . c ) . isSameAs ( context . getBean ( BeanC . class ) ) ;
assertThat ( context . getBean ( BeanB . class ) . applicationContext ) . isSameAs ( context ) ;
assertThat ( context . getDefaultListableBeanFactory ( ) . getDependentBeans ( "annotationConfigApplicationContextTests.BeanB" ) ) . isEqualTo ( new String [ ] { "annotationConfigApplicationContextTests.BeanA" } ) ;
assertThat ( context . getDefaultListableBeanFactory ( ) . getDependentBeans ( "annotationConfigApplicationContextTests.BeanC" ) ) . isEqualTo ( new String [ ] { "annotationConfigApplicationContextTests.BeanA" } ) ;
assertThat ( context . getDefaultListableBeanFactory ( ) . getDependentBeans ( "annotationConfigApplicationContextTests.BeanB" ) )
. containsExactly ( "annotationConfigApplicationContextTests.BeanA" ) ;
assertThat ( context . getDefaultListableBeanFactory ( ) . getDependentBeans ( "annotationConfigApplicationContextTests.BeanC" ) )
. containsExactly ( "annotationConfigApplicationContextTests.BeanA" ) ;
}
@Test
public void individualBeanWithSupplierAndCustomizer ( ) {
void individualBeanWithSupplierAndCustomizer ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( BeanA . class ,
( ) - > new BeanA ( context . getBean ( BeanB . class ) , context . getBean ( BeanC . class ) ) ,
@ -231,7 +230,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -231,7 +230,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualNamedBeanWithSupplier ( ) {
void individualNamedBeanWithSupplier ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( "a" , BeanA . class ,
( ) - > new BeanA ( context . getBean ( BeanB . class ) , context . getBean ( BeanC . class ) ) ) ;
@ -246,7 +245,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -246,7 +245,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualNamedBeanWithSupplierAndCustomizer ( ) {
void individualNamedBeanWithSupplierAndCustomizer ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( "a" , BeanA . class ,
( ) - > new BeanA ( context . getBean ( BeanB . class ) , context . getBean ( BeanC . class ) ) ,
@ -262,7 +261,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -262,7 +261,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualBeanWithNullReturningSupplier ( ) {
void individualBeanWithNullReturningSupplier ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( "a" , BeanA . class , ( ) - > null ) ;
context . registerBean ( "b" , BeanB . class , BeanB : : new ) ;
@ -272,13 +271,13 @@ public class AnnotationConfigApplicationContextTests {
@@ -272,13 +271,13 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( ObjectUtils . containsElement ( context . getBeanNamesForType ( BeanA . class ) , "a" ) ) . isTrue ( ) ;
assertThat ( ObjectUtils . containsElement ( context . getBeanNamesForType ( BeanB . class ) , "b" ) ) . isTrue ( ) ;
assertThat ( ObjectUtils . containsElement ( context . getBeanNamesForType ( BeanC . class ) , "c" ) ) . isTrue ( ) ;
assertThat ( context . getBeansOfType ( BeanA . class ) . isEmpty ( ) ) . isTrue ( ) ;
assertThat ( context . getBeansOfType ( BeanA . class ) ) . isEmpty ( ) ;
assertThat ( context . getBeansOfType ( BeanB . class ) . values ( ) . iterator ( ) . next ( ) ) . isSameAs ( context . getBean ( BeanB . class ) ) ;
assertThat ( context . getBeansOfType ( BeanC . class ) . values ( ) . iterator ( ) . next ( ) ) . isSameAs ( context . getBean ( BeanC . class ) ) ;
}
@Test
public void individualBeanWithSpecifiedConstructorArguments ( ) {
void individualBeanWithSpecifiedConstructorArguments ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
BeanB b = new BeanB ( ) ;
BeanC c = new BeanC ( ) ;
@ -287,11 +286,11 @@ public class AnnotationConfigApplicationContextTests {
@@ -287,11 +286,11 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getBean ( BeanA . class ) . b ) . isSameAs ( b ) ;
assertThat ( context . getBean ( BeanA . class ) . c ) . isSameAs ( c ) ;
assertThat ( ( Object ) b . applicationContext ) . isNull ( ) ;
assertThat ( b . applicationContext ) . isNull ( ) ;
}
@Test
public void individualNamedBeanWithSpecifiedConstructorArguments ( ) {
void individualNamedBeanWithSpecifiedConstructorArguments ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
BeanB b = new BeanB ( ) ;
BeanC c = new BeanC ( ) ;
@ -300,11 +299,11 @@ public class AnnotationConfigApplicationContextTests {
@@ -300,11 +299,11 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getBean ( "a" , BeanA . class ) . b ) . isSameAs ( b ) ;
assertThat ( context . getBean ( "a" , BeanA . class ) . c ) . isSameAs ( c ) ;
assertThat ( ( Object ) b . applicationContext ) . isNull ( ) ;
assertThat ( b . applicationContext ) . isNull ( ) ;
}
@Test
public void individualBeanWithMixedConstructorArguments ( ) {
void individualBeanWithMixedConstructorArguments ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
BeanC c = new BeanC ( ) ;
context . registerBean ( BeanA . class , c ) ;
@ -317,7 +316,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -317,7 +316,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualNamedBeanWithMixedConstructorArguments ( ) {
void individualNamedBeanWithMixedConstructorArguments ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
BeanC c = new BeanC ( ) ;
context . registerBean ( "a" , BeanA . class , c ) ;
@ -330,7 +329,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -330,7 +329,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualBeanWithFactoryBeanSupplier ( ) {
void individualBeanWithFactoryBeanSupplier ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . registerBean ( "fb" , NonInstantiatedFactoryBean . class , NonInstantiatedFactoryBean : : new , bd - > bd . setLazyInit ( true ) ) ;
context . refresh ( ) ;
@ -340,7 +339,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -340,7 +339,7 @@ public class AnnotationConfigApplicationContextTests {
}
@Test
public void individualBeanWithFactoryBeanSupplierAndTargetType ( ) {
void individualBeanWithFactoryBeanSupplierAndTargetType ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
RootBeanDefinition bd = new RootBeanDefinition ( ) ;
bd . setInstanceSupplier ( NonInstantiatedFactoryBean : : new ) ;
@ -351,12 +350,12 @@ public class AnnotationConfigApplicationContextTests {
@@ -351,12 +350,12 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getType ( "fb" ) ) . isEqualTo ( String . class ) ;
assertThat ( context . getType ( "&fb" ) ) . isEqualTo ( FactoryBean . class ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) . length ) . isEqualTo ( 1 ) ;
assertThat ( context . getBeanNamesForType ( NonInstantiatedFactoryBean . class ) . length ) . isEqualTo ( 0 ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) ) . hasSize ( 1 ) ;
assertThat ( context . getBeanNamesForType ( NonInstantiatedFactoryBean . class ) ) . isEmpty ( ) ;
}
@Test
public void individualBeanWithFactoryBeanObjectTypeAsTargetType ( ) {
void individualBeanWithFactoryBeanObjectTypeAsTargetType ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
RootBeanDefinition bd = new RootBeanDefinition ( ) ;
bd . setBeanClass ( TypedFactoryBean . class ) ;
@ -366,12 +365,12 @@ public class AnnotationConfigApplicationContextTests {
@@ -366,12 +365,12 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getType ( "&fb" ) ) . isEqualTo ( TypedFactoryBean . class ) ;
assertThat ( context . getType ( "fb" ) ) . isEqualTo ( String . class ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) . length ) . isEqualTo ( 1 ) ;
assertThat ( context . getBeanNamesForType ( TypedFactoryBean . class ) . length ) . isEqualTo ( 1 ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) ) . hasSize ( 1 ) ;
assertThat ( context . getBeanNamesForType ( TypedFactoryBean . class ) ) . hasSize ( 1 ) ;
}
@Test
public void individualBeanWithFactoryBeanObjectTypeAsTargetTypeAndLazy ( ) {
void individualBeanWithFactoryBeanObjectTypeAsTargetTypeAndLazy ( ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
RootBeanDefinition bd = new RootBeanDefinition ( ) ;
bd . setBeanClass ( TypedFactoryBean . class ) ;
@ -385,8 +384,8 @@ public class AnnotationConfigApplicationContextTests {
@@ -385,8 +384,8 @@ public class AnnotationConfigApplicationContextTests {
assertThat ( context . getBean ( "&fb" ) ) . isInstanceOf ( FactoryBean . class ) ;
assertThat ( context . getType ( "&fb" ) ) . isEqualTo ( TypedFactoryBean . class ) ;
assertThat ( context . getType ( "fb" ) ) . isEqualTo ( String . class ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) . length ) . isEqualTo ( 1 ) ;
assertThat ( context . getBeanNamesForType ( TypedFactoryBean . class ) . length ) . isEqualTo ( 1 ) ;
assertThat ( context . getBeanNamesForType ( FactoryBean . class ) ) . hasSize ( 1 ) ;
assertThat ( context . getBeanNamesForType ( TypedFactoryBean . class ) ) . hasSize ( 1 ) ;
}
@ -394,7 +393,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -394,7 +393,7 @@ public class AnnotationConfigApplicationContextTests {
static class Config {
@Bean
public TestBean testBean ( ) {
TestBean testBean ( ) {
TestBean testBean = new TestBean ( ) ;
testBean . name = "foo" ;
return testBean ;
@ -405,7 +404,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -405,7 +404,7 @@ public class AnnotationConfigApplicationContextTests {
static class ConfigWithCustomName {
@Bean
public TestBean testBean ( ) {
TestBean testBean ( ) {
return new TestBean ( ) ;
}
}
@ -446,7 +445,9 @@ public class AnnotationConfigApplicationContextTests {
@@ -446,7 +445,9 @@ public class AnnotationConfigApplicationContextTests {
BeanB b ;
BeanC c ;
@Autowired public BeanA ( BeanB b , BeanC c ) {
@Autowired
BeanA ( BeanB b , BeanC c ) {
this . b = b ;
this . c = c ;
}
@ -464,7 +465,7 @@ public class AnnotationConfigApplicationContextTests {
@@ -464,7 +465,7 @@ public class AnnotationConfigApplicationContextTests {
static class NonInstantiatedFactoryBean implements FactoryBean < String > {
public NonInstantiatedFactoryBean ( ) {
NonInstantiatedFactoryBean ( ) {
throw new IllegalStateException ( ) ;
}