@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2014 the original author or authors .
* Copyright 2002 - 2015 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 .
@ -17,12 +17,9 @@
@@ -17,12 +17,9 @@
package org.springframework.context.annotation ;
import java.io.IOException ;
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.lang.annotation.Target ;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition ;
@ -33,137 +30,130 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
@@ -33,137 +30,130 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory ;
import static org.junit.Assert.* ;
import static org.springframework.context.annotation.ScopedProxyMode.* ;
/ * *
* Unit tests for { @link AnnotationScopeMetadataResolver } .
*
* @author Rick Evans
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
* /
public final class AnnotationScopeMetadataResolverTests {
private AnnotationScopeMetadataResolver scopeMetadataResolver ;
@Before
public void setUp ( ) throws Exception {
this . scopeMetadataResolver = new AnnotationScopeMetadataResolver ( ) ;
}
public class AnnotationScopeMetadataResolverTests {
private AnnotationScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver ( ) ;
@Test
public void testThatResolveScopeMetadataDoesNotApplyScopedProxyModeToA Singleton( ) {
public void resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton ( ) {
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( AnnotatedWithSingletonScope . class ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( BeanDefinition . SCOPE_SINGLETON , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . NO , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( NO , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testThatResolveScopeMetadataDoesApplyScopedProxyModeToA Prototype( ) {
this . scopeMetadataResolver = new AnnotationScopeMetadataResolver ( ScopedProxyMode . INTERFACES ) ;
public void resolveScopeMetadataShouldApplyScopedProxyModeTo Prototype( ) {
this . scopeMetadataResolver = new AnnotationScopeMetadataResolver ( INTERFACES ) ;
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( AnnotatedWithPrototypeScope . class ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( BeanDefinition . SCOPE_PROTOTYPE , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . INTERFACES , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( INTERFACES , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testThatResolveScopeMetadataDoesReadScopedProxyModeFromThe Annotation( ) {
public void resolveScopeMetadataShouldReadScopedProxyModeFrom Annotation( ) {
this . scopeMetadataResolver = new AnnotationScopeMetadataResolver ( ) ;
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( AnnotatedWithScopedProxy . class ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( "request" , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testC ustomRequestScope( ) {
public void c ustomRequestScope( ) {
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( AnnotatedWithCustomRequestScope . class ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( "request" , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . NO , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( NO , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testC ustomRequestScopeViaAsm( ) throws IOException {
public void c ustomRequestScopeViaAsm( ) throws IOException {
MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory ( ) ;
MetadataReader reader = readerFactory . getMetadataReader ( AnnotatedWithCustomRequestScope . class . getName ( ) ) ;
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( reader . getAnnotationMetadata ( ) ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( "request" , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . NO , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( NO , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testCustomRequestScopeWithAttribute ( ) {
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( AnnotatedWithCustomRequestScopeWithAttribute . class ) ;
public void customRequestScopeWithAttribute ( ) {
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition (
AnnotatedWithCustomRequestScopeWithAttributeOverride . class ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( "request" , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test
public void testC ustomRequestScopeWithAttributeViaAsm( ) throws IOException {
public void c ustomRequestScopeWithAttributeViaAsm( ) throws IOException {
MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory ( ) ;
MetadataReader reader = readerFactory . getMetadataReader ( AnnotatedWithCustomRequestScopeWithAttribute . class . getName ( ) ) ;
MetadataReader reader = readerFactory . getMetadataReader ( AnnotatedWithCustomRequestScopeWithAttributeOverride . class . getName ( ) ) ;
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition ( reader . getAnnotationMetadata ( ) ) ;
ScopeMetadata scopeMetadata = this . scopeMetadataResolver . resolveScopeMetadata ( bd ) ;
assertNotNull ( "resolveScopeMetadata(..) must *never* return null." , scopeMetadata ) ;
assertEquals ( "request" , scopeMetadata . getScopeName ( ) ) ;
assertEquals ( ScopedProxyMode . TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
assertEquals ( TARGET_CLASS , scopeMetadata . getScopedProxyMode ( ) ) ;
}
@Test ( expected = IllegalArgumentException . class )
public void testC torWithNullScopedProxyMode( ) {
@Test ( expected = IllegalArgumentException . class )
public void c torWithNullScopedProxyMode( ) {
new AnnotationScopeMetadataResolver ( null ) ;
}
@Test ( expected = IllegalArgumentException . class )
public void te stS etScopeAnnotationTypeWithNullType( ) {
@Test ( expected = IllegalArgumentException . class )
public void setScopeAnnotationTypeWithNullType ( ) {
scopeMetadataResolver . setScopeAnnotationType ( null ) ;
}
@Scope ( "singleton" )
private static final class AnnotatedWithSingletonScope {
}
@Scope ( "prototype" )
private static final class AnnotatedWithPrototypeScope {
}
@Scope ( value = "request" , proxyMode = ScopedProxyMode . TARGET_CLASS )
private static final class AnnotatedWithScopedProxy {
@Retention ( RetentionPolicy . RUNTIME )
@Scope ( "request" )
@interface CustomRequestScope {
}
@Target ( { ElementType . TYPE , ElementType . METHOD } )
@Retention ( RetentionPolicy . RUNTIME )
@Scope ( "request" )
public @interface CustomRequestScope {
@interface CustomRequestScopeWithAttributeOverride {
ScopedProxyMode proxyMode ( ) ;
}
@CustomRequestScope
private static final class AnnotatedWithCustomRequest Scope {
@Scope ( "singleton" )
private static class AnnotatedWithSingleton Scope {
}
@Scope ( "prototype" )
private static class AnnotatedWithPrototypeScope {
}
@Target ( { ElementType . TYPE , ElementType . METHOD } )
@Retention ( RetentionPolicy . RUNTIME )
@Scope ( "request" )
public @interface CustomRequestScopeWithAttribute {
@Scope ( name = "request" , proxyMode = TARGET_CLASS )
private static class AnnotatedWithScopedProxy {
}
ScopedProxyMode proxyMode ( ) ;
@CustomRequestScope
private static class AnnotatedWithCustomRequestScope {
}
@CustomRequestScopeWithAttribute ( proxyMode = ScopedProxyMode . TARGET_CLASS )
private static final class AnnotatedWithCustomRequestScopeWithAttribute {
@CustomRequestScopeWithAttributeOverride ( proxyMode = TARGET_CLASS )
private static class AnnotatedWithCustomRequestScopeWithAttributeOverrid e {
}
}