1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package jrummikub.util;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Test class for {@link Event2}
*/
public class Event3Test {
int fired, fired2, fired3, fired4, fired5, fired6;
/** */
@Test
public void singleListener() {
fired = 0;
fired2 = 0;
fired3 = 0;
Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>();
testEvent.add(new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fired += n;
fired2 += m;
fired3 += o;
}
});
assertEquals(fired, 0);
assertEquals(fired2, 0);
assertEquals(fired3, 0);
testEvent.emit(10, 5, 3);
assertEquals(fired, 10);
assertEquals(fired2, 5);
assertEquals(fired3, 3);
testEvent.emit(20, 45, 42);
assertEquals(fired, 30);
assertEquals(fired2, 50);
assertEquals(fired3, 45);
}
/** */
@Test
public void twoListeners() {
fired = 0;
fired2 = 0;
fired3 = 0;
fired4 = 0;
fired5 = 0;
fired6 = 0;
Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>();
testEvent.add(new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fired += n;
fired2 += m;
fired3 += o;
}
});
testEvent.add(new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fired4 -= n;
fired5 -= m;
fired6 -= o;
}
});
assertEquals(fired, 0);
assertEquals(fired2, 0);
assertEquals(fired3, 0);
assertEquals(fired4, 0);
assertEquals(fired5, 0);
assertEquals(fired6, 0);
testEvent.emit(5, 10, 4);
assertEquals(fired, 5);
assertEquals(fired2, 10);
assertEquals(fired3, 4);
assertEquals(fired4, -5);
assertEquals(fired5, -10);
assertEquals(fired6, -4);
}
/** */
@Test
public void removeListener() {
fired = 0;
fired2 = 0;
fired3 = 0;
Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>();
testEvent.add(new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fired += n;
fired2 += m;
fired3 += o;
}
});
IListener3<Integer, Integer, Integer> rem = new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fail();
}
};
assertEquals(fired, 0);
assertEquals(fired2, 0);
assertEquals(fired3, 0);
testEvent.add(rem);
testEvent.remove(rem);
testEvent.emit(10, 20, 30);
assertEquals(fired, 10);
assertEquals(fired2, 20);
assertEquals(fired3, 30);
}
/** */
@Test
public void removeListenerByConnection() {
fired = 0;
fired2 = 0;
fired3 = 0;
Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>();
testEvent.add(new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fired += n;
fired2 += m;
fired3 += o;
}
});
IListener3<Integer, Integer, Integer> rem = new IListener3<Integer, Integer, Integer>() {
@Override
public void handle(Integer n, Integer m, Integer o) {
fail();
}
};
assertEquals(fired, 0);
assertEquals(fired2, 0);
assertEquals(fired3, 0);
Connection connection = testEvent.add(rem);
connection.remove();
testEvent.emit(10, 20, 30);
assertEquals(fired, 10);
assertEquals(fired2, 20);
assertEquals(fired3, 30);
}
}
|