Class ComparingInvocationHandler<T>
- All Implemented Interfaces:
InvocationHandler
- Direct Known Subclasses:
ComparatorKeyring
Given an interface, and two instances of that interface (the "original" instance we know works, and a "second" instance we wish to test), creates an InvocationHandler that acts like an exact proxy to the "original" instance.
In addition, it will log any differences in return values or thrown exception between the "original" and "second" instances.
This can be used to create an exact proxy to the original instance that can be placed in any code, while live testing the second instance.
-
Constructor Summary
ConstructorDescriptionComparingInvocationHandler
(Class<T> interfaceClass, T actualImplementation, T secondImplementation) Creates a new InvocationHandler for the given interface. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
compareResults
(Method method, Object actual, Object second) Checks whether the method results are as similar as we expect.protected boolean
compareThrown
(Method method, Throwable actual, Throwable second) Checks whether the thrown exceptions are as similar as we expect.final Object
protected abstract void
Called when there was a difference between the implementations.final T
Returns the proxy to the actualImplementation.protected String
stringifyResult
(Method method, Object object) Implements toString for specific types.protected String
stringifyThrown
(Method method, Throwable throwable) Implements toString for thrown exceptions.
-
Constructor Details
-
ComparingInvocationHandler
public ComparingInvocationHandler(Class<T> interfaceClass, T actualImplementation, T secondImplementation) Creates a new InvocationHandler for the given interface.- Parameters:
interfaceClass
- the interface we want to create.actualImplementation
- the resulting proxy will be an exact proxy to this objectsecondImplementation
- Only used to log difference compared to actualImplementation. Otherwise has no effect on the resulting proxy's behavior.
-
-
Method Details
-
makeProxy
Returns the proxy to the actualImplementation.The return value is a drop-in replacement to the actualImplementation, but will log any difference with the secondImplementation during normal execution.
-
log
Called when there was a difference between the implementations.- Parameters:
method
- the method where the difference was foundmessage
- human readable description of the difference found
-
stringifyResult
Implements toString for specific types.By default objects are logged using their .toString. If .toString isn't implemented for some relevant classes (or if we want to use a different version), override this method with the desired implementation.
- Parameters:
method
- the method whose return value is givenobject
- the object returned by a call to method
-
compareResults
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 with the desired implementation.
- Parameters:
method
- the method whose return value is givenactual
- the object returned by a call to method for the "actual" implementationsecond
- the object returned by a call to method for the "second" implementation
-
compareThrown
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.
- Parameters:
method
- the method whose return value is givenactual
- the exception thrown by a call to method for the "actual" implementationsecond
- the exception thrown by a call to method for the "second" implementation
-
stringifyThrown
Implements toString for thrown exceptions.By default exceptions are logged using their .toString. If more data is needed (part of stack trace for example), override this method with the desired implementation.
- Parameters:
method
- the method whose return value is giventhrowable
- the exception thrown by a call to method
-
invoke
- Specified by:
invoke
in interfaceInvocationHandler
- Throws:
Throwable
-