com.google.testing.threadtester.utils
Class BlockingAnswer<T>

java.lang.Object
  extended by com.google.testing.threadtester.AbstractBreakpoint
      extended by com.google.testing.threadtester.utils.BlockingAnswer<T>
Type Parameters:
T - the type returned from this answer.
All Implemented Interfaces:
Breakpoint, ReusableBreakpoint, org.easymock.IAnswer<T>

public class BlockingAnswer<T>
extends AbstractBreakpoint
implements ReusableBreakpoint, org.easymock.IAnswer<T>

An implementation of the EasyMock IAnswer interface that can be used as a Breakpoint. When the answer is used, the calling thread will break.

Sample usage:

   BlockingAnswer answer = new BlockingAnswer(true);
   expect(mock.isActive()).andAnswer(answer);
   ...
   answer.await(); // Thread will block at 'isActive()'
 
Note that when using a mock that will be accessed in more than on thread, you must invoke EasyMock.makeThreadSafe(java.lang.Object, boolean);

Author:
alasdair.mackintosh@gmail.com (Alasdair Mackintosh)

Field Summary
 
Fields inherited from class com.google.testing.threadtester.AbstractBreakpoint
blocked, enabled, limit, numHits, thread
 
Constructor Summary
BlockingAnswer(T result)
          Creates a BlockingAnswer that returns the given value.
BlockingAnswer(T result, Thread thread)
          Creates a BlockingAnswer that returns the given value.
 
Method Summary
 T answer()
           
 Thread getThread()
          Gets the thread associated with this Breakpoint, if any.
 void setThread(Thread thread)
          Sets the thread associated with this breakpoint.
 
Methods inherited from class com.google.testing.threadtester.AbstractBreakpoint
await, disable, enable, finishWaiting, getHits, getId, getLimit, hitBreakpoint, isBlocked, isEnabled, reset, resume, resume, setHandler, setLimit, setThreadImpl, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.google.testing.threadtester.Breakpoint
await, disable, enable, getLimit, isBlocked, isEnabled, resume, resume, setHandler, setLimit
 

Constructor Detail

BlockingAnswer

public BlockingAnswer(T result)
Creates a BlockingAnswer that returns the given value. The underlying BreakPoint will be active for all threads. Note that if a thread is subsequently set by calling setThread(java.lang.Thread) then the breakpoint will become active for that specific thread.


BlockingAnswer

public BlockingAnswer(T result,
                      Thread thread)
Creates a BlockingAnswer that returns the given value. The underlying BreakPoint will be active only for the given thread.

Method Detail

answer

public T answer()
Specified by:
answer in interface org.easymock.IAnswer<T>

getThread

public Thread getThread()
Description copied from interface: Breakpoint
Gets the thread associated with this Breakpoint, if any. Some Breakpoints may only be valid for a given thread, and will not block when reached by another thread. Consult the documentation for the specific Breakpoint type for details. If this Breakpoint is not associated with a thread, returns null.

Specified by:
getThread in interface Breakpoint
Overrides:
getThread in class AbstractBreakpoint

setThread

public void setThread(Thread thread)
Description copied from interface: ReusableBreakpoint
Sets the thread associated with this breakpoint. Invoking this method will also reset other internal state. This method may be invoked before a breakpoint is used, or after a thread has called Breakpoint.await() followed by Breakpoint.resume().

Specified by:
setThread in interface ReusableBreakpoint


Copyright © 2011. All Rights Reserved.