|
@@ -0,0 +1,172 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2012 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+ * THE SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+package com.dmdirc.util.collections;
|
|
23
|
+
|
|
24
|
+import org.junit.Test;
|
|
25
|
+
|
|
26
|
+import static org.junit.Assert.*;
|
|
27
|
+import static org.mockito.Mockito.*;
|
|
28
|
+
|
|
29
|
+public class ListenerListTest {
|
|
30
|
+
|
|
31
|
+ @Test
|
|
32
|
+ public void testGenericAddListener() {
|
|
33
|
+ final Object listener = new Object();
|
|
34
|
+ final Object listener2 = new Object();
|
|
35
|
+ ListenerList instance = new ListenerList();
|
|
36
|
+ instance.add(Object.class, listener);
|
|
37
|
+ instance.add(Object.class, listener2);
|
|
38
|
+ assertTrue(instance.get(Object.class).contains(listener));
|
|
39
|
+ assertTrue(instance.get(Object.class).contains(listener));
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+ @Test(expected=IllegalArgumentException.class)
|
|
44
|
+ public void testGenericAddNull() {
|
|
45
|
+ ListenerList instance = new ListenerList();
|
|
46
|
+ instance.add(Object.class, null);
|
|
47
|
+ assertFalse(instance.get(Object.class).contains(null));
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ @Test
|
|
51
|
+ public void testStringAddListener() {
|
|
52
|
+ final Object listener = new Object();
|
|
53
|
+ final Object listener2 = new Object();
|
|
54
|
+ ListenerList instance = new ListenerList();
|
|
55
|
+ instance.add("Object", listener);
|
|
56
|
+ instance.add("Object", listener2);
|
|
57
|
+ assertTrue(instance.get("Object").contains(listener));
|
|
58
|
+ assertTrue(instance.get("Object").contains(listener2));
|
|
59
|
+ }
|
|
60
|
+ @Test(expected=IllegalArgumentException.class)
|
|
61
|
+ public void testStringAddNull() {
|
|
62
|
+ ListenerList instance = new ListenerList();
|
|
63
|
+ instance.add("Object", null);
|
|
64
|
+ assertFalse(instance.get(Object.class).contains(null));
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ @Test
|
|
68
|
+ public void testGenericRemoveListener() {
|
|
69
|
+ final Object listener = new Object();
|
|
70
|
+ ListenerList instance = new ListenerList();
|
|
71
|
+ instance.add(Object.class, listener);
|
|
72
|
+ assertTrue(instance.get(Object.class).contains(listener));
|
|
73
|
+ instance.remove(Object.class, listener);
|
|
74
|
+ assertFalse(instance.get(Object.class).contains(listener));
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ @Test
|
|
78
|
+ public void testStringRemoveListener() {
|
|
79
|
+ final Object listener = new Object();
|
|
80
|
+ ListenerList instance = new ListenerList();
|
|
81
|
+ instance.add("Object", listener);
|
|
82
|
+ assertTrue(instance.get("Object").contains(listener));
|
|
83
|
+ instance.remove("Object", listener);
|
|
84
|
+ assertFalse(instance.get("Object").contains(listener));
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ @Test
|
|
88
|
+ public void testStringRemoveUnknownListener() {
|
|
89
|
+ final Object listener = new Object();
|
|
90
|
+ ListenerList instance = new ListenerList();
|
|
91
|
+ instance.add("Object", listener);
|
|
92
|
+ assertTrue(instance.get("Object").contains(listener));
|
|
93
|
+ instance.remove("String", listener);
|
|
94
|
+ assertTrue(instance.get("Object").contains(listener));
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ @Test
|
|
98
|
+ public void testGenericGetListeners() {
|
|
99
|
+ final Object listener = new Object();
|
|
100
|
+ final String listener2 = "";
|
|
101
|
+ ListenerList instance = new ListenerList();
|
|
102
|
+ assertTrue(instance.get(Object.class).isEmpty());
|
|
103
|
+ instance.add(Object.class, listener);
|
|
104
|
+ assertTrue(instance.get(Object.class).contains(listener));
|
|
105
|
+ assertTrue(instance.get(String.class).isEmpty());
|
|
106
|
+ instance.add(String.class, listener2);
|
|
107
|
+ assertTrue(instance.get(String.class).contains(listener2));
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ @Test
|
|
111
|
+ public void testStringGetListeners() {
|
|
112
|
+ final Object listener = new Object();
|
|
113
|
+ final String listener2 = "";
|
|
114
|
+ ListenerList instance = new ListenerList();
|
|
115
|
+ assertTrue(instance.get("Object").isEmpty());
|
|
116
|
+ instance.add("Object", listener);
|
|
117
|
+ assertTrue(instance.get("Object").contains(listener));
|
|
118
|
+ assertTrue(instance.get("String").isEmpty());
|
|
119
|
+ instance.add("String", listener2);
|
|
120
|
+ assertTrue(instance.get("String").contains(listener2));
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ @Test
|
|
124
|
+ public void testGetCallableNoArgs() {
|
|
125
|
+ final TestCallable one = mock(TestCallable.class);
|
|
126
|
+ final TestCallable two = mock(TestCallable.class);
|
|
127
|
+ ListenerList instance = new ListenerList();
|
|
128
|
+ instance.add(TestCallable.class, one);
|
|
129
|
+ instance.add(TestCallable.class, two);
|
|
130
|
+ final TestCallable test = instance.getCallable(TestCallable.class);
|
|
131
|
+ test.testMethod();
|
|
132
|
+ verify(one).testMethod();
|
|
133
|
+ verify(two).testMethod();
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ @Test
|
|
137
|
+ public void testGetCallableArg() {
|
|
138
|
+ final TestCallable one = mock(TestCallable.class);
|
|
139
|
+ ListenerList instance = new ListenerList();
|
|
140
|
+ instance.add(TestCallable.class, one);
|
|
141
|
+ final TestCallable test = instance.getCallable(TestCallable.class);
|
|
142
|
+ test.testMethod("test");
|
|
143
|
+ verify(one).testMethod("test");
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ @Test
|
|
147
|
+ public void testGetCallableArgs() {
|
|
148
|
+ final TestCallable one = mock(TestCallable.class);
|
|
149
|
+ ListenerList instance = new ListenerList();
|
|
150
|
+ instance.add(TestCallable.class, one);
|
|
151
|
+ final TestCallable test = instance.getCallable(TestCallable.class);
|
|
152
|
+ test.testMethod("test", "test1");
|
|
153
|
+ verify(one).testMethod("test", "test1");
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @Test(expected=IndexOutOfBoundsException.class)
|
|
157
|
+ public void testGetCallableThrowsException() {
|
|
158
|
+ final TestCallable one = mock(TestCallable.class);
|
|
159
|
+ when(one.testMethod()).thenThrow(new IndexOutOfBoundsException());
|
|
160
|
+ ListenerList instance = new ListenerList();
|
|
161
|
+ instance.add(TestCallable.class, one);
|
|
162
|
+ final TestCallable test = instance.getCallable(TestCallable.class);
|
|
163
|
+ test.testMethod();
|
|
164
|
+ verify(one).testMethod();
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ private interface TestCallable {
|
|
168
|
+ String testMethod();
|
|
169
|
+ String testMethod(final String test);
|
|
170
|
+ String testMethod(final String test, final String test2);
|
|
171
|
+ }
|
|
172
|
+}
|