Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0DE60CA56 for ; Thu, 7 Jun 2012 15:11:22 +0000 (UTC) Received: (qmail 72102 invoked by uid 500); 7 Jun 2012 15:11:21 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 72037 invoked by uid 500); 7 Jun 2012 15:11:21 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 72028 invoked by uid 99); 7 Jun 2012 15:11:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jun 2012 15:11:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jun 2012 15:11:15 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 99D3D23889E0 for ; Thu, 7 Jun 2012 15:10:53 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1347663 - in /commons/proper/proxy/branches/version-2.0-work/stub/src: main/java/org/apache/commons/proxy2/stub/ test/java/org/apache/commons/proxy2/stub/ Date: Thu, 07 Jun 2012 15:10:53 -0000 To: commits@commons.apache.org From: mbenson@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120607151053.99D3D23889E0@eris.apache.org> Author: mbenson Date: Thu Jun 7 15:10:52 2012 New Revision: 1347663 URL: http://svn.apache.org/viewvc?rev=1347663&view=rev Log: delegating annotation stubs 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/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfigurer.java commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AnnotationFactoryTest.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=1347663&r1=1347662&r2=1347663&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 Jun 7 15:10:52 2012 @@ -26,6 +26,7 @@ import java.lang.reflect.Proxy; import java.util.Map; import org.apache.commons.lang3.AnnotationUtils; +import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.proxy2.Interceptor; import org.apache.commons.proxy2.Invocation; @@ -34,6 +35,7 @@ import org.apache.commons.proxy2.ObjectP import org.apache.commons.proxy2.ProxyFactory; import org.apache.commons.proxy2.ProxyUtils; import org.apache.commons.proxy2.impl.AbstractProxyFactory; +import org.apache.commons.proxy2.provider.ConstantProvider; /** * {@link AnnotationFactory} provides a simplified API over {@link StubProxyFactory} @@ -164,6 +166,9 @@ public class AnnotationFactory { @Override protected void configure(A stub) { + if (attributes == null) { + return; + } When bud; StubConfiguration dy = this; for (Map.Entry attr : attributes.entrySet()) { @@ -251,61 +256,100 @@ public class AnnotationFactory { * @return stubbed annotation proxy */ public A create(StubConfigurer configurer) { - @SuppressWarnings("unchecked") - final A result = (A) createInternal(Thread.currentThread().getContextClassLoader(), configurer); - return result; + return create(Thread.currentThread().getContextClassLoader(), Validate.notNull(configurer, "null configurer")); + } + + /** + * Create a delegating annotation of the type supported by configurer. + * @param + * @param target not {@code null} + * @param configurer not {@code null} + * @return stubbed annotation proxy + */ + public A createDelegator(A target, StubConfigurer configurer) { + return createInternal(Thread.currentThread().getContextClassLoader(), + Validate.notNull(target, "null target"), Validate.notNull(configurer, "null configurer")); } /** * Create an annotation of the type supported by configurer in the specified classpath. * @param - * @param classLoader - * @param configurer + * @param classLoader not {@code null} + * @param configurer not {@code null} * @return stubbed annotation proxy */ public A create(ClassLoader classLoader, StubConfigurer configurer) { - @SuppressWarnings("unchecked") - final A result = (A) createInternal(classLoader, configurer); - return result; + return createInternal(Validate.notNull(classLoader, "null classLoader"), + null, Validate.notNull(configurer, "null configurer")); + } + + /** + * Create a delegating annotation of the type supported by configurer in the specified classpath. + * @param + * @param classLoader not {@code null} + * @param target not {@code null} + * @param configurer not {@code null} + * @return stubbed annotation proxy + */ + public A createDelegator(ClassLoader classLoader, A target, StubConfigurer configurer) { + return createInternal(Validate.notNull(classLoader, "null classLoader"), + Validate.notNull(target, "null target"), Validate.notNull(configurer, "null configurer")); } /** * Create an annotation of annotationType with fully default behavior. * @param - * @param classLoader - * @param annotationType + * @param annotationType not {@code null} * @return stubbed annotation proxy */ public A create(Class annotationType) { @SuppressWarnings("unchecked") - final A result = (A) createInternal(Thread.currentThread().getContextClassLoader(), annotationType); + final A result = + (A) createInternal(Thread.currentThread().getContextClassLoader(), + Validate.notNull(annotationType, "null annotationType")); return result; } /** * Create an annotation of annotationType with fully default behavior. * @param - * @param classLoader - * @param annotationType + * @param classLoader not {@code null} + * @param annotationType not {@code null} * @return stubbed annotation proxy */ public A create(ClassLoader classLoader, Class annotationType) { @SuppressWarnings("unchecked") - final A result = (A) createInternal(classLoader, annotationType); + final A result = + (A) createInternal(Validate.notNull(classLoader, "null classLoader"), + Validate.notNull(annotationType, "null annotationType")); return result; } /** * Create an annotation of annotationType with behavior specified by a {@link String}-keyed {@link Map}. * @param - * @param classLoader - * @param annotationType + * @param annotationType not {@code null} * @param attributes * @return stubbed annotation proxy */ public A create(Class annotationType, Map attributes) { - return attributes == null || attributes.isEmpty() ? create(annotationType) - : create(new MapBasedAnnotationConfigurer(annotationType, attributes)); + if (attributes == null || attributes.isEmpty()) { + return create(annotationType); + } + return create(new MapBasedAnnotationConfigurer(annotationType, attributes)); + } + + /** + * Create a delegating annotation of annotationType with behavior specified by a {@link String}-keyed {@link Map}. + * @param + * @param target not {@code null} + * @param attributes + * @return stubbed annotation proxy + */ + public A createDelegator(A target, Map attributes) { + @SuppressWarnings("unchecked") + final Class annotationType = (Class) Validate.notNull(target, "null target").annotationType(); + return createDelegator(target, new MapBasedAnnotationConfigurer(annotationType, attributes)); } /** @@ -318,11 +362,28 @@ public class AnnotationFactory { */ public A create(ClassLoader classLoader, Class annotationType, Map attributes) { - return attributes == null || attributes.isEmpty() ? create(classLoader, annotationType) : create(classLoader, - new MapBasedAnnotationConfigurer(annotationType, attributes)); + return create(classLoader, new MapBasedAnnotationConfigurer(annotationType, attributes)); + } + + /** + * Create a delegating annotation of annotationType with behavior specified by a {@link String}-keyed {@link Map}. + * @param + * @param classLoader + * @param target + * @param attributes + * @return stubbed annotation proxy + */ + public A createDelegator(ClassLoader classLoader, A target, Map attributes) { + @SuppressWarnings("unchecked") + final Class annotationType = (Class) Validate.notNull(target, "null target").annotationType(); + return createDelegator(classLoader, target, new MapBasedAnnotationConfigurer(annotationType, attributes)); } private A createInternal(ClassLoader classLoader, Object configurer) { + return createInternal(classLoader, null, configurer); + } + + private A createInternal(ClassLoader classLoader, A target, Object configurer) { final Object existingConfigurer = CONFIGURER.get(); final boolean outerContext = CONTEXT.get() == null; try { @@ -330,8 +391,17 @@ public class AnnotationFactory { if (outerContext) { CONTEXT.set(ImmutablePair.of(this, classLoader)); } - @SuppressWarnings("unchecked") - final A result = (A) proxyFactory.createInvokerProxy(classLoader, ANNOTATION_INVOKER, getStubType()); + final A result; + if (target == null) { + @SuppressWarnings("unchecked") + A invoker = (A) proxyFactory.createInvokerProxy(classLoader, ANNOTATION_INVOKER, getStubType()); + result = invoker; + } else { + @SuppressWarnings("unchecked") + A delegator = + (A) proxyFactory.createDelegatorProxy(classLoader, new ConstantProvider(target), getStubType()); + result = delegator; + } return validate(result); } finally { if (existingConfigurer == null) { Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java?rev=1347663&r1=1347662&r2=1347663&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java Thu Jun 7 15:10:52 2012 @@ -17,12 +17,16 @@ package org.apache.commons.proxy2.stub; +import org.apache.commons.functor.UnaryPredicate; import org.apache.commons.proxy2.ObjectProvider; /** * Fluent stub configuration interface inspired by Mockito stubbing mechanisms. - * This interface declares all the methods necessary to, in particular, allow - * varargs to be used to specify return values for any array type. + * This interface declares all the methods necessary to: + *
    + *
  • allow varargs to be used to specify return values for any array type
  • + *
  • make interactive stub calls describe nonspecific argument matching situations
  • + *
