Class EventCatcher


  • public class EventCatcher
    extends java.lang.Object
    A testing class for catching and logging events.
     // 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));
     }
     
    Version:
    $Revision: 1.3 $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List eventRecords_  
      private java.lang.reflect.InvocationHandler invocationHandler_
      An inner class to handle the various events.
    • Constructor Summary

      Constructors 
      Constructor Description
      EventCatcher()
      Create a new EventCatcher.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method 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.
      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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • invocationHandler_

        private java.lang.reflect.InvocationHandler invocationHandler_
        An inner class to handle the various events.
      • eventRecords_

        private final java.util.List eventRecords_
    • Constructor Detail

      • EventCatcher

        public EventCatcher()
        Create a new EventCatcher.
    • Method Detail

      • size

        public int size()
        Deprecated.
        Use getEventCount() instead
        Return the number of events that have been caught.
        Returns:
        the number of events that have been caught.
      • getListener

        public java.lang.Object getListener​(java.lang.Class clazz)
        Return a listener object that will log all fired events. This listener should be used when you want to only listen for one kind of events on a bean. If you want to listen to all events then you should just call listenTo(Object)
         // Catch all window events
         final ObjectCatcher objectCatcher = new ObjectCatcher();
         final JFrame frame = new JFrame();
        
         frame.addWindowListener( (WindowListener)objectCatcher.getListener(WindowListener.class) );
         
        Parameters:
        clazz - The listener interface that we need to support.
        Returns:
        A listener.
      • listenTo

        public void listenTo​(java.lang.Object object)
                      throws java.lang.IllegalAccessException,
                             java.lang.reflect.InvocationTargetException
        Register the event catcher as a listener for all events that this object fires.
         // Catch all events fired by JFrame
         final ObjectCatcher objectCatcher = new ObjectCatcher();
         final JFrame frame = new JFrame();
        
         eventCatcher.listenTo(frame);
         
        Parameters:
        object - The object that we will be listening to.
        Throws:
        java.lang.IllegalAccessException - If we do not have authorization to call the respective addXXXListener() method
        java.lang.reflect.InvocationTargetException - If an exception is thrown during the call to the addXXXListener() method
      • getEventAt

        public java.util.EventObject getEventAt​(int index)
        Return the event at the specified index.
        Parameters:
        index - The index
        Returns:
        The event at that index.
      • getEventCatcherRecordAt

        public EventCatcherRecord getEventCatcherRecordAt​(int index)
        Return the record at the specified index. The record will contain the event and assorted information about the event.
        Parameters:
        index - The index
        Returns:
        The record at that index.
      • getEventCount

        public int getEventCount()
        Return the number of events that have been collected so far.
        Returns:
        The number of events.
      • getEvents

        public java.util.List getEvents()
        Return an immutable list containing all the events collected so far.
        Returns:
        A list of collected events.
      • clear

        public void clear()
        Throw away all the currently collected events.
      • assertEventsAppearEquals

        public void assertEventsAppearEquals​(java.util.List expectedEvents)
        Compare the specified events against the actual collected event to see if they appear to be the same. Refer to TestUtil.appearsEqual(Object,Object) for an explanation of "appearing" to be the same.
        Parameters:
        expectedEvents - The events that we expect to have been collected.