Home | History | Annotate | Download | only in object
      1 package org.hamcrest.object;
      2 
      3 import org.hamcrest.Description;
      4 import org.hamcrest.Matcher;
      5 import org.hamcrest.TypeSafeDiagnosingMatcher;
      6 
      7 import java.util.EventObject;
      8 
      9 
     10 /**
     11  * Tests if the value is an event announced by a specific object.
     12  */
     13 public class IsEventFrom extends TypeSafeDiagnosingMatcher<EventObject> {
     14     private final Class<?> eventClass;
     15     private final Object source;
     16 
     17     public IsEventFrom(Class<?> eventClass, Object source) {
     18         this.eventClass = eventClass;
     19         this.source = source;
     20     }
     21 
     22     @Override
     23     public boolean matchesSafely(EventObject item, Description mismatchDescription) {
     24         if (!eventClass.isInstance(item)) {
     25           mismatchDescription.appendText("item type was " + item.getClass().getName());
     26           return false;
     27         }
     28 
     29         if (!eventHasSameSource(item)) {
     30           mismatchDescription.appendText("source was ").appendValue(item.getSource());
     31           return false;
     32         }
     33         return true;
     34     }
     35 
     36 
     37     private boolean eventHasSameSource(EventObject ev) {
     38         return ev.getSource() == source;
     39     }
     40 
     41     @Override
     42     public void describeTo(Description description) {
     43         description.appendText("an event of type ")
     44                 .appendText(eventClass.getName())
     45                 .appendText(" from ")
     46                 .appendValue(source);
     47     }
     48 
     49     /**
     50      * Creates a matcher of {@link java.util.EventObject} that matches any object
     51      * derived from <var>eventClass</var> announced by <var>source</var>.
     52      * For example:
     53      * <pre>assertThat(myEvent, is(eventFrom(PropertyChangeEvent.class, myBean)))</pre>
     54      *
     55      * @param eventClass
     56      *     the class of the event to match on
     57      * @param source
     58      *     the source of the event
     59      */
     60     public static Matcher<EventObject> eventFrom(Class<? extends EventObject> eventClass, Object source) {
     61         return new IsEventFrom(eventClass, source);
     62     }
     63 
     64     /**
     65      * Creates a matcher of {@link java.util.EventObject} that matches any EventObject
     66      * announced by <var>source</var>.
     67      * For example:
     68      * <pre>assertThat(myEvent, is(eventFrom(myBean)))</pre>
     69      *
     70      * @param source
     71      *     the source of the event
     72      */
     73     public static Matcher<EventObject> eventFrom(Object source) {
     74         return eventFrom(EventObject.class, source);
     75     }
     76 }
     77