org.progeeks.junit
Class TestInvocationHandler

java.lang.Object
  extended by org.progeeks.junit.TestInvocationHandler
All Implemented Interfaces:
java.lang.reflect.InvocationHandler

public class TestInvocationHandler
extends java.lang.Object
implements java.lang.reflect.InvocationHandler

A proxy invocation handler for testing that can serve as any interface and records the interface's last method invocations and its parameters. Mostly intended to be used for quickly generating listeners to test classes that fire off events. The handler supports configurable objects to return based on the name of the method that was invoked.

Version:
$Revision: 1.5 $
Author:
Paul Wisneskey

Nested Class Summary
protected static class TestInvocationHandler.TestException
          Class used to denote a specific method invocation should generate an exception rather than return a result.
 
Constructor Summary
TestInvocationHandler()
           
 
Method Summary
static java.lang.Object createProxy(java.lang.Class interfaceClass)
          Convenience method that creates a test proxy class with a new generic test proxy as the invocation handler.
static java.lang.Object createProxy(java.lang.Class interfaceClass, TestInvocationHandler testProxy)
          Creates a test proxy class using the supplied generic test proxy as the invocation handler.
static TestInvocationHandler getInvocationHandler(java.lang.Object object)
          Convenience method that returns the invocation handler for the test proxy.
 java.lang.Object getLastArg(int index)
          Convenience method for returning a given argument for the last method invoked.
 java.lang.Object[] getLastArgs()
          Returns an array of the arguments for the last method invoked or an empty array if no method has been invoked.
 java.lang.reflect.Method getLastMethod()
          Returns the last method invoked.
 java.lang.String getLastMethodName()
          Convenience method that returns the name of the last method invoked or null if no method has been invoked.
 java.lang.Object getLastProxy()
          Returns the last proxy object that had a method invoked.
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
          Invocation handler method invoked when a call is made on the test proxy.
 boolean invoked()
          Returns true if a method has been invoked on the proxy.
 boolean invoked(java.lang.String methodName)
          Returns true if the method with the given method name was invoked.
 void reset()
          Resets the proxy's last method details to the state where no method is considered to have been invoked.
 void setReturnException(java.lang.String methodName, java.lang.Throwable throwable)
          Sets the exception to generate for the given method's invocation.
 void setReturnResult(java.lang.String methodName, java.lang.Object result)
          Sets the result object to return for a given method's invocation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TestInvocationHandler

public TestInvocationHandler()
Method Detail

createProxy

public static java.lang.Object createProxy(java.lang.Class interfaceClass,
                                           TestInvocationHandler testProxy)
Creates a test proxy class using the supplied generic test proxy as the invocation handler.


createProxy

public static java.lang.Object createProxy(java.lang.Class interfaceClass)
Convenience method that creates a test proxy class with a new generic test proxy as the invocation handler. If you need to obtain the handler from the test proxy, use the getInvocationHandler(Object) method.


getInvocationHandler

public static TestInvocationHandler getInvocationHandler(java.lang.Object object)
Convenience method that returns the invocation handler for the test proxy.


invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Invocation handler method invoked when a call is made on the test proxy. Just records the details of which method was invoked.

Specified by:
invoke in interface java.lang.reflect.InvocationHandler
Throws:
java.lang.Throwable

invoked

public boolean invoked()
Returns true if a method has been invoked on the proxy.


invoked

public boolean invoked(java.lang.String methodName)
Returns true if the method with the given method name was invoked.


getLastProxy

public java.lang.Object getLastProxy()
Returns the last proxy object that had a method invoked. Useful if the invocation handler is being used by multiple proxy objects.


getLastMethod

public java.lang.reflect.Method getLastMethod()
Returns the last method invoked.


getLastMethodName

public java.lang.String getLastMethodName()
Convenience method that returns the name of the last method invoked or null if no method has been invoked.


getLastArgs

public java.lang.Object[] getLastArgs()
Returns an array of the arguments for the last method invoked or an empty array if no method has been invoked.


getLastArg

public java.lang.Object getLastArg(int index)
Convenience method for returning a given argument for the last method invoked. Returns null if no method has been invoked.


reset

public void reset()
Resets the proxy's last method details to the state where no method is considered to have been invoked. Does not clear the configured return results.


setReturnResult

public void setReturnResult(java.lang.String methodName,
                            java.lang.Object result)
Sets the result object to return for a given method's invocation.


setReturnException

public void setReturnException(java.lang.String methodName,
                               java.lang.Throwable throwable)
Sets the exception to generate for the given method's invocation.



Copyright © 2002-2003 Paul Speed. All Rights Reserved.