@ -18,6 +18,15 @@ package org.springframework.aop.scope;
@@ -18,6 +18,15 @@ package org.springframework.aop.scope;
import org.junit.jupiter.api.Test ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.config.BeanDefinitionHolder ;
import org.springframework.beans.factory.support.AbstractBeanDefinition ;
import org.springframework.beans.factory.support.AutowireCandidateQualifier ;
import org.springframework.beans.factory.support.BeanDefinitionRegistry ;
import org.springframework.beans.factory.support.GenericBeanDefinition ;
import org.springframework.beans.factory.support.RootBeanDefinition ;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException ;
@ -25,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
@@ -25,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for { @link ScopedProxyUtils } .
*
* @author Sam Brannen
* @author Juergen Hoeller
* @since 5 . 1 . 10
* /
class ScopedProxyUtilsTests {
@ -53,15 +63,79 @@ class ScopedProxyUtilsTests {
@@ -53,15 +63,79 @@ class ScopedProxyUtilsTests {
@Test
void getOriginalBeanNameForNullTargetBean ( ) {
assertThatIllegalArgumentException ( )
. isThrownBy ( ( ) - > ScopedProxyUtils . getOriginalBeanName ( null ) )
. withMessage ( "bean name 'null' does not refer to the target of a scoped proxy" ) ;
. isThrownBy ( ( ) - > ScopedProxyUtils . getOriginalBeanName ( null ) )
. withMessage ( "bean name 'null' does not refer to the target of a scoped proxy" ) ;
}
@Test
void getOriginalBeanNameForNonScopedTarget ( ) {
assertThatIllegalArgumentException ( )
. isThrownBy ( ( ) - > ScopedProxyUtils . getOriginalBeanName ( "myBean" ) )
. withMessage ( "bean name 'myBean' does not refer to the target of a scoped proxy" ) ;
. isThrownBy ( ( ) - > ScopedProxyUtils . getOriginalBeanName ( "myBean" ) )
. withMessage ( "bean name 'myBean' does not refer to the target of a scoped proxy" ) ;
}
@Test
void createScopedProxyTargetAppliesAutowireSettingsToProxyBeanDefinition ( ) {
AbstractBeanDefinition targetDefinition = new GenericBeanDefinition ( ) ;
// Opposite of defaults
targetDefinition . setAutowireCandidate ( false ) ;
targetDefinition . setDefaultCandidate ( false ) ;
targetDefinition . setPrimary ( true ) ;
targetDefinition . setFallback ( true ) ;
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry ( ) ;
BeanDefinitionHolder proxyHolder = ScopedProxyUtils . createScopedProxy (
new BeanDefinitionHolder ( targetDefinition , "myBean" ) , registry , false ) ;
AbstractBeanDefinition proxyBeanDefinition = ( AbstractBeanDefinition ) proxyHolder . getBeanDefinition ( ) ;
assertThat ( proxyBeanDefinition . isAutowireCandidate ( ) ) . isFalse ( ) ;
assertThat ( proxyBeanDefinition . isDefaultCandidate ( ) ) . isFalse ( ) ;
assertThat ( proxyBeanDefinition . isPrimary ( ) ) . isTrue ( ) ;
assertThat ( proxyBeanDefinition . isFallback ( ) ) . isTrue ( ) ;
}
@Test
void createScopedProxyTargetAppliesBeanAttributesToProxyBeanDefinition ( ) {
GenericBeanDefinition targetDefinition = new GenericBeanDefinition ( ) ;
// Opposite of defaults
targetDefinition . setRole ( BeanDefinition . ROLE_INFRASTRUCTURE ) ;
targetDefinition . setSource ( "theSource" ) ;
targetDefinition . addQualifier ( new AutowireCandidateQualifier ( "myQualifier" ) ) ;
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry ( ) ;
BeanDefinitionHolder proxyHolder = ScopedProxyUtils . createScopedProxy (
new BeanDefinitionHolder ( targetDefinition , "myBean" ) , registry , false ) ;
BeanDefinition proxyBeanDefinition = proxyHolder . getBeanDefinition ( ) ;
assertThat ( proxyBeanDefinition . getRole ( ) ) . isEqualTo ( BeanDefinition . ROLE_INFRASTRUCTURE ) ;
assertThat ( proxyBeanDefinition ) . isInstanceOf ( RootBeanDefinition . class ) ;
assertThat ( proxyBeanDefinition . getPropertyValues ( ) ) . hasSize ( 2 ) ;
assertThat ( proxyBeanDefinition . getPropertyValues ( ) . get ( "proxyTargetClass" ) ) . isEqualTo ( false ) ;
assertThat ( proxyBeanDefinition . getPropertyValues ( ) . get ( "targetBeanName" ) ) . isEqualTo (
ScopedProxyUtils . getTargetBeanName ( "myBean" ) ) ;
RootBeanDefinition rootBeanDefinition = ( RootBeanDefinition ) proxyBeanDefinition ;
assertThat ( rootBeanDefinition . getQualifiers ( ) ) . hasSize ( 1 ) ;
assertThat ( rootBeanDefinition . hasQualifier ( "myQualifier" ) ) . isTrue ( ) ;
assertThat ( rootBeanDefinition . getSource ( ) ) . isEqualTo ( "theSource" ) ;
}
@Test
void createScopedProxyTargetCleansAutowireSettingsInTargetDefinition ( ) {
AbstractBeanDefinition targetDefinition = new GenericBeanDefinition ( ) ;
targetDefinition . setAutowireCandidate ( true ) ;
targetDefinition . setDefaultCandidate ( true ) ;
targetDefinition . setPrimary ( true ) ;
targetDefinition . setFallback ( true ) ;
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry ( ) ;
ScopedProxyUtils . createScopedProxy (
new BeanDefinitionHolder ( targetDefinition , "myBean" ) , registry , false ) ;
assertThat ( targetDefinition . isAutowireCandidate ( ) ) . isFalse ( ) ;
assertThat ( targetDefinition . isDefaultCandidate ( ) ) . isFalse ( ) ;
assertThat ( targetDefinition . isPrimary ( ) ) . isFalse ( ) ;
assertThat ( targetDefinition . isFallback ( ) ) . isFalse ( ) ;
}
}