|
Java example source code file (ForwardingWrapperTesterTest.java)
The ForwardingWrapperTesterTest.java Java example source code/* * Copyright (C) 2012 The Guava Authors * * Licensed 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 com.google.common.testing.anotherpackage; import static com.google.common.truth.Truth.assertThat; import com.google.common.base.Equivalence; import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.collect.Ordering; import com.google.common.primitives.UnsignedInteger; import com.google.common.primitives.UnsignedLong; import com.google.common.testing.ForwardingWrapperTester; import com.google.common.testing.NullPointerTester; import junit.framework.AssertionFailedError; import junit.framework.TestCase; import java.io.InputStream; import java.nio.charset.Charset; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; /** * Tests for {@link ForwardingWrapperTester}. Live in a different package to detect reflection * access issues, if any. * * @author Ben Yu */ public class ForwardingWrapperTesterTest extends TestCase { private final ForwardingWrapperTester tester = new ForwardingWrapperTester(); public void testGoodForwarder() { tester.testForwarding(Arithmetic.class, new Function<Arithmetic, Arithmetic>() { @Override public Arithmetic apply(Arithmetic arithmetic) { return new ForwardingArithmetic(arithmetic); } }); tester.testForwarding(ParameterTypesDifferent.class, new Function<ParameterTypesDifferent, ParameterTypesDifferent>() { @Override public ParameterTypesDifferent apply(ParameterTypesDifferent delegate) { return new ParameterTypesDifferentForwarder(delegate); } }); } public void testVoidMethodForwarding() { tester.testForwarding(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable); } }); } 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 testFailsToForwardToString() { assertFailure(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public String toString() { return ""; } }; } }, "toString()"); } public void testFailsToForwardHashCode() { tester.includingEquals(); assertFailure(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; } }; } }, "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 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 testFailsToForward() { assertFailure(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable runnable) { return new ForwardingRunnable(runnable) { @Override public void run() {} }; } }, "run()", "Failed to forward"); } public void testRedundantForwarding() { assertFailure(Runnable.class, new Function<Runnable, Runnable>() { @Override public Runnable apply(final Runnable runnable) { return new Runnable() { @Override public void run() { runnable.run(); runnable.run(); } }; } }, "run()", "invoked more than once"); } public void testFailsToForwardParameters() { assertFailure(Adder.class, new Function<Adder, Adder>() { @Override public Adder apply(Adder adder) { return new FailsToForwardParameters(adder); } }, "add(", "Parameter #0"); } public void testForwardsToTheWrongMethod() { assertFailure(Arithmetic.class, new Function<Arithmetic, Arithmetic>() { @Override public Arithmetic apply(Arithmetic adder) { return new ForwardsToTheWrongMethod(adder); } }, "minus"); } public void testFailsToForwardReturnValue() { assertFailure(Adder.class, new Function<Adder, Adder>() { @Override public Adder apply(Adder adder) { return new FailsToForwardReturnValue(adder); } }, "add(", "Return value"); } public void testFailsToPropagateException() { assertFailure(Adder.class, new Function<Adder, Adder>() { @Override public Adder apply(Adder adder) { return new FailsToPropagageException(adder); } }, "add(", "exception"); } public void testNotInterfaceType() { try { new ForwardingWrapperTester().testForwarding(String.class, Functions.<String>identity()); fail(); } catch (IllegalArgumentException expected) {} } public void testNulls() { new NullPointerTester() .setDefault(Class.class, Runnable.class) .testAllPublicInstanceMethods(new ForwardingWrapperTester()); } private <T> void assertFailure( Class<T> interfaceType, Function Other Java examples (source code examples)Here is a short list of links related to this Java ForwardingWrapperTesterTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.