Skip to content

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.junit.runner.notification;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Method;
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Tests for {@link SynchronizedRunListener}.
15+
*
16+
* @author kcooney (Kevin Cooney)
17+
*/
18+
public class SynchronizedRunListenerTest {
19+
20+
private static class MethodSignature {
21+
private final Method method;
22+
private final String name;
23+
private final List<Class<?>> parameterTypes;
24+
25+
public MethodSignature(Method method) {
26+
this.method = method;
27+
name = method.getName();
28+
parameterTypes = Arrays.asList(method.getParameterTypes());
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return method.toString();
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return name.hashCode();
39+
}
40+
41+
@Override
42+
public boolean equals(Object obj) {
43+
if (this == obj) {
44+
return true;
45+
}
46+
47+
if (!(obj instanceof MethodSignature)) {
48+
return false;
49+
}
50+
51+
MethodSignature that = (MethodSignature) obj;
52+
return name.equals(that.name) && parameterTypes.equals(that.parameterTypes);
53+
}
54+
}
55+
56+
private Set<MethodSignature> getAllDeclaredMethods(Class<?> type) {
57+
Set<MethodSignature> methods = new HashSet<MethodSignature>();
58+
for (Method method : type.getDeclaredMethods()) {
59+
methods.add(new MethodSignature(method));
60+
}
61+
return methods;
62+
}
63+
64+
@Test
65+
public void overridesAllMethodsInRunListener() {
66+
Set<MethodSignature> runListenerMethods = getAllDeclaredMethods(RunListener.class);
67+
Set<MethodSignature> synchronizedRunListenerMethods = getAllDeclaredMethods(SynchronizedRunListener.class);
68+
assertTrue(synchronizedRunListenerMethods.containsAll(runListenerMethods));
69+
}
70+
71+
private static class NamedListener extends RunListener {
72+
private final String name;
73+
74+
public NamedListener(String name) {
75+
this.name = name;
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return name.hashCode();
81+
}
82+
83+
@Override
84+
public boolean equals(Object obj) {
85+
if (this == obj) {
86+
return true;
87+
}
88+
89+
if (!(obj instanceof NamedListener)) {
90+
return false;
91+
}
92+
93+
NamedListener that = (NamedListener) obj;
94+
return name.equals(that.name);
95+
}
96+
}
97+
98+
@Test
99+
public void equalsDelegates() {
100+
NamedListener listener1 = new NamedListener("blue");
101+
NamedListener listener2 = new NamedListener("blue");
102+
NamedListener listener3 = new NamedListener("red");
103+
104+
assertEquals(new SynchronizedRunListener(listener1), new SynchronizedRunListener(listener1));
105+
assertEquals(new SynchronizedRunListener(listener1), new SynchronizedRunListener(listener2));
106+
assertNotEquals(new SynchronizedRunListener(listener1), new SynchronizedRunListener(listener3));
107+
assertEquals(new SynchronizedRunListener(listener1), listener1);
108+
assertNotEquals(listener1, new SynchronizedRunListener(listener1));
109+
}
110+
111+
@Test
112+
public void hashCodeDelegates() {
113+
NamedListener listener = new NamedListener("blue");
114+
assertEquals(listener.hashCode(), new SynchronizedRunListener(listener).hashCode());
115+
}
116+
}

0 commit comments

Comments
 (0)