You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jc...@apache.org on 2013/08/02 05:17:26 UTC
svn commit: r1509522 - 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/
Author: jcarman
Date: Fri Aug 2 03:17:25 2013
New Revision: 1509522
URL: http://svn.apache.org/r1509522
Log:
Refactoring test cases.
Removing old code.
Added:
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubTestCase.java
Removed:
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubConfiguration.java
Modified:
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubBuilderTest.java
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java?rev=1509522&r1=1509521&r2=1509522&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java Fri Aug 2 03:17:25 2013
@@ -17,12 +17,14 @@
package org.apache.commons.proxy2.stub;
+import org.apache.commons.lang3.builder.Builder;
import org.apache.commons.proxy2.ObjectProvider;
import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
import org.apache.commons.proxy2.invoker.NullInvoker;
import org.apache.commons.proxy2.provider.ConstantProvider;
-public class StubBuilder<T>
+public class StubBuilder<T> implements Builder<T>
{
//----------------------------------------------------------------------------------------------------------------------
// Fields
@@ -30,8 +32,8 @@ public class StubBuilder<T>
private final ProxyFactory proxyFactory;
private final T target;
- private final StubInterceptorBuilder interceptorBuilder;
private final Class<T> type;
+ private final SwitchInterceptor switchInterceptor = new SwitchInterceptor();
//----------------------------------------------------------------------------------------------------------------------
// Constructors
@@ -42,7 +44,6 @@ public class StubBuilder<T>
this.proxyFactory = proxyFactory;
this.type = type;
this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, type);
- this.interceptorBuilder = new StubInterceptorBuilder(proxyFactory);
}
public StubBuilder(ProxyFactory proxyFactory, Class<T> type, ObjectProvider<? extends T> provider)
@@ -50,7 +51,6 @@ public class StubBuilder<T>
this.proxyFactory = proxyFactory;
this.type = type;
this.target = proxyFactory.createDelegatorProxy(provider, type);
- this.interceptorBuilder = new StubInterceptorBuilder(proxyFactory);
}
public StubBuilder(ProxyFactory proxyFactory, Class<T> type, T target)
@@ -58,7 +58,6 @@ public class StubBuilder<T>
this.proxyFactory = proxyFactory;
this.type = type;
this.target = proxyFactory.createDelegatorProxy(new ConstantProvider<T>(target), type);
- this.interceptorBuilder = new StubInterceptorBuilder(proxyFactory);
}
//----------------------------------------------------------------------------------------------------------------------
@@ -67,12 +66,21 @@ public class StubBuilder<T>
public T build()
{
- return proxyFactory.createInterceptorProxy(target, interceptorBuilder.build(), type);
+ return proxyFactory.createInterceptorProxy(target, switchInterceptor, type);
}
public StubBuilder<T> train(Trainer<T> trainer)
{
- interceptorBuilder.trainFor(type, trainer);
+ try
+ {
+ TrainingContext trainingContext = TrainingContext.set(proxyFactory);
+ T trainee = trainingContext.push(type, switchInterceptor);
+ trainer.train(trainee);
+ }
+ finally
+ {
+ TrainingContext.clear();
+ }
return this;
}
}
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java?rev=1509522&r1=1509521&r2=1509522&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java Fri Aug 2 03:17:25 2013
@@ -18,6 +18,7 @@
package org.apache.commons.proxy2.stub;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.reflect.TypeUtils;
import org.apache.commons.proxy2.ObjectProvider;
import org.apache.commons.proxy2.ProxyUtils;
import org.apache.commons.proxy2.interceptor.InterceptorUtils;
@@ -30,7 +31,7 @@ public abstract class Trainer<T>
// Abstract Methods
//----------------------------------------------------------------------------------------------------------------------
- protected abstract void train(T stub);
+ protected abstract void train(T trainee);
//----------------------------------------------------------------------------------------------------------------------
// Other Methods
@@ -53,6 +54,12 @@ public abstract class Trainer<T>
return value;
}
+ @SuppressWarnings("unchecked")
+ public Class<T> getTraineeType()
+ {
+ return (Class<T>) TypeUtils.getRawType(Trainer.class.getTypeParameters()[0], getClass());
+ }
+
protected <R> R isInstance(Class<R> type)
{
record(ArgumentMatcherUtils.isA(type));
@@ -83,42 +90,42 @@ public abstract class Trainer<T>
{
return new WhenByteArray();
}
-
+
protected WhenBooleanArray when(boolean[] expression)
{
return new WhenBooleanArray();
}
-
+
protected WhenIntArray when(int[] expression)
{
return new WhenIntArray();
}
-
+
protected WhenShortArray when(short[] expresssion)
{
return new WhenShortArray();
}
-
+
protected WhenLongArray when(long[] expression)
{
return new WhenLongArray();
}
-
+
protected WhenFloatArray when(float[] expression)
{
return new WhenFloatArray();
}
-
+
protected WhenDoubleArray when(double[] expression)
{
return new WhenDoubleArray();
}
-
+
protected <R> WhenObjectArray<R> when(R[] expression)
{
- return new WhenObjectArray<R>();
+ return new WhenObjectArray<R>();
}
-
+
protected WhenCharArray when(char[] expression)
{
return new WhenCharArray();
@@ -130,9 +137,9 @@ public abstract class Trainer<T>
protected abstract class BaseWhen<R>
{
- protected Trainer<T> thenStub(Class<R> type, Trainer<R> trainer)
+ protected Trainer<T> thenStub(Trainer<R> trainer)
{
- R trainee = trainingContext().push(type);
+ R trainee = trainingContext().push(trainer.getTraineeType());
trainer.train(trainee);
trainingContext().then(InterceptorUtils.constant(trainingContext().pop()));
return Trainer.this;
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java?rev=1509522&r1=1509521&r2=1509522&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/TrainingContext.java Fri Aug 2 03:17:25 2013
@@ -147,7 +147,7 @@ public class TrainingContext
}
}
- private static class TrainingContextFrame<T>
+ private static final class TrainingContextFrame<T>
{
private final String id = UUID.randomUUID().toString();
@@ -200,7 +200,7 @@ public class TrainingContext
}
}
- private static class TrainingInvoker implements Invoker
+ private static final class TrainingInvoker implements Invoker
{
private final String id;
Added: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubTestCase.java?rev=1509522&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubTestCase.java (added)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubTestCase.java Fri Aug 2 03:17:25 2013
@@ -0,0 +1,383 @@
+package org.apache.commons.proxy2.stub;
+
+import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.cglib.CglibProxyFactory;
+import org.apache.commons.proxy2.invoker.NullInvoker;
+import org.apache.commons.proxy2.provider.ObjectProviderUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.*;
+
+public abstract class AbstractStubTestCase
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+ protected ProxyFactory proxyFactory;
+ protected StubInterface target;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Abstract Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+ protected abstract StubInterface createProxy(Trainer<StubInterface> trainer);
+
+//----------------------------------------------------------------------------------------------------------------------
+// Other Methods
+//----------------------------------------------------------------------------------------------------------------------
+
+ @Before
+ public final void setUpProxyFactory()
+ {
+ this.proxyFactory = new CglibProxyFactory();
+ this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class);
+ }
+
+ @Test
+ public void testAnyMatcher()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one(any(String.class))).thenReturn("World");
+ }
+ });
+ assertEquals("World", proxy.one("Hello"));
+ assertEquals("World", proxy.one(null));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testMixingArgumentMatchingStrategies()
+ {
+ createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.three(isInstance(String.class), "World")).thenAnswer(ObjectProviderUtils.constant("World"));
+ }
+ });
+ }
+
+ @Test
+ public void testStubReturn()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.stub()).thenStub(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one("Hello")).thenReturn("World");
+ }
+ });
+ }
+ });
+ assertNotNull(proxy.stub());
+ assertEquals("World", proxy.stub().one("Hello"));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testThenBeforeWhen()
+ {
+ createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ thenThrow(new RuntimeException("Oops!"));
+ }
+ });
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testThrowExceptionWithException()
+ {
+ StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ trainee.voidMethod("Hello");
+ thenThrow(new IllegalArgumentException("Nope!"));
+ }
+ });
+ proxy.voidMethod("Hello");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testThrowExceptionWithProvidedException()
+ {
+ StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ trainee.voidMethod("Hello");
+ thenThrow(ObjectProviderUtils.constant(new IllegalArgumentException("Nope!")));
+ }
+ });
+ proxy.voidMethod("Hello");
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testThrowingExceptionObject()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one("Hello")).thenThrow(new RuntimeException("No way, Jose!"));
+ }
+ });
+ proxy.one("Hello");
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testThrowingProvidedException()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one("Hello")).thenThrow(ObjectProviderUtils.constant(new RuntimeException("No way, Jose!")));
+ }
+ });
+ proxy.one("Hello");
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testUsingWrongStub()
+ {
+ createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(final StubInterface parent)
+ {
+ when(parent.stub()).thenStub(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(final StubInterface child)
+ {
+ when(parent.one("Hello")).thenReturn("World");
+ }
+ });
+ }
+ });
+ }
+
+ @Test
+ public void testWithArgumentMatchers()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one(isInstance(String.class))).thenAnswer(ObjectProviderUtils.constant("World"));
+ }
+ });
+ assertEquals("World", proxy.one("Hello"));
+ assertEquals("World", proxy.one("Whatever"));
+ }
+
+ @Test
+ public void testWithArrayParameter()
+ {
+ StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.arrayParameter("One", "Two", "Three")).thenReturn("Four");
+ }
+ });
+
+ assertEquals("Four", proxy.arrayParameter("One", "Two", "Three"));
+ }
+
+ @Test
+ public void testWithBooleanArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.booleanArray()).thenReturn(false, true, false);
+ }
+ });
+ assertTrue(Arrays.equals(new boolean[]{false, true, false}, proxy.booleanArray()));
+ }
+
+ @Test
+ public void testWithByteArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.byteArray()).thenReturn((byte) 1, (byte) 2);
+ }
+ });
+ assertArrayEquals(new byte[]{1, 2}, proxy.byteArray());
+ }
+
+ @Test
+ public void testWithCharArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.charArray()).thenReturn('a', 'b', 'c');
+ }
+ });
+ assertArrayEquals(new char[]{'a', 'b', 'c'}, proxy.charArray());
+ }
+
+ @Test
+ public void testWithDoubleArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.doubleArray()).thenReturn(1.0, 2.0);
+ }
+ });
+ assertArrayEquals(new double[]{1.0, 2.0}, proxy.doubleArray(), 0.0);
+ }
+
+ @Test
+ public void testWithFloatArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.floatArray()).thenReturn(1f, 2f);
+ }
+ });
+ assertArrayEquals(new float[]{1f, 2f}, proxy.floatArray(), 0.0f);
+ }
+
+ @Test
+ public void testWithIntArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.intArray()).thenReturn(1, 2);
+ }
+ });
+ assertArrayEquals(new int[]{1, 2}, proxy.intArray());
+ }
+
+ @Test
+ public void testWithLongArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.longArray()).thenReturn(1, 2);
+ }
+ });
+ assertArrayEquals(new long[]{1, 2}, proxy.longArray());
+ }
+
+ @Test
+ public void testWithMismatchedArgument()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one(eq("Hello"))).thenReturn("World");
+ }
+ });
+ assertEquals("World", proxy.one("Hello"));
+ assertEquals(null, proxy.one("Whatever"));
+ }
+
+ @Test
+ public void testWithMultipleMethodsTrained()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one("Hello")).thenReturn("World");
+ when(trainee.two("Foo")).thenReturn("Bar");
+ }
+ });
+ assertEquals("World", proxy.one("Hello"));
+ assertEquals("Bar", proxy.two("Foo"));
+ }
+
+ @Test
+ public void testWithShortArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.shortArray()).thenReturn((short) 1, (short) 2);
+ }
+ });
+ assertArrayEquals(new short[]{1, 2}, proxy.shortArray());
+ }
+
+ @Test
+ public void testWithSingleMethodTrained()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.one("Hello")).thenReturn("World");
+ }
+ });
+ assertEquals("World", proxy.one("Hello"));
+ assertEquals(null, proxy.two("Whatever"));
+ assertEquals(null, proxy.one("Mismatch!"));
+ }
+
+ @Test
+ public void testWithStringArray()
+ {
+ final StubInterface proxy = createProxy(new Trainer<StubInterface>()
+ {
+ @Override
+ protected void train(StubInterface trainee)
+ {
+ when(trainee.stringArray()).thenReturn("One", "Two");
+ }
+ });
+ assertArrayEquals(new String[]{"One", "Two"}, proxy.stringArray());
+ }
+}
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubBuilderTest.java?rev=1509522&r1=1509521&r2=1509522&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubBuilderTest.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubBuilderTest.java Fri Aug 2 03:17:25 2013
@@ -18,7 +18,6 @@
package org.apache.commons.proxy2.stub;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.proxy2.ProxyFactory;
import org.apache.commons.proxy2.cglib.CglibProxyFactory;
import org.apache.commons.proxy2.provider.BeanProvider;
import org.junit.Before;
@@ -27,22 +26,18 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-public class StubBuilderTest
+public class StubBuilderTest extends AbstractStubTestCase
{
-//----------------------------------------------------------------------------------------------------------------------
-// Fields
-//----------------------------------------------------------------------------------------------------------------------
-
- private ProxyFactory proxyFactory;
//----------------------------------------------------------------------------------------------------------------------
// Other Methods
//----------------------------------------------------------------------------------------------------------------------
- @Before
- public void initialize()
+
+ @Override
+ protected StubInterface createProxy(Trainer<StubInterface> trainer)
{
- proxyFactory = new CglibProxyFactory();
+ return new StubBuilder<StubInterface>(proxyFactory, StubInterface.class).train(trainer).build();
}
@Test
@@ -52,9 +47,9 @@ public class StubBuilderTest
builder.train(new Trainer<StubInterface>()
{
@Override
- protected void train(StubInterface stub)
+ protected void train(StubInterface trainee)
{
- when(stub.one("Foo")).thenReturn("Bar");
+ when(trainee.one("Foo")).thenReturn("Bar");
}
});
StubInterface stub = builder.build();
@@ -76,9 +71,9 @@ public class StubBuilderTest
builder.train(new Trainer<StubInterface>()
{
@Override
- protected void train(StubInterface stub)
+ protected void train(StubInterface trainee)
{
- when(stub.one("Foo")).thenReturn("Bar");
+ when(trainee.one("Foo")).thenReturn("Bar");
}
});
StubInterface stub = builder.build();
@@ -92,9 +87,9 @@ public class StubBuilderTest
builder.train(new Trainer<StubInterface>()
{
@Override
- protected void train(StubInterface stub)
+ protected void train(StubInterface trainee)
{
- when(stub.one("Foo")).thenReturn("Bar");
+ when(trainee.one("Foo")).thenReturn("Bar");
}
});
StubInterface stub = builder.build();
@@ -176,7 +171,7 @@ public class StubBuilderTest
@Override
public String[] stringArray()
{
- return new String[] {"One", "Two", "Three"};
+ return new String[]{"One", "Two", "Three"};
}
@Override
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java?rev=1509522&r1=1509521&r2=1509522&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java Fri Aug 2 03:17:25 2013
@@ -25,20 +25,16 @@ import org.apache.commons.proxy2.provide
import org.junit.Before;
import org.junit.Test;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import static org.junit.Assert.*;
-public class StubInterceptorBuilderTest
+public class StubInterceptorBuilderTest extends AbstractStubTestCase
{
//----------------------------------------------------------------------------------------------------------------------
// Fields
//----------------------------------------------------------------------------------------------------------------------
- private ProxyFactory proxyFactory;
- private StubInterface target;
private StubInterceptorBuilder builder;
//----------------------------------------------------------------------------------------------------------------------
@@ -46,401 +42,58 @@ public class StubInterceptorBuilderTest
//----------------------------------------------------------------------------------------------------------------------
@Before
- public void setUp()
+ public void initialize()
{
- this.proxyFactory = new CglibProxyFactory();
- this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class);
- this.builder = new StubInterceptorBuilder(proxyFactory);
+ builder = new StubInterceptorBuilder(proxyFactory);
}
- @Test(expected = IllegalArgumentException.class)
- public void testThrowExceptionWithException()
- {
- StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- stub.voidMethod("Hello");
- thenThrow(new IllegalArgumentException("Nope!"));
- }
- });
- proxy.voidMethod("Hello");
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testThrowExceptionWithProvidedException()
- {
- StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- stub.voidMethod("Hello");
- thenThrow(ObjectProviderUtils.constant(new IllegalArgumentException("Nope!")));
- }
- });
- proxy.voidMethod("Hello");
- }
-
- @Test
- public void testWithArrayParameter()
- {
- StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.arrayParameter("One", "Two", "Three")).thenReturn("Four");
- }
- });
-
- assertEquals("Four", proxy.arrayParameter("One", "Two", "Three"));
- }
-
- @Test
- public void testAnyMatcher()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one(any(String.class))).thenReturn("World");
- }
- });
- assertEquals("World", proxy.one("Hello"));
- assertEquals("World", proxy.one(null));
- }
-
- @Test(expected = IllegalStateException.class)
- public void testMixingArgumentMatchingStrategies()
- {
- builder.trainFor(StubInterface.class, new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.three(isInstance(String.class), "World")).thenAnswer(ObjectProviderUtils.constant("World"));
- }
- });
- }
-
- @Test(expected = RuntimeException.class)
- public void testThrowingExceptionObject()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one("Hello")).thenThrow(new RuntimeException("No way, Jose!"));
- }
- });
- proxy.one("Hello");
- }
-
- @Test(expected = RuntimeException.class)
- public void testThrowingProvidedException()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one("Hello")).thenThrow(ObjectProviderUtils.constant(new RuntimeException("No way, Jose!")));
- }
- });
- proxy.one("Hello");
- }
-
- @Test
- public void testWithArgumentMatchers()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one(isInstance(String.class))).thenAnswer(ObjectProviderUtils.constant("World"));
- }
- });
- assertEquals("World", proxy.one("Hello"));
- assertEquals("World", proxy.one("Whatever"));
- }
-
- private StubInterface createProxy(Trainer<StubInterface> trainer)
+ @Override
+ protected StubInterface createProxy(Trainer<StubInterface> trainer)
{
Interceptor interceptor = builder.trainFor(StubInterface.class, trainer).build();
-
- return proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
- }
-
- @Test
- public void testWithStringArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.stringArray()).thenReturn("One", "Two");
- }
- });
- assertArrayEquals(new String[]{"One", "Two"}, proxy.stringArray());
- }
-
- @Test
- public void testWithBooleanArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.booleanArray()).thenReturn(false, true, false);
- }
- });
- assertTrue(Arrays.equals(new boolean[]{false, true, false}, proxy.booleanArray()));
- }
-
- @Test
- public void testWithByteArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.byteArray()).thenReturn((byte) 1, (byte) 2);
- }
- });
- assertArrayEquals(new byte[]{1, 2}, proxy.byteArray());
- }
-
- @Test
- public void testWithShortArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.shortArray()).thenReturn((short) 1, (short) 2);
- }
- });
- assertArrayEquals(new short[]{1, 2}, proxy.shortArray());
- }
-
- @Test
- public void testWithIntArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.intArray()).thenReturn(1, 2);
- }
- });
- assertArrayEquals(new int[]{1, 2}, proxy.intArray());
- }
-
- @Test
- public void testWithLongArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.longArray()).thenReturn(1, 2);
- }
- });
- assertArrayEquals(new long[]{1, 2}, proxy.longArray());
- }
-
- @Test
- public void testWithFloatArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.floatArray()).thenReturn(1f, 2f);
- }
- });
- assertArrayEquals(new float[]{1f, 2f}, proxy.floatArray(), 0.0f);
- }
-
- @Test
- public void testWithDoubleArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.doubleArray()).thenReturn(1.0, 2.0);
- }
- });
- assertArrayEquals(new double[]{1.0, 2.0}, proxy.doubleArray(), 0.0);
- }
-
- @Test
- public void testWithCharArray()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.charArray()).thenReturn('a', 'b', 'c');
- }
- });
- assertArrayEquals(new char[]{'a', 'b', 'c'}, proxy.charArray());
- }
-
- @Test
- public void testWithMismatchedArgument()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one(eq("Hello"))).thenReturn("World");
- }
- });
- assertEquals("World", proxy.one("Hello"));
- assertEquals(null, proxy.one("Whatever"));
- }
-
- @Test
- public void testWithMultipleMethodsTrained()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one("Hello")).thenReturn("World");
- when(stub.two("Foo")).thenReturn("Bar");
- }
- });
- assertEquals("World", proxy.one("Hello"));
- assertEquals("Bar", proxy.two("Foo"));
- }
-
- @Test
- public void testWithSingleMethodTrained()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one("Hello")).thenReturn("World");
- }
- });
- assertEquals("World", proxy.one("Hello"));
- assertEquals(null, proxy.two("Whatever"));
- assertEquals(null, proxy.one("Mismatch!"));
- }
-
- @Test
- public void testStubReturn()
- {
- final StubInterface proxy = createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.stub()).thenStub(StubInterface.class, new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- when(stub.one("Hello")).thenReturn("World");
- }
- });
- }
- });
- assertNotNull(proxy.stub());
- assertEquals("World", proxy.stub().one("Hello"));
- }
-
- @Test(expected = IllegalStateException.class)
- public void testUsingWrongStub()
- {
- createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(final StubInterface parent)
- {
- when(parent.stub()).thenStub(StubInterface.class, new Trainer<StubInterface>()
- {
- @Override
- protected void train(final StubInterface child)
- {
- when(parent.one("Hello")).thenReturn("World");
- }
- });
- }
- });
- }
-
- @Test(expected = IllegalStateException.class)
- public void testThenBeforeWhen()
- {
- createProxy(new Trainer<StubInterface>()
- {
- @Override
- protected void train(StubInterface stub)
- {
- thenThrow(new RuntimeException("Oops!"));
- }
- });
- }
-
- @Test
- public void testWithNestedAnnotations()
- {
- Interceptor interceptor = builder.trainFor(RetentionWrapper.class, new Trainer<RetentionWrapper>()
- {
- @Override
- protected void train(RetentionWrapper stub)
- {
-
- when(stub.value()).thenStub(Retention.class, new Trainer<Retention>()
- {
- @Override
- protected void train(Retention stub)
- {
- when(stub.value()).thenReturn(RetentionPolicy.RUNTIME);
- }
- });
- }
- }).build();
- RetentionWrapper wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, RetentionWrapper.class);
- assertNotNull(wrapper.value());
- assertEquals(RetentionPolicy.RUNTIME, wrapper.value().value());
- }
-
- @Test
- public void testWithSimpleAnnotations()
- {
- Interceptor interceptor = builder.trainFor(Retention.class, new Trainer<Retention>()
- {
- @Override
- protected void train(Retention stub)
- {
- when(stub.value()).thenReturn(RetentionPolicy.RUNTIME);
- }
- }).build();
- Retention wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, Retention.class);
- assertEquals(RetentionPolicy.RUNTIME, wrapper.value());
- }
+ return proxyFactory.createInterceptorProxy(
+ proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class),
+ interceptor,
+ StubInterface.class);
+ }
+
+ // @Test
+// public void testWithNestedAnnotations()
+// {
+// Interceptor interceptor = builder.trainFor(RetentionWrapper.class, new Trainer<RetentionWrapper>()
+// {
+// @Override
+// protected void train(RetentionWrapper trainee)
+// {
+//
+// when(trainee.value()).thenStub(new Trainer<Retention>()
+// {
+// @Override
+// protected void train(Retention trainee)
+// {
+// when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME);
+// }
+// });
+// }
+// }).build();
+// RetentionWrapper wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, RetentionWrapper.class);
+// assertNotNull(wrapper.value());
+// assertEquals(RetentionPolicy.RUNTIME, wrapper.value().value());
+// }
+//
+// @Test
+// public void testWithSimpleAnnotations()
+// {
+// Interceptor interceptor = builder.trainFor(Retention.class, new Trainer<Retention>()
+// {
+// @Override
+// protected void train(Retention trainee)
+// {
+// when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME);
+// }
+// }).build();
+// Retention wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, Retention.class);
+// assertEquals(RetentionPolicy.RUNTIME, wrapper.value());
+// }
}