You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2010/09/23 20:03:10 UTC
svn commit: r1000559 - in
/commons/proper/proxy/branches/version-2.0-work/stub/src:
main/java/org/apache/commons/proxy2/stub/StubInterceptor.java
test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java
Author: mbenson
Date: Thu Sep 23 18:03:09 2010
New Revision: 1000559
URL: http://svn.apache.org/viewvc?rev=1000559&view=rev
Log:
bail early if bad return values are specified while stubbing
Modified:
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java
commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java?rev=1000559&r1=1000558&r2=1000559&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java Thu Sep 23 18:03:09 2010
@@ -17,10 +17,13 @@
package org.apache.commons.proxy2.stub;
+import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
+import org.apache.commons.lang3.ClassUtils;
+import org.apache.commons.lang3.reflect.TypeUtils;
import org.apache.commons.proxy2.Interceptor;
import org.apache.commons.proxy2.Invocation;
import org.apache.commons.proxy2.ObjectProvider;
@@ -133,7 +136,7 @@ abstract class StubInterceptor implement
}
void addAnswer(Object o) {
- resultStack.push(new Answer(currentMatcher(), o));
+ resultStack.push(validAnswer(o));
}
void addThrow(ObjectProvider<? extends Throwable> throwableProvider) {
@@ -166,6 +169,26 @@ abstract class StubInterceptor implement
}
}
+ /**
+ * Validate and return the requested answer to the current invocation.
+ * @param o
+ * @return Answer
+ */
+ synchronized Answer validAnswer(Object o) {
+ if (currentInvocation == null) {
+ //fall through and let currentMatcher() throw the exception
+ } else if (o instanceof ObjectProvider<?>) {
+ // give ObjectProviders the benefit of the doubt?
+ } else {
+ Method m = currentInvocation.getInvokedMethod();
+ if (!TypeUtils.isInstance(o, m.getReturnType())) {
+ throw new IllegalArgumentException(String.format("%s does not specify a valid return value for %s", o,
+ m));
+ }
+ }
+ return new Answer(currentMatcher(), o);
+ }
+
void complete() {
this.complete = true;
}
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java?rev=1000559&r1=1000558&r2=1000559&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/test/java/org/apache/commons/proxy2/stub/AbstractStubProxyFactoryTest.java Thu Sep 23 18:03:09 2010
@@ -281,6 +281,34 @@ public abstract class AbstractStubProxyF
assertIterator(strings.iterator(), "foo", "bar", "baz");
}
+ @Test(expected=IllegalArgumentException.class)
+ public void testBadReturnValue() {
+ StubProxyFactory factory = new StubProxyFactory(createParent(), new StubConfigurer<AcceptArguments>() {
+
+ @Override
+ protected void configure(AcceptArguments stub) {
+ when((Object) stub.respondTo("x")).thenReturn(100);
+ }
+
+ });
+
+ factory.createInvokerProxy(NullInvoker.INSTANCE, AcceptArguments.class);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testBadPrimitiveReturnValue() {
+ StubProxyFactory factory = new StubProxyFactory(createParent(), new StubConfigurer<Foo>() {
+
+ @Override
+ protected void configure(Foo stub) {
+ when(stub.fooInt()).thenReturn(null);
+ }
+
+ });
+
+ factory.createInvokerProxy(NullInvoker.INSTANCE, Foo.class);
+ }
+
private <T> void assertIterator(Iterator<T> iter, T... expected) {
for (T t : expected) {
assertTrue(iter.hasNext());