Skip to content

Commit e8ede1d

Browse files
author
joeretro
committed
Java5ified IsEventFrom matcher.
1 parent b8bb8dc commit e8ede1d

2 files changed

Lines changed: 42 additions & 39 deletions

File tree

‎src/library/org/hamcrest/object/IsEventFrom.java‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,26 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
import java.util.EventObject;
910

1011

1112
/**
1213
* Tests if the value is an event announced by a specific object.
1314
*/
14-
public class IsEventFrom implements Matcher {
15+
public class IsEventFrom implements Matcher<EventObject> {
1516
private final Class eventClass;
1617
private final Object source;
1718

18-
/**
19-
* Constructs an IsEventFrom predicate that returns true for any object
20-
* derived from {@link java.util.EventObject}announced by <var>source
21-
* </var>.
22-
*/
23-
public IsEventFrom(Object source) {
24-
this(EventObject.class, source);
25-
}
26-
27-
/**
28-
* Constructs an IsEventFrom predicate that returns true for any object
29-
* derived from <var>event_class</var> announced by <var>source</var>.
30-
*/
3119
public IsEventFrom(Class eventClass, Object source) {
3220
this.eventClass = eventClass;
3321
this.source = source;
3422
}
3523

36-
public boolean match(Object o) {
37-
return (o instanceof EventObject)
38-
&& eventClass.isInstance(o)
39-
&& eventHasSameSource((EventObject) o);
24+
public boolean match(EventObject item) {
25+
return eventClass.isInstance(item)
26+
&& eventHasSameSource(item);
4027
}
4128

4229
private boolean eventHasSameSource(EventObject ev) {
@@ -49,4 +36,23 @@ public void describeTo(Description description) {
4936
.appendText(" from ")
5037
.appendValue(source);
5138
}
39+
40+
/**
41+
* Constructs an IsEventFrom Matcher that returns true for any object
42+
* derived from <var>eventClass</var> announced by <var>source</var>.
43+
*/
44+
@Factory
45+
public static Matcher<EventObject> isEventFrom(Class<? extends EventObject> eventClass, Object source) {
46+
return new IsEventFrom(eventClass, source);
47+
}
48+
49+
/**
50+
* Constructs an IsEventFrom Matcher that returns true for any object
51+
* derived from {@link java.util.EventObject} announced by <var>source
52+
* </var>.
53+
*/
54+
@Factory
55+
public static Matcher<EventObject> isEventFrom(Object source) {
56+
return isEventFrom(EventObject.class, source);
57+
}
5258
}

‎src/unit-test/org/hamcrest/object/IsEventFromTest.java‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
*/
33
package org.hamcrest.object;
44

5+
import static org.hamcrest.core.IsNot.not;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.object.IsEventFrom.isEventFrom;
58
import org.hamcrest.AbstractMatcherTest;
9+
import org.hamcrest.Matcher;
610

711
import java.util.EventObject;
812

@@ -13,13 +17,10 @@ public void testEvaluatesToTrueIfArgumentIsAnEventObjectFiredByASpecifiedSource(
1317
EventObject ev = new EventObject(o);
1418
EventObject ev2 = new EventObject(new Object());
1519

16-
IsEventFrom p = new IsEventFrom(o);
20+
Matcher<EventObject> isEventMatcher = isEventFrom(o);
1721

18-
assertTrue(p.match(ev));
19-
assertTrue("p should match to false for an event not from o",
20-
!p.match(ev2));
21-
assertTrue("p should match to false for objects that are not events",
22-
!p.match(o));
22+
assertThat(ev, isEventMatcher);
23+
assertThat(ev2, not(isEventMatcher));
2324
}
2425

2526
private static class DerivedEvent extends EventObject {
@@ -32,20 +33,16 @@ public DerivedEvent(Object source) {
3233

3334
public void testCanTestForSpecificEventClasses() {
3435
Object o = new Object();
35-
DerivedEvent good_ev = new DerivedEvent(o);
36-
DerivedEvent wrong_source = new DerivedEvent(new Object());
37-
EventObject wrong_type = new EventObject(o);
38-
EventObject wrong_source_and_type = new EventObject(new Object());
39-
40-
IsEventFrom p = new IsEventFrom(DerivedEvent.class, o);
41-
42-
assertTrue(p.match(good_ev));
43-
assertTrue("p should match to false for an event not from o",
44-
!p.match(wrong_source));
45-
assertTrue("p should match to false for an event of the wrong type",
46-
!p.match(wrong_type));
47-
assertTrue("p should match to false for an event of the wrong type " +
48-
"and from the wrong source",
49-
!p.match(wrong_source_and_type));
36+
DerivedEvent goodEv = new DerivedEvent(o);
37+
DerivedEvent wrongSource = new DerivedEvent(new Object());
38+
EventObject wrongType = new EventObject(o);
39+
EventObject wrongSourceAndType = new EventObject(new Object());
40+
41+
Matcher<EventObject> isEventMatcher = isEventFrom(DerivedEvent.class, o);
42+
43+
assertThat(goodEv, isEventMatcher);
44+
assertThat(wrongSource, not(isEventMatcher));
45+
assertThat(wrongType, not(isEventMatcher));
46+
assertThat(wrongSourceAndType, not(isEventMatcher));
5047
}
5148
}

0 commit comments

Comments
 (0)