1 package org.hamcrest.object; 2 3 import org.hamcrest.AbstractMatcherTest; 4 import org.hamcrest.Matcher; 5 6 import java.util.EventObject; 7 8 import static org.hamcrest.MatcherAssert.assertThat; 9 import static org.hamcrest.object.IsEventFrom.eventFrom; 10 11 12 public class IsEventFromTest extends AbstractMatcherTest { 13 14 @Override createMatcher()15 protected Matcher<?> createMatcher() { 16 return eventFrom(null); 17 } 18 testEvaluatesToTrueIfArgumentIsAnEventObjectFiredByASpecifiedSource()19 public void testEvaluatesToTrueIfArgumentIsAnEventObjectFiredByASpecifiedSource() { 20 Object o = "Source"; 21 EventObject ev = new EventObject(o); 22 EventObject ev2 = new EventObject("source 2"); 23 24 Matcher<EventObject> isEventMatcher = eventFrom(o); 25 26 assertThat(ev, isEventMatcher); 27 assertMismatchDescription("source was \"source 2\"", isEventMatcher, ev2); 28 } 29 30 private static class DerivedEvent extends EventObject { 31 private static final long serialVersionUID = 1L; 32 DerivedEvent(Object source)33 public DerivedEvent(Object source) { 34 super(source); 35 } 36 } 37 testCanTestForSpecificEventClasses()38 public void testCanTestForSpecificEventClasses() { 39 Object o = new Object(); 40 DerivedEvent goodEv = new DerivedEvent(o); 41 DerivedEvent wrongSource = new DerivedEvent("wrong source"); 42 EventObject wrongType = new EventObject(o); 43 EventObject wrongSourceAndType = new EventObject(new Object()); 44 45 Matcher<EventObject> isEventMatcher = IsEventFrom.eventFrom(DerivedEvent.class, o); 46 47 assertThat(goodEv, isEventMatcher); 48 assertMismatchDescription("source was \"wrong source\"", isEventMatcher, wrongSource); 49 assertMismatchDescription("item type was java.util.EventObject", isEventMatcher, wrongType); 50 assertMismatchDescription("item type was java.util.EventObject", isEventMatcher, wrongSourceAndType); 51 } 52 } 53