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/03 18:11:50 UTC
svn commit: r1510022 - in
/commons/proper/proxy/branches/version-2.0-work/core/src:
main/java/org/apache/commons/proxy2/interceptor/
test/java/org/apache/commons/proxy2/interceptor/
test/java/org/apache/commons/proxy2/util/
Author: jcarman
Date: Sat Aug 3 16:11:49 2013
New Revision: 1510022
URL: http://svn.apache.org/r1510022
Log:
Improving test coverage.
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/test/java/org/apache/commons/proxy2/interceptor/InterceptorUtilsTest.java
commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/AbstractTestCase.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=1510022&r1=1510021&r2=1510022&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 Sat Aug 3 16:11:49 2013
@@ -27,21 +27,45 @@ public final class InterceptorUtils
// Static Methods
//----------------------------------------------------------------------------------------------------------------------
+ /**
+ * Creates an {@link Interceptor} which always returns a constant value (for all methods).
+ * @param value the constant
+ * @return an {@link Interceptor} which always returns a constant value (for all methods)
+ */
public static Interceptor constant(Object value)
{
return new ObjectProviderInterceptor(ObjectProviderUtils.constant(value));
}
+ /**
+ * Creates an {@link Interceptor} which returns the resulting object from an
+ * object provider (for all methods).
+ * @param provider the object provider
+ * @return an {@link Interceptor} which returns the resulting object from an
+ * object provider (for all methods)
+ */
public static Interceptor provider(ObjectProvider<?> provider)
{
return new ObjectProviderInterceptor(provider);
}
+ /**
+ * Creates an {@link Interceptor} which throws a specific exception (for all methods).
+ * @param e the exception
+ * @return an {@link Interceptor} which throws a specific exception (for all methods)
+ */
public static Interceptor throwing(Exception e)
{
return new ThrowingInterceptor(ObjectProviderUtils.constant(e));
}
+ /**
+ * Creates an {@link Interceptor} which throws the exception provided by an object
+ * provider (for all methods).
+ * @param provider the object provider
+ * @return an {@link Interceptor} which throws the exception provided by an object
+ * provider (for all methods)
+ */
public static Interceptor throwing(ObjectProvider<? extends Exception> provider)
{
return new ThrowingInterceptor(provider);
Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/InterceptorUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/InterceptorUtilsTest.java?rev=1510022&r1=1510021&r2=1510022&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/InterceptorUtilsTest.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/InterceptorUtilsTest.java Sat Aug 3 16:11:49 2013
@@ -19,6 +19,7 @@ package org.apache.commons.proxy2.interc
import org.apache.commons.proxy2.Interceptor;
import org.apache.commons.proxy2.Invocation;
+import org.apache.commons.proxy2.provider.ObjectProviderUtils;
import org.apache.commons.proxy2.util.AbstractTestCase;
import org.apache.commons.proxy2.util.Echo;
import org.apache.commons.proxy2.util.MockInvocation;
@@ -34,8 +35,32 @@ public class InterceptorUtilsTest extend
public void testConstant() throws Throwable
{
Interceptor interceptor = InterceptorUtils.constant("Hello!");
- Method method = Echo.class.getMethod("echoBack", String.class);
- Invocation invocation = new MockInvocation(method, "World!");
+ Invocation invocation = mockInvocation(Echo.class, "echoBack", String.class).withArguments("World!").build();
assertEquals("Hello!", interceptor.intercept(invocation));
}
+
+ @Test
+ public void testProvider() throws Throwable
+ {
+ Interceptor interceptor = InterceptorUtils.provider(ObjectProviderUtils.constant("Foo!"));
+ Invocation invocation = mockInvocation(Echo.class, "echoBack", String.class).withArguments("World!").build();
+ assertEquals("Foo!", interceptor.intercept(invocation));
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testThrowingExceptionObject() throws Throwable
+ {
+ Interceptor interceptor = InterceptorUtils.throwing(new RuntimeException("Oops!"));
+ Invocation invocation = mockInvocation(Echo.class, "echoBack", String.class).withArguments("World!").build();
+ interceptor.intercept(invocation);
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testThrowingProvidedException() throws Throwable
+ {
+ Interceptor interceptor = InterceptorUtils.throwing(ObjectProviderUtils.constant(new RuntimeException("Oops!")));
+ Invocation invocation = mockInvocation(Echo.class, "echoBack", String.class).withArguments("World!").build();
+ interceptor.intercept(invocation);
+ }
+
}
Modified: commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/AbstractTestCase.java?rev=1510022&r1=1510021&r2=1510022&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/AbstractTestCase.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/util/AbstractTestCase.java Sat Aug 3 16:11:49 2013
@@ -1,8 +1,13 @@
package org.apache.commons.proxy2.util;
import org.apache.commons.lang3.SerializationUtils;
+import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.builder.Builder;
+import org.apache.commons.proxy2.Invocation;
+import org.apache.commons.proxy2.ProxyUtils;
import java.io.Serializable;
+import java.lang.reflect.Method;
import static org.junit.Assert.*;
@@ -21,4 +26,46 @@ public abstract class AbstractTestCase
assertTrue(o instanceof Serializable);
SerializationUtils.clone(( Serializable ) o);
}
+
+ protected MockInvocationBuilder mockInvocation(Class<?> type, String name, Class<?>... argumentTypes)
+ {
+ try
+ {
+ return new MockInvocationBuilder(Validate.notNull(type).getMethod(name, argumentTypes));
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new IllegalArgumentException("Method not found.", e);
+ }
+ }
+
+ protected static final class MockInvocationBuilder implements Builder<Invocation>
+ {
+ private final Method method;
+ private Object[] arguments = ProxyUtils.EMPTY_ARGUMENTS;
+ private Object returnValue = null;
+
+ public MockInvocationBuilder(Method method)
+ {
+ this.method = method;
+ }
+
+ public MockInvocationBuilder withArguments(Object... arguments)
+ {
+ this.arguments = arguments;
+ return this;
+ }
+
+ public MockInvocationBuilder returning(Object value)
+ {
+ this.returnValue = value;
+ return this;
+ }
+
+ @Override
+ public Invocation build()
+ {
+ return new MockInvocation(method, returnValue, arguments);
+ }
+ }
}