public class EventCatcher
extends java.lang.Object
// Catch all events fired by JFrame
final JFrame frame = new JFrame();
final EventCatcher eventCatcher = new EventCatcher();
eventCatcher.listenTo(frame);
frame.show();
for( int i=0; i<eventCatcher.size(); i++ ) {
System.out.println(eventCatcher.getEventAt(i));
}
| Modifier and Type | Field and Description |
|---|---|
private java.util.List |
eventRecords_ |
private java.lang.reflect.InvocationHandler |
invocationHandler_
An inner class to handle the various events.
|
| Constructor and Description |
|---|
EventCatcher()
Create a new EventCatcher.
|
| Modifier and Type | Method and Description |
|---|---|
void |
assertEventsAppearEquals(java.util.List expectedEvents)
Compare the specified events against the actual collected event to see if they
appear to be the same.
|
void |
clear()
Throw away all the currently collected events.
|
EventCatcherRecord |
get(int index)
Deprecated.
Use
getEventCatcherRecordAt(int) instead |
java.util.EventObject |
getEventAt(int index)
Return the event at the specified index.
|
EventCatcherRecord |
getEventCatcherRecordAt(int index)
Return the record at the specified index.
|
int |
getEventCount()
Return the number of events that have been collected so far.
|
java.util.List |
getEvents()
Return an immutable list containing all the events collected so far.
|
java.lang.Object |
getListener(java.lang.Class clazz)
Return a listener object that will log all fired events.
|
void |
listenTo(java.lang.Object object)
Register the event catcher as a listener for all events that this object fires.
|
int |
size()
Deprecated.
Use
getEventCount() instead |
private java.lang.reflect.InvocationHandler invocationHandler_
private final java.util.List eventRecords_
public EventCatcherRecord get(int index)
getEventCatcherRecordAt(int) insteadindex - The index.public int size()
getEventCount() insteadpublic java.lang.Object getListener(java.lang.Class clazz)
listenTo(Object)
// Catch all window events final ObjectCatcher objectCatcher = new ObjectCatcher(); final JFrame frame = new JFrame(); frame.addWindowListener( (WindowListener)objectCatcher.getListener(WindowListener.class) );
clazz - The listener interface that we need to support.public void listenTo(java.lang.Object object)
throws java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
// Catch all events fired by JFrame final ObjectCatcher objectCatcher = new ObjectCatcher(); final JFrame frame = new JFrame(); eventCatcher.listenTo(frame);
object - The object that we will be listening to.java.lang.IllegalAccessException - If we do not have authorization to call
the respective addXXXListener() methodjava.lang.reflect.InvocationTargetException - If an exception is thrown during the
call to the addXXXListener() methodpublic java.util.EventObject getEventAt(int index)
index - The indexpublic EventCatcherRecord getEventCatcherRecordAt(int index)
index - The indexpublic int getEventCount()
public java.util.List getEvents()
public void clear()
public void assertEventsAppearEquals(java.util.List expectedEvents)
TestUtil.appearsEqual(Object,Object)
for an explanation of "appearing" to be the same.expectedEvents - The events that we expect to have been collected.