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/07/30 05:36:18 UTC
svn commit: r1508286 - in /commons/proper/proxy/branches/version-2.0-work:
core/src/main/java/org/apache/commons/proxy2/interceptor/
core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/
stub/src/main/java/org/apache/commons/proxy2...
Author: jcarman
Date: Tue Jul 30 03:36:17 2013
New Revision: 1508286
URL: http://svn.apache.org/r1508286
Log:
Adding type-safe array return specification methods.
Added:
commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ThrowingInterceptor.java
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java
- copied, changed from r1508275, commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
Removed:
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java
Modified:
commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.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/StubInterface.java
Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java?rev=1508286&r1=1508285&r2=1508286&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/InterceptorUtils.java Tue Jul 30 03:36:17 2013
@@ -37,6 +37,16 @@ public final class InterceptorUtils
return new ObjectProviderInterceptor(provider);
}
+ public static Interceptor throwing(Exception e)
+ {
+ return new ThrowingInterceptor(ObjectProviderUtils.constant(e));
+ }
+
+ public static Interceptor throwing(ObjectProvider<? extends Exception> provider)
+ {
+ return new ThrowingInterceptor(provider);
+ }
+
//----------------------------------------------------------------------------------------------------------------------
// Constructors
//----------------------------------------------------------------------------------------------------------------------
Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ThrowingInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ThrowingInterceptor.java?rev=1508286&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ThrowingInterceptor.java (added)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/ThrowingInterceptor.java Tue Jul 30 03:36:17 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.proxy2.interceptor;
+
+import org.apache.commons.proxy2.Interceptor;
+import org.apache.commons.proxy2.Invocation;
+import org.apache.commons.proxy2.ObjectProvider;
+
+public class ThrowingInterceptor implements Interceptor
+{
+//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+
+ private final ObjectProvider<? extends Exception> provider;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Constructors
+//----------------------------------------------------------------------------------------------------------------------
+
+ public ThrowingInterceptor(ObjectProvider<? extends Exception> provider)
+ {
+ this.provider = provider;
+ }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Interceptor Implementation
+//----------------------------------------------------------------------------------------------------------------------
+
+ @Override
+ public Object intercept(Invocation invocation) throws Throwable
+ {
+ throw provider.getObject();
+ }
+}
Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java?rev=1508286&r1=1508285&r2=1508286&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/matcher/argument/ArgumentMatcherUtils.java Tue Jul 30 03:36:17 2013
@@ -11,37 +11,110 @@ public class ArgumentMatcherUtils
public static ArgumentMatcher any()
{
- return new ArgumentMatcher()
- {
- @Override
- public boolean matches(Object argument)
- {
- return true;
- }
- };
+ return new AnyMatcher();
}
public static ArgumentMatcher eq(final Object value)
{
- return new ArgumentMatcher()
+ return new EqualsMatcher(value);
+ }
+
+ public static ArgumentMatcher isA(final Class<?> type)
+ {
+ return new InstanceOfMatcher(type);
+ }
+
+ public static ArgumentMatcher isNull()
+ {
+ return new IsNullMatcher();
+ }
+
+ public static ArgumentMatcher notNull()
+ {
+ return new NotNullMatcher();
+ }
+
+ public static ArgumentMatcher same(final Object ref)
+ {
+ return new SameMatcher(ref);
+ }
+
+//----------------------------------------------------------------------------------------------------------------------
+// Inner Classes
+//----------------------------------------------------------------------------------------------------------------------
+
+ private static class AnyMatcher implements ArgumentMatcher
+ {
+ @Override
+ public boolean matches(Object argument)
+ {
+ return true;
+ }
+ }
+
+ private static class EqualsMatcher implements ArgumentMatcher
+ {
+ private final Object value;
+
+ public EqualsMatcher(Object value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public boolean matches(Object argument)
+ {
+ return ObjectUtils.equals(argument, value);
+ }
+ }
+
+ private static class InstanceOfMatcher implements ArgumentMatcher
+ {
+ private final Class<?> type;
+
+ public InstanceOfMatcher(Class<?> type)
+ {
+ this.type = type;
+ }
+
+ @Override
+ public boolean matches(Object argument)
+ {
+ return type.isInstance(argument);
+ }
+ }
+
+ private static class IsNullMatcher implements ArgumentMatcher
+ {
+ @Override
+ public boolean matches(Object argument)
+ {
+ return argument == null;
+ }
+ }
+
+ private static class NotNullMatcher implements ArgumentMatcher
+ {
+ @Override
+ public boolean matches(Object argument)
+ {
+ return argument != null;
+ }
+ }
+
+ private static class SameMatcher implements ArgumentMatcher
+ {
+ private final Object ref;
+
+ public SameMatcher(Object ref)
+ {
+ this.ref = ref;
+ }
+
+ @Override
+ public boolean matches(Object argument)
{
- @Override
- public boolean matches(Object argument)
- {
- return ObjectUtils.equals(argument, value);
- }
- };
- }
-
- public static ArgumentMatcher isInstance(final Class<?> type)
- {
- return new ArgumentMatcher()
- {
- @Override
- public boolean matches(Object argument)
- {
- return type.isInstance(argument);
- }
- };
+ return argument == ref;
+ }
}
}
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java?rev=1508286&r1=1508285&r2=1508286&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Behavior.java Tue Jul 30 03:36:17 2013
@@ -17,7 +17,9 @@
package org.apache.commons.proxy2.stub;
+import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.proxy2.ObjectProvider;
+import org.apache.commons.proxy2.ProxyUtils;
import org.apache.commons.proxy2.interceptor.InterceptorUtils;
import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
import org.apache.commons.proxy2.interceptor.matcher.argument.ArgumentMatcherUtils;
@@ -40,27 +42,27 @@ public abstract class Behavior<T>
// Other Methods
//----------------------------------------------------------------------------------------------------------------------
- protected <R> R any()
+ protected <R> R any(Class<R> type)
{
- recordArgumentMatcher(ArgumentMatcherUtils.any());
+ record(ArgumentMatcherUtils.any());
return null;
}
- private void recordArgumentMatcher(ArgumentMatcher matcher)
+ private void record(ArgumentMatcher matcher)
{
trainingContext.addArgumentMatcher(matcher);
}
protected <R> R eq(R value)
{
- recordArgumentMatcher(ArgumentMatcherUtils.eq(value));
- return null;
+ record(ArgumentMatcherUtils.eq(value));
+ return value;
}
protected <R> R isInstance(Class<R> type)
{
- recordArgumentMatcher(ArgumentMatcherUtils.isInstance(type));
- return null;
+ record(ArgumentMatcherUtils.isA(type));
+ return ProxyUtils.nullValue(type);
}
void train(TrainingContext context, T stub)
@@ -69,26 +71,167 @@ public abstract class Behavior<T>
train(stub);
}
- protected <R> When<R> when(R expression)
+ protected <R> WhenObject<R> when(R expression)
+ {
+ return new WhenObject<R>();
+ }
+
+ protected WhenByteArray when(byte[] expression)
+ {
+ 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 When();
+ 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>();
+ }
+
+ protected WhenCharArray when(char[] expression)
+ {
+ return new WhenCharArray();
}
//----------------------------------------------------------------------------------------------------------------------
// Inner Classes
//----------------------------------------------------------------------------------------------------------------------
- protected class When<R>
+ protected abstract class BaseWhen<R>
+ {
+ protected Behavior<T> thenThrow(Exception e)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.throwing(e));
+ return Behavior.this;
+ }
+
+ protected Behavior<T> thenThrow(ObjectProvider<? extends Exception> provider)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.throwing(provider));
+ return Behavior.this;
+ }
+
+ protected <R> Behavior<T> thenAnswer(ObjectProvider<? extends R> provider)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.provider(provider));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenBooleanArray extends BaseWhen<boolean[]>
+ {
+ protected Behavior<T> thenReturn(boolean... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenByteArray extends BaseWhen<byte[]>
+ {
+ protected Behavior<T> thenReturn(byte... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenCharArray extends BaseWhen<char[]>
+ {
+ protected Behavior<T> thenReturn(char... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenDoubleArray extends BaseWhen<double[]>
+ {
+ protected Behavior<T> thenReturn(double... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenFloatArray extends BaseWhen<float[]>
+ {
+ protected Behavior<T> thenReturn(float... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenIntArray extends BaseWhen<int[]>
+ {
+ protected Behavior<T> thenReturn(int... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenLongArray extends BaseWhen<long[]>
+ {
+ protected Behavior<T> thenReturn(long... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenObject<R> extends BaseWhen
{
protected Behavior<T> thenReturn(R value)
{
trainingContext.setInterceptor(InterceptorUtils.constant(value));
return Behavior.this;
}
+ }
- protected Behavior<T> thenReturn(ObjectProvider<R> provider)
+ protected class WhenObjectArray<R> extends BaseWhen<R[]>
+ {
+ protected Behavior<T> thenReturn(R... values)
{
- trainingContext.setInterceptor(InterceptorUtils.provider(provider));
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
+ return Behavior.this;
+ }
+ }
+
+ protected class WhenShortArray extends BaseWhen<short[]>
+ {
+ protected Behavior<T> thenReturn(short... values)
+ {
+ trainingContext.setInterceptor(InterceptorUtils.constant(ArrayUtils.clone(values)));
return Behavior.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=1508286&r1=1508285&r2=1508286&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 Tue Jul 30 03:36:17 2013
@@ -145,7 +145,7 @@ public class TrainingContext
public boolean matches(Invocation invocation)
{
return invocation.getMethod().equals(recordedInvocation.getInvokedMethod()) &&
- Arrays.equals(invocation.getArguments(), recordedInvocation.getArguments());
+ Arrays.deepEquals(invocation.getArguments(), recordedInvocation.getArguments());
}
}
}
Copied: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java (from r1508275, commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.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?p2=commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java&p1=commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java&r1=1508275&r2=1508286&rev=1508286&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/TestStubInterceptorBuilder.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterceptorBuilderTest.java Tue Jul 30 03:36:17 2013
@@ -25,9 +25,11 @@ import org.apache.commons.proxy2.provide
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
+import java.util.Arrays;
-public class TestStubInterceptorBuilder
+import static org.junit.Assert.*;
+
+public class StubInterceptorBuilderTest
{
//----------------------------------------------------------------------------------------------------------------------
// Fields
@@ -35,6 +37,7 @@ public class TestStubInterceptorBuilder
private ProxyFactory proxyFactory;
private StubInterface target;
+ private StubInterceptorBuilder builder;
//----------------------------------------------------------------------------------------------------------------------
// Other Methods
@@ -45,53 +48,239 @@ public class TestStubInterceptorBuilder
{
this.proxyFactory = new CglibProxyFactory();
this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class);
+ this.builder = new StubInterceptorBuilder(proxyFactory);
+ }
+
+ @Test
+ public void testWithArrayParameter()
+ {
+ StubInterface proxy = createProxy(new Behavior<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 Behavior<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()
{
- StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
builder.configure(StubInterface.class, new Behavior<StubInterface>()
{
@Override
protected void train(StubInterface stub)
{
- when(stub.three(isInstance(String.class), "World")).thenReturn(ObjectProviderUtils.constant("World"));
+ when(stub.three(isInstance(String.class), "World")).thenAnswer(ObjectProviderUtils.constant("World"));
+ }
+ });
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testThrowingExceptionObject()
+ {
+ final StubInterface proxy = createProxy(new Behavior<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 Behavior<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()
{
- StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
- Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+ final StubInterface proxy = createProxy(new Behavior<StubInterface>()
{
@Override
protected void train(StubInterface stub)
{
- when(stub.one(isInstance(String.class))).thenReturn(ObjectProviderUtils.constant("World"));
+ when(stub.one(isInstance(String.class))).thenAnswer(ObjectProviderUtils.constant("World"));
}
- }).build();
-
- final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
+ });
assertEquals("World", proxy.one("Hello"));
assertEquals("World", proxy.one("Whatever"));
}
+ private StubInterface createProxy(Behavior<StubInterface> behavior)
+ {
+ Interceptor interceptor = builder.configure(StubInterface.class, behavior).build();
+
+ return proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
+ }
+
+ @Test
+ public void testWithStringArray()
+ {
+ final StubInterface proxy = createProxy(new Behavior<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 Behavior<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 Behavior<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 Behavior<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 Behavior<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 Behavior<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 Behavior<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 Behavior<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 Behavior<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()
{
- StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
- Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+ final StubInterface proxy = createProxy(new Behavior<StubInterface>()
{
@Override
protected void train(StubInterface stub)
{
when(stub.one(eq("Hello"))).thenReturn("World");
}
- }).build();
- final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
+ });
assertEquals("World", proxy.one("Hello"));
assertEquals(null, proxy.one("Whatever"));
}
@@ -99,8 +288,7 @@ public class TestStubInterceptorBuilder
@Test
public void testWithMultipleMethodsTrained()
{
- StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
- Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+ final StubInterface proxy = createProxy(new Behavior<StubInterface>()
{
@Override
protected void train(StubInterface stub)
@@ -108,9 +296,7 @@ public class TestStubInterceptorBuilder
when(stub.one("Hello")).thenReturn("World");
when(stub.two("Foo")).thenReturn("Bar");
}
- }).build();
-
- final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
+ });
assertEquals("World", proxy.one("Hello"));
assertEquals("Bar", proxy.two("Foo"));
}
@@ -118,17 +304,14 @@ public class TestStubInterceptorBuilder
@Test
public void testWithSingleMethodTrained()
{
- StubInterceptorBuilder builder = new StubInterceptorBuilder(proxyFactory);
- Interceptor interceptor = builder.configure(StubInterface.class, new Behavior<StubInterface>()
+ final StubInterface proxy = createProxy(new Behavior<StubInterface>()
{
@Override
protected void train(StubInterface stub)
{
when(stub.one("Hello")).thenReturn("World");
}
- }).build();
-
- final StubInterface proxy = proxyFactory.createInterceptorProxy(target, interceptor, StubInterface.class);
+ });
assertEquals("World", proxy.one("Hello"));
assertEquals(null, proxy.two("Whatever"));
assertEquals(null, proxy.one("Mismatch!"));
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java?rev=1508286&r1=1508285&r2=1508286&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/StubInterface.java Tue Jul 30 03:36:17 2013
@@ -26,4 +26,16 @@ public interface StubInterface
public String one(String value);
public String three(String arg1, String arg2);
public String two(String value);
+
+ public byte[] byteArray();
+ public char[] charArray();
+ public short[] shortArray();
+ public int[] intArray();
+ public long[] longArray();
+ public float[] floatArray();
+ public double[] doubleArray();
+ public boolean[] booleanArray();
+ public String[] stringArray();
+
+ public String arrayParameter(String... strings);
}