Author: mbenson Date: Thu Sep 16 17:11:15 2010 New Revision: 997852 URL: http://svn.apache.org/viewvc?rev=997852&view=rev Log: since AnnotationFactory now uses a single StubFactory instance internally, the annotationType() handling can be handled therein. Having taken away AnnotationStubConfigurer's only reason for being, it is deleted.
Removed: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationStubConfigurer.java Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractAnnotationFactoryTest.java Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java?rev=997852&r1=997851&r2=997852&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java Thu Sep 16 17:11:15 2010 @@ -26,9 +26,9 @@ import org.apache.commons.proxy2.ProxyUt /** * {...@link AnnotationFactory} provides a simplified API over {...@link StubProxyFactory} - * to stub a Java {...@link Annotation}. Non-stubbed methods will return the values - * that would have been returned from a "real" annotation whose methods' values - * were unspecified. + * to stub a Java {...@link Annotation}. Non-stubbed methods including + * {...@link Annotation#annotationType()} will return the values that would have been + * returned from a "real" annotation whose methods' values were unspecified. * * @author Matt Benson */ @@ -49,7 +49,8 @@ public class AnnotationFactory { private static final ThreadLocal<Object> CONFIGURER = new ThreadLocal<Object>(); - private static final AnnotationStubConfigurer<Annotation> SHARED_CONFIGURER = new AnnotationStubConfigurer<Annotation>() { + private static final StubConfigurer<Annotation> SHARED_CONFIGURER = new StubConfigurer<Annotation>() { + /** * {...@inheritdoc} */ @@ -62,11 +63,12 @@ public class AnnotationFactory { * {...@inheritdoc} */ @Override - protected void configureAnnotation(Annotation stub) { + protected void configure(Annotation stub) { + when(stub.annotationType()).thenReturn(getStubType()); Object o = CONFIGURER.get(); - if (o instanceof AnnotationStubConfigurer<?>) { + if (o instanceof StubConfigurer<?>) { @SuppressWarnings("unchecked") - final AnnotationStubConfigurer<Annotation> configurer = (AnnotationStubConfigurer<Annotation>) o; + final StubConfigurer<Annotation> configurer = (StubConfigurer<Annotation>) o; configurer.configure(requireStubInterceptor(), stub); } } @@ -98,13 +100,13 @@ public class AnnotationFactory { * @return stubbed annotation proxy */ public <A extends Annotation> A create( - AnnotationStubConfigurer<A> configurer) { + StubConfigurer<A> configurer) { @SuppressWarnings("unchecked") final A result = (A) createInternal(Thread.currentThread() - .getContextClassLoader(), configurer); + .getContextClassLoader(), configurer); return result; } - + /** * Create an annotation of the type supported by <code>configurer</code> in the specified classpath. * @param <A> @@ -113,12 +115,12 @@ public class AnnotationFactory { * @return stubbed annotation proxy */ public <A extends Annotation> A create(ClassLoader classLoader, - AnnotationStubConfigurer<A> configurer) { + StubConfigurer<A> configurer) { @SuppressWarnings("unchecked") final A result = (A) createInternal(classLoader, configurer); return result; } - + /** * Create an annotation of <code>annotationType</code> with fully default behavior. * @param <A> @@ -149,6 +151,7 @@ public class AnnotationFactory { private <A extends Annotation> A createInternal(ClassLoader classLoader, Object configurer) { + final Object existingConfigurer = CONFIGURER.get(); try { CONFIGURER.set(configurer); @SuppressWarnings("unchecked") @@ -156,7 +159,11 @@ public class AnnotationFactory { ANNOTATION_INVOKER, getStubType()); return result; } finally { - CONFIGURER.remove(); + if (existingConfigurer == null) { + CONFIGURER.remove(); + } else { + CONFIGURER.set(existingConfigurer); + } } } @@ -168,7 +175,7 @@ public class AnnotationFactory { return result; } @SuppressWarnings("unchecked") - final AnnotationStubConfigurer<A> configurer = (AnnotationStubConfigurer<A>) o; + final StubConfigurer<A> configurer = (StubConfigurer<A>) o; return configurer.getStubType(); } } Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractAnnotationFactoryTest.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractAnnotationFactoryTest.java?rev=997852&r1=997851&r2=997852&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractAnnotationFactoryTest.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractAnnotationFactoryTest.java Thu Sep 16 17:11:15 2010 @@ -20,8 +20,6 @@ package org.apache.commons.proxy2.stub; import static org.junit.Assert.*; import org.apache.commons.proxy2.ProxyFactory; -import org.apache.commons.proxy2.stub.AnnotationFactory; -import org.apache.commons.proxy2.stub.AnnotationStubConfigurer; import org.junit.Before; import org.junit.Test; @@ -50,10 +48,10 @@ public abstract class AbstractAnnotation @Test public void testStubbedAnnotation() { - CustomAnnotation customAnnotation = annotationFactory.create(new AnnotationStubConfigurer<CustomAnnotation>() { + CustomAnnotation customAnnotation = annotationFactory.create(new StubConfigurer<CustomAnnotation>() { @Override - protected void configureAnnotation(CustomAnnotation stub) { + protected void configure(CustomAnnotation stub) { when(stub.someType()).thenReturn(Object.class).when(stub.finiteValues()) .thenReturn(FiniteValues.ONE, FiniteValues.THREE).when(stub.annString()).thenReturn("hey"); } @@ -65,6 +63,28 @@ public abstract class AbstractAnnotation assertEquals(Object.class, customAnnotation.someType()); } + @Test + public void testNestedStubbedAnnotation() { + NestingAnnotation nestingAnnotation = + annotationFactory.create(new StubConfigurer<NestingAnnotation>() { + @Override + protected void configure(NestingAnnotation stub) { + when(stub.child()).thenReturn(annotationFactory.create(CustomAnnotation.class)) + .when(stub.somethingElse()).thenReturn("somethingElse"); + } + }); + assertEquals("", nestingAnnotation.child().annString()); + assertEquals(0, nestingAnnotation.child().finiteValues().length); + assertEquals(null, nestingAnnotation.child().someType()); + assertEquals("somethingElse", nestingAnnotation.somethingElse()); + } + + public @interface NestingAnnotation { + CustomAnnotation child(); + + String somethingElse(); + } + public @interface CustomAnnotation { String annString() default "";