From e6a5083b55e751bb89f09c418cdc57d5fe0408c5 Mon Sep 17 00:00:00 2001 From: guyben Date: Tue, 14 Mar 2017 17:14:58 -0700 Subject: [PATCH] Rename .toString and .equals to be less confusing. There will now be "resultToString" and "resultEquals" to stringify / compare the method results. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=150135475 --- .../util/ComparingInvocationHandler.java | 28 +++++++++---------- .../util/ComparingInvocationHandlerTest.java | 17 +++++------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/java/google/registry/util/ComparingInvocationHandler.java b/java/google/registry/util/ComparingInvocationHandler.java index 02a8e8a5e..b1b031ec2 100644 --- a/java/google/registry/util/ComparingInvocationHandler.java +++ b/java/google/registry/util/ComparingInvocationHandler.java @@ -83,14 +83,14 @@ public abstract class ComparingInvocationHandler implements InvocationHandler * @param method the method whose return value is given * @param object the object returned by a call to method */ - protected String toString( + protected String stringifyResult( @SuppressWarnings("unused") Method method, @Nullable Object object) { return String.valueOf(object); } /** - * Implements equals for specific types. + * Checks whether the method results are as similar as we expect. * *

By default objects are compared using their .equals. If .equals isn't implemented for some * relevant classes (or if we want change what is considered "not equal"), override this method @@ -100,7 +100,7 @@ public abstract class ComparingInvocationHandler implements InvocationHandler * @param actual the object returned by a call to method for the "actual" implementation * @param second the object returned by a call to method for the "second" implementation */ - protected boolean equals( + protected boolean compareResults( @SuppressWarnings("unused") Method method, @Nullable Object actual, @Nullable Object second) { @@ -108,7 +108,7 @@ public abstract class ComparingInvocationHandler implements InvocationHandler } /** - * Implements equals for thrown exceptions. + * Checks whether the thrown exceptions are as similar as we expect. * *

By default this returns 'true' for any input: all we check by default is that both * implementations threw something. Override if you need to actually compare both throwables. @@ -117,7 +117,7 @@ public abstract class ComparingInvocationHandler implements InvocationHandler * @param actual the exception thrown by a call to method for the "actual" implementation * @param second the exception thrown by a call to method for the "second" implementation */ - protected boolean exceptionEquals( + protected boolean compareException( @SuppressWarnings("unused") Method method, Throwable actual, Throwable second) { @@ -133,7 +133,7 @@ public abstract class ComparingInvocationHandler implements InvocationHandler * @param method the method whose return value is given * @param exception the exception thrown by a call to method */ - protected String exceptionToString( + protected String stringifyException( @SuppressWarnings("unused") Method method, Throwable exception) { return exception.toString(); @@ -159,35 +159,35 @@ public abstract class ComparingInvocationHandler implements InvocationHandler // First compare the two implementations' result, and log any differences: if (actualException != null && secondException != null) { - if (!exceptionEquals(method, actualException, secondException)) { + if (!compareException(method, actualException, secondException)) { log( method, String.format( "Both implementations threw, but got different exceptions! '%s' vs '%s'", - exceptionToString(method, actualException), - exceptionToString(method, secondException))); + stringifyException(method, actualException), + stringifyException(method, secondException))); } } else if (actualException != null) { log( method, String.format( "Only actual implementation threw exception: %s", - exceptionToString(method, actualException))); + stringifyException(method, actualException))); } else if (secondException != null) { log( method, String.format( "Only second implementation threw exception: %s", - exceptionToString(method, secondException))); + stringifyException(method, secondException))); } else { // Neither threw exceptions - we compare the results - if (!equals(method, actualResult, secondResult)) { + if (!compareResults(method, actualResult, secondResult)) { log( method, String.format( "Got different results! '%s' vs '%s'", - toString(method, actualResult), - toString(method, secondResult))); + stringifyResult(method, actualResult), + stringifyResult(method, secondResult))); } } diff --git a/javatests/google/registry/util/ComparingInvocationHandlerTest.java b/javatests/google/registry/util/ComparingInvocationHandlerTest.java index 027435d6f..ce6dfcad6 100644 --- a/javatests/google/registry/util/ComparingInvocationHandlerTest.java +++ b/javatests/google/registry/util/ComparingInvocationHandlerTest.java @@ -79,26 +79,27 @@ public class ComparingInvocationHandlerTest { log.add(String.format("%s: %s", method.getName(), message)); } - @Override protected boolean equals(Method method, @Nullable Object a, @Nullable Object b) { + @Override protected boolean compareResults( + Method method, @Nullable Object a, @Nullable Object b) { if (method.getReturnType().equals(Dummy.class)) { return dummyEqualsResult; } - return super.equals(method, a, b); + return super.compareResults(method, a, b); } - @Override protected String toString(Method method, @Nullable Object a) { + @Override protected String stringifyResult(Method method, @Nullable Object a) { if (method.getReturnType().equals(Dummy.class)) { return "dummy"; } - return super.toString(method, a); + return super.stringifyResult(method, a); } - @Override protected boolean exceptionEquals(Method method, Throwable a, Throwable b) { - return exceptionEqualsResult && super.exceptionEquals(method, a, b); + @Override protected boolean compareException(Method method, Throwable a, Throwable b) { + return exceptionEqualsResult && super.compareException(method, a, b); } - @Override protected String exceptionToString(Method method, Throwable a) { - return String.format("testException(%s)", super.exceptionToString(method, a)); + @Override protected String stringifyException(Method method, Throwable a) { + return String.format("testException(%s)", super.stringifyException(method, a)); } }