*/ public interface StubConfiguration { @@ -344,4 +348,67 @@ public interface StubConfiguration { */ WhenClass when(Class call); + /** + * Match any boolean. + * @return mock argument + */ + boolean anyBoolean(); + + /** + * Match any byte. + * @return mock argument + */ + byte anyByte(); + + /** + * Match any short. + * @return mock argument + */ + short anyShort(); + + /** + * Match any int. + * @return mock argument + */ + int anyInt(); + + /** + * Match any char. + * @return mock argument + */ + char anyChar(); + + /** + * Match any long. + * @return mock argument + */ + long anyLong(); + + /** + * Match any float. + * @return mock argument + */ + float anyFloat(); + + /** + * Match any double. + * @return mock argument + */ + double anyDouble(); + + /** + * Match any object. + * @param + * @return mock argument + */ + T any(); + + /** + * Match an argument with a test. + * @param + * @param test + * @return mock argument + */ + T argThat(UnaryPredicate test); + } Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfigurer.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfigurer.java?rev=1347663&r1=1347662&r2=1347663&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfigurer.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfigurer.java Thu Jun 7 15:10:52 2012 @@ -17,11 +17,13 @@ package org.apache.commons.proxy2.stub; +import org.apache.commons.functor.UnaryPredicate; import org.apache.commons.lang3.reflect.TypeUtils; import org.apache.commons.proxy2.ObjectProvider; import org.apache.commons.proxy2.provider.ConstantProvider; /** + *

* Configuration mechanism for a stub. Implements {@link StubConfiguration} for maximum fluency. * A {@link StubConfigurer} needs to know the type of stub object to which it applies. * Any useful runtime subclass should have the type variable non-generically @@ -34,7 +36,11 @@ import org.apache.commons.proxy2.provide * } * } * + *

* + *

Note that when argument matching is used, all method parameters must be specified using matchers. + * TODO add matching example + *

* @param * @author Matt Benson */ @@ -65,12 +71,11 @@ public abstract class StubConfigurer return; } @SuppressWarnings("unchecked") - final Class resolvedVariable = (Class) TypeUtils.getRawType( - StubConfigurer.class.getTypeParameters()[0], getClass()); + final Class resolvedVariable = + (Class) TypeUtils.getRawType(StubConfigurer.class.getTypeParameters()[0], getClass()); if (resolvedVariable == null) { - throw new IllegalArgumentException( - "stubType was not specified and could not be calculated for " - + getClass()); + throw new IllegalArgumentException("stubType was not specified and could not be calculated for " + + getClass()); } this.stubType = resolvedVariable; } @@ -86,8 +91,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.When when( - RT call) { + public org.apache.commons.proxy2.stub.StubConfiguration.When when(RT call) { return new When() { public StubConfiguration thenReturn(RT result) { @@ -95,8 +99,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -105,8 +108,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -116,8 +118,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenBooleanArray when( - boolean[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenBooleanArray when(boolean[] call) { return new WhenBooleanArray() { public StubConfiguration thenReturn(boolean... b) { @@ -125,8 +126,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -135,8 +135,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -146,8 +145,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenByteArray when( - byte[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenByteArray when(byte[] call) { return new WhenByteArray() { public StubConfiguration thenReturn(byte... b) { @@ -155,8 +153,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -165,8 +162,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -176,8 +172,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenShortArray when( - short[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenShortArray when(short[] call) { return new WhenShortArray() { public StubConfiguration thenReturn(short... s) { @@ -185,8 +180,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -195,8 +189,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -206,8 +199,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenIntArray when( - int[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenIntArray when(int[] call) { return new WhenIntArray() { public StubConfiguration thenReturn(int... i) { @@ -215,8 +207,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -225,8 +216,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -236,8 +226,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenCharArray when( - char[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenCharArray when(char[] call) { return new WhenCharArray() { public StubConfiguration thenReturn(char... c) { @@ -245,8 +234,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -255,8 +243,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -266,8 +253,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenLongArray when( - long[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenLongArray when(long[] call) { return new WhenLongArray() { public StubConfiguration thenReturn(long... l) { @@ -275,8 +261,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -285,8 +270,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -296,8 +280,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenFloatArray when( - float[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenFloatArray when(float[] call) { return new WhenFloatArray() { public StubConfiguration thenReturn(float... f) { @@ -305,8 +288,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -315,8 +297,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -326,8 +307,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenDoubleArray when( - double[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenDoubleArray when(double[] call) { return new WhenDoubleArray() { public StubConfiguration thenReturn(double... d) { @@ -335,8 +315,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -345,8 +324,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -356,8 +334,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenObjectArray when( - C[] call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenObjectArray when(C[] call) { return new WhenObjectArray() { public StubConfiguration thenReturn(C... c) { @@ -365,8 +342,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -375,8 +351,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -386,8 +361,7 @@ public abstract class StubConfigurer /** * {@inheritDoc} */ - public org.apache.commons.proxy2.stub.StubConfiguration.WhenClass when( - Class call) { + public org.apache.commons.proxy2.stub.StubConfiguration.WhenClass when(Class call) { return new WhenClass() { public StubConfiguration thenReturn(Class c) { @@ -395,8 +369,7 @@ public abstract class StubConfigurer return StubConfigurer.this; } - public StubConfiguration thenAnswer( - ObjectProvider> objectProvider) { + public StubConfiguration thenAnswer(ObjectProvider> objectProvider) { requireStubInterceptor().addAnswer(objectProvider); return StubConfigurer.this; } @@ -405,8 +378,7 @@ public abstract class StubConfigurer return thenThrow(new ConstantProvider(t)); } - public StubConfiguration thenThrow( - ObjectProvider throwableProvider) { + public StubConfiguration thenThrow(ObjectProvider throwableProvider) { requireStubInterceptor().addThrow(throwableProvider); return StubConfigurer.this; } @@ -414,13 +386,102 @@ public abstract class StubConfigurer } /** + * {@inheritDoc} + */ + @Override + public boolean anyBoolean() { + // TODO Auto-generated method stub + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public byte anyByte() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public short anyShort() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public int anyInt() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public char anyChar() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public long anyLong() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public float anyFloat() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public double anyDouble() { + // TODO Auto-generated method stub + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public U any() { + // TODO Auto-generated method stub + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public U argThat(UnaryPredicate test) { + // TODO Auto-generated method stub + return null; + } + + /** * Apply thyself against the specified stub interceptor. * @param stubInterceptor */ final void configure(StubInterceptor stubInterceptor, T stub) { if (stubInterceptor == null) { - throw new IllegalArgumentException( - "Cannot configure null StubInterceptor"); + throw new IllegalArgumentException("Cannot configure null StubInterceptor"); } synchronized (this) { this.stubInterceptor = stubInterceptor; Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AnnotationFactoryTest.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AnnotationFactoryTest.java?rev=1347663&r1=1347662&r2=1347663&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AnnotationFactoryTest.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AnnotationFactoryTest.java Thu Jun 7 15:10:52 2012 @@ -22,9 +22,13 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.assertNotNull; import java.lang.annotation.Annotation; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; import org.junit.Test; @@ -32,6 +36,7 @@ import org.junit.Test; * Test {@link AnnotationFactory}. */ public class AnnotationFactoryTest { + @CustomAnnotation(annString = "FOO", finiteValues = { FiniteValues.ONE, FiniteValues.TWO, FiniteValues.THREE }, someType = Object.class) private AnnotationFactory annotationFactory; @Before @@ -167,12 +172,65 @@ public class AnnotationFactoryTest { annotationFactory.create(CustomAnnotation.class, attributes); } + @Test + public void testDelegator() { + final boolean forceAccess = true; + final CustomAnnotation sourceAnnotation = + FieldUtils.getDeclaredField(AnnotationFactoryTest.class, "annotationFactory", forceAccess).getAnnotation( + CustomAnnotation.class); + assertNotNull(sourceAnnotation); + CustomAnnotation stub = + annotationFactory.createDelegator(sourceAnnotation, new AnnotationConfigurer() { + + @Override + protected void configure(CustomAnnotation stub) { + when(stub.finiteValues()).thenReturn(FiniteValues.ONE); + } + }); + assertEquals(CustomAnnotation.class, stub.annotationType()); + assertEquals(Object.class, stub.someType()); + assertEquals("FOO", stub.annString()); + assertArrayEquals(new FiniteValues[] { FiniteValues.ONE }, stub.finiteValues()); + } + + @Test(expected = NullPointerException.class) + public void testDelegatorMissingTarget() { + annotationFactory.createDelegator(null, new StubConfigurer() { + + @Override + protected void configure(CustomAnnotation stub) { + } + }); + } + + @Test + public void testDelegatorWithAttributes() { + final boolean forceAccess = true; + final CustomAnnotation sourceAnnotation = + FieldUtils.getDeclaredField(AnnotationFactoryTest.class, "annotationFactory", forceAccess).getAnnotation( + CustomAnnotation.class); + assertNotNull(sourceAnnotation); + Map attributes = + Collections. singletonMap("finiteValues", new FiniteValues[] { FiniteValues.ONE }); + CustomAnnotation stub = annotationFactory.createDelegator(sourceAnnotation, attributes); + assertEquals(CustomAnnotation.class, stub.annotationType()); + assertEquals(Object.class, stub.someType()); + assertEquals("FOO", stub.annString()); + assertArrayEquals(new FiniteValues[] { FiniteValues.ONE }, stub.finiteValues()); + } + + @Test(expected = NullPointerException.class) + public void testDelegatorWithAttributesMissingTarget() { + annotationFactory.createDelegator(null, Collections. emptyMap()); + } + public @interface NestingAnnotation { CustomAnnotation child(); String somethingElse(); } + @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation { String annString() default "";