com.google.testing.threadtester
Class MethodRecorder<T>

java.lang.Object
  extended by com.google.testing.threadtester.MethodRecorder<T>
Type Parameters:
T - the type for which we want to create positions.

public class MethodRecorder<T>
extends Object

Provides a mechanism for creating CodePositions for a given object-under-test, using direct method calls. Sample useage is as follows:


  ClassUnderTest object = new ClassUnderTest();
  MethodRecorder recorder =
      new MethodRecorder(object)

  ClassUnderTest control = recorder.getControl();

  // Create a CodePosition corresponding to the start
  // of the call to 'aMethod' in the test object.
  CodePosition cp = recorder.atStart(control.aMethod()).position();
 
Calling getControl() returns a dummy object that is of the same class as the object-under-test. Methods invoked on the control object have no effect, but the method calls are noted by the MethodRecorder, and can then be used to create CodePositions. If the methods invoked require arguments, any argument (including null) may be passed in.

A CodePosition can also be created relative to an internal method call. Given the following implementation:

  public ClassUnderTest {
    public int aMethod() {
      int value = getValue();
      System.out.printf("Value = %d", value);
      return value;
    }
 
A CodePosition in the method aMethod, immediately after the call to printf, could be created as follows:
  ClassUnderTest control = recorder.getControl();
  PrintStream stream = recorder.createTarget(PrintStream.class);
  CodePosition cp =
    recorder.in(control.aMethod()).afterCalling(stream.printf("")).position();
 
Again, the target object is a dummy instance, and any argument may be passed in to the method calls.

The above examples can only be used when the method in question returns a value. If it does not, then the "LastMethod" variations must be used. Given the following class:

  public ClassUnderTest {
    public void voidMethod() {
      System.out.println("in void method");
    }
 
A CodePosition in the method voidMethod, immediately after the call to println, could be created as follows:
  ClassUnderTest control = recorder.getControl();
  PrintStream stream = recorder.createTarget(PrintStream.class);
  control.voidMethod();
  recorder.inLastMethod();
  stream.println("");
  recorder.afterCallingLastMethod();
  CodePosition cp = recorder.position();
 


Constructor Summary
MethodRecorder(Class<T> clss)
          Creates a new MethodRecorder for the given class.
MethodRecorder(T object)
          Creates a new MethodRecorder for the given object.
 
Method Summary
 MethodRecorder<T> afterCalling(Object result)
          Sets the state of the recorder to represent a position after a call to the last method invoked on the target object.
 MethodRecorder<T> afterCallingLastMethod()
          Sets the state of the recorder to represent a position after a call to the last method invoked on the target object.
 MethodRecorder<T> atEndOf(Object result)
          Sets the state of the recorder to represent a position at the end of the last method invoked on the control object.
 MethodRecorder<T> atEndOfLastMethod()
          Sets the state of the recorder to represent a position at the end of the last method invoked on the control object.
 MethodRecorder<T> atStartOf(Object result)
          Sets the state of the recorder to represent a position at the beginning of the last method invoked on the control object.
 MethodRecorder<T> atStartOfLastMethod()
          Sets the state of the recorder to represent a position at the beginning of the last method invoked on the control object.
 MethodRecorder<T> beforeCalling(Object result)
          Sets the state of the recorder to represent a position before a call to the last method invoked on the target object.
 MethodRecorder<T> beforeCallingLastMethod()
          Sets the state of the recorder to represent a position before a call to the last method invoked on the target object.
 Breakpoint breakpoint(Thread thread)
          Creates a Breakpoint for the current CodePosition in the given thread.
<T> T
createTarget(Class<T> clss)
          Creates a target object of the given class.
 T getControl()
          Gets the control object.
 MethodRecorder<T> in(Object result)
          Sets the state of the recorder to represent a position within the last method invoked on the control object.
 MethodRecorder<T> inLastMethod()
          Sets the state of the recorder to represent a position within the last method invoked on the control object.
 CodePosition position()
          Creates a new CodePosition corresponding to the last methods called on the control object, and optionally on a target object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodRecorder

public MethodRecorder(T object)
Creates a new MethodRecorder for the given object. Note that the object's class must be Instrumented.

See Also:
Instrumentation

MethodRecorder

public MethodRecorder(Class<T> clss)
Creates a new MethodRecorder for the given class. Note that the class must be Instrumented.

See Also:
Instrumentation
Method Detail

getControl

public T getControl()
Gets the control object. This is a dummy instance of the object passed into the constructor. Method calls made on the control object can be recorded to create CodePositions.


createTarget

public <T> T createTarget(Class<T> clss)
Creates a target object of the given class. This is a dummy instance, and can be used to record method calls.


in

public MethodRecorder<T> in(Object result)
Sets the state of the recorder to represent a position within the last method invoked on the control object. After calling this method, you must call beforeCalling(java.lang.Object) or afterCalling(java.lang.Object) before calling position.

See Also:
getControl()

inLastMethod

public MethodRecorder<T> inLastMethod()
Sets the state of the recorder to represent a position within the last method invoked on the control object. Used for void methods where chaining is impossible.

See Also:
in(java.lang.Object)

atStartOf

public MethodRecorder<T> atStartOf(Object result)
Sets the state of the recorder to represent a position at the beginning of the last method invoked on the control object.


atStartOfLastMethod

public MethodRecorder<T> atStartOfLastMethod()
Sets the state of the recorder to represent a position at the beginning of the last method invoked on the control object. Used for void methods where chaining is impossible.


atEndOf

public MethodRecorder<T> atEndOf(Object result)
Sets the state of the recorder to represent a position at the end of the last method invoked on the control object.


atEndOfLastMethod

public MethodRecorder<T> atEndOfLastMethod()
Sets the state of the recorder to represent a position at the end of the last method invoked on the control object. Used for void methods where chaining is impossible.


beforeCalling

public MethodRecorder<T> beforeCalling(Object result)
Sets the state of the recorder to represent a position before a call to the last method invoked on the target object. You must call in(java.lang.Object) before calling this method.


beforeCallingLastMethod

public MethodRecorder<T> beforeCallingLastMethod()
Sets the state of the recorder to represent a position before a call to the last method invoked on the target object. Used for void methods where chaining is impossible.

See Also:
beforeCalling(java.lang.Object)

afterCalling

public MethodRecorder<T> afterCalling(Object result)
Sets the state of the recorder to represent a position after a call to the last method invoked on the target object. You must call in(java.lang.Object) before calling this method.


afterCallingLastMethod

public MethodRecorder<T> afterCallingLastMethod()
Sets the state of the recorder to represent a position after a call to the last method invoked on the target object. Used for void methods where chaining is impossible.

See Also:
afterCalling(java.lang.Object)

position

public CodePosition position()
Creates a new CodePosition corresponding to the last methods called on the control object, and optionally on a target object. After returning a code position, the state of the recorder is reset, and other target methods must be invoked before calling this method again.

Throws:
IllegalStateException - if control and target methods have not been called.
See Also:
getControl(), createTarget(java.lang.Class)

breakpoint

public Breakpoint breakpoint(Thread thread)
Creates a Breakpoint for the current CodePosition in the given thread. Note that this method can only be called if this recorder was created with an object, not a class. See MethodRecorder(Class)

See Also:
position


Copyright © 2013. All Rights Reserved.