@SuppressWarnings({"rawtypes", "unchecked"}) public void testForwarding() { new ForwardingWrapperTester() .testForwarding( Set.class, new Function<Set, Set>() { @Override public Set apply(Set delegate) { return wrap(delegate); } }); }
checkNotNull(wrapperFunction); checkArgument(interfaceType.isInterface(), "%s isn't an interface", interfaceType); Method[] methods = getMostConcreteMethods(interfaceType); AccessibleObject.setAccessible(methods, true); for (Method method : methods) { continue; testSuccessfulForwarding(interfaceType, method, wrapperFunction); testExceptionPropagation(interfaceType, method, wrapperFunction); testEquals(interfaceType, wrapperFunction); testToString(interfaceType, wrapperFunction);
private static <T> void testExceptionPropagation( Class<T> interfaceType, Method method, Function<? super T, ? extends T> wrapperFunction) { final RuntimeException exception = new RuntimeException(); T proxy = Reflection.newProxy( interfaceType, new AbstractInvocationHandler() { @Override protected Object handleInvocation(Object p, Method m, Object[] args) throws Throwable { throw exception; } }); T wrapper = wrapperFunction.apply(proxy); try { method.invoke(wrapper, getParameterValues(method)); fail(method + " failed to throw exception as is."); } catch (InvocationTargetException e) { if (exception != e.getCause()) { throw new RuntimeException(e); } } catch (IllegalAccessException e) { throw new AssertionError(e); } }
public void testEqualsAndHashCodeForwarded() { tester.includingEquals(); tester.testForwarding( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public boolean equals(Object o) { if (o instanceof ForwardingRunnable) { ForwardingRunnable that = (ForwardingRunnable) o; return runnable.equals(that.runnable); } return false; } @Override public int hashCode() { return runnable.hashCode(); } }; } }); }
public void testExplicitEqualsAndHashCodeDelegatedWhenExplicitlyAsked() { try { new ForwardingWrapperTester() .includingEquals() .testForwarding(Equals.class, NoDelegateToEquals.WRAPPER); } catch (AssertionFailedError expected) { return; } fail("Should have failed"); }
public void testChainingCalls() { tester.testForwarding( ChainingCalls.class, new Function<ChainingCalls, ChainingCalls>() { @Override public ChainingCalls apply(ChainingCalls delegate) { return new ForwardingChainingCalls(delegate); } }); } }
public void testNulls() { new NullPointerTester() .setDefault(Class.class, Runnable.class) .testAllPublicInstanceMethods(new ForwardingWrapperTester()); }
public void testFailsToForwardEquals() { tester.includingEquals(); assertFailure( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public int hashCode() { return runnable.hashCode(); } }; } }, "Runnable"); }
public void testExplicitEqualsAndHashCodeDelegatedWhenExplicitlyAsked() { try { new ForwardingWrapperTester() .includingEquals() .testForwarding(Equals.class, NoDelegateToEquals.WRAPPER); } catch (AssertionFailedError expected) { return; } fail("Should have failed"); }
public void testVoidMethodForwarding() { tester.testForwarding( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable); } }); }
public void testEqualsAndHashCodeForwarded() { tester.includingEquals(); tester.testForwarding(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public boolean equals(Object o) { if (o instanceof ForwardingRunnable) { ForwardingRunnable that = (ForwardingRunnable) o; return runnable.equals(that.runnable); } return false; } @Override public int hashCode() { return runnable.hashCode(); } }; } }); }
public void testNulls() { new NullPointerTester() .setDefault(Class.class, Runnable.class) .testAllPublicInstanceMethods(new ForwardingWrapperTester()); }
public void testFailsToForwardHashCode() { tester.includingEquals(); assertFailure( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @SuppressWarnings("EqualsHashCode") @Override public boolean equals(Object o) { if (o instanceof ForwardingRunnable) { ForwardingRunnable that = (ForwardingRunnable) o; return runnable.equals(that.runnable); } return false; } }; } }, "Runnable"); }
@SuppressWarnings({"rawtypes", "unchecked"}) public void testForwarding() { new ForwardingWrapperTester() .testForwarding( SortedMultiset.class, new Function<SortedMultiset, SortedMultiset>() { @Override public SortedMultiset apply(SortedMultiset delegate) { return wrap(delegate); } }); }
checkNotNull(wrapperFunction); checkArgument(interfaceType.isInterface(), "%s isn't an interface", interfaceType); Method[] methods = getMostConcreteMethods(interfaceType); AccessibleObject.setAccessible(methods, true); for (Method method : methods) { continue; testSuccessfulForwarding(interfaceType, method, wrapperFunction); testExceptionPropagation(interfaceType, method, wrapperFunction); testEquals(interfaceType, wrapperFunction); testToString(interfaceType, wrapperFunction);
public void testExplicitEqualsAndHashCodeDelegatedWhenExplicitlyAsked() { try { new ForwardingWrapperTester() .includingEquals() .testForwarding(Equals.class, NoDelegateToEquals.WRAPPER); } catch (AssertionError expected) { return; } fail("Should have failed"); }
public void testToStringForwarding() { tester.testForwarding( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public String toString() { return runnable.toString(); } }; } }); }
public void testEqualsAndHashCodeForwarded() { tester.includingEquals(); tester.testForwarding( Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public boolean equals(Object o) { if (o instanceof ForwardingRunnable) { ForwardingRunnable that = (ForwardingRunnable) o; return runnable.equals(that.runnable); } return false; } @Override public int hashCode() { return runnable.hashCode(); } }; } }); }
public void testNulls() { new NullPointerTester() .setDefault(Class.class, Runnable.class) .testAllPublicInstanceMethods(new ForwardingWrapperTester()); }
public void testFailsToForwardEquals() { tester.includingEquals(); assertFailure(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public int hashCode() { return runnable.hashCode(); } }; } }, "Runnable"); }