1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.event;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.Collection;
24
25 import org.junit.Before;
26 import org.junit.Test;
27
28
29
30
31
32
33 public class TestEventSource
34 {
35
36 static final int TEST_TYPE = 42;
37
38
39 static final String TEST_PROPNAME = "test.property.name";
40
41
42 static final Object TEST_PROPVALUE = "a test property value";
43
44
45 CountingEventSource source;
46
47 @Before
48 public void setUp() throws Exception
49 {
50 source = new CountingEventSource();
51 }
52
53
54
55
56 @Test
57 public void testInit()
58 {
59 assertTrue("Listeners list is not empty", source
60 .getConfigurationListeners().isEmpty());
61 assertFalse("Removing listener", source
62 .removeConfigurationListener(new TestListener()));
63 assertFalse("Detail events are enabled", source.isDetailEvents());
64 assertTrue("Error listeners list is not empty", source
65 .getErrorListeners().isEmpty());
66 }
67
68
69
70
71 @Test
72 public void testAddConfigurationListener()
73 {
74 TestListener l = new TestListener();
75 source.addConfigurationListener(l);
76 Collection<ConfigurationListener> listeners = source.getConfigurationListeners();
77 assertEquals("Wrong number of listeners", 1, listeners.size());
78 assertTrue("Listener not in list", listeners.contains(l));
79 }
80
81
82
83
84
85 @Test(expected = IllegalArgumentException.class)
86 public void testAddNullConfigurationListener()
87 {
88 source.addConfigurationListener(null);
89 }
90
91
92
93
94 @Test
95 public void testRemoveConfigurationListener()
96 {
97 TestListener l = new TestListener();
98 assertFalse("Listener can be removed?", source
99 .removeConfigurationListener(l));
100 source.addConfigurationListener(l);
101 source.addConfigurationListener(new TestListener());
102 assertFalse("Unknown listener can be removed", source
103 .removeConfigurationListener(new TestListener()));
104 assertTrue("Could not remove listener", source
105 .removeConfigurationListener(l));
106 assertFalse("Listener still in list", source
107 .getConfigurationListeners().contains(l));
108 }
109
110
111
112
113 @Test
114 public void testRemoveNullConfigurationListener()
115 {
116 source.addConfigurationListener(new TestListener());
117 assertFalse("Null listener can be removed", source
118 .removeConfigurationListener(null));
119 assertEquals("Listener list was modified", 1, source
120 .getConfigurationListeners().size());
121 }
122
123
124
125
126 @Test(expected = UnsupportedOperationException.class)
127 public void testGetConfigurationListenersUpdate()
128 {
129 source.addConfigurationListener(new TestListener());
130 Collection<ConfigurationListener> list = source.getConfigurationListeners();
131 list.clear();
132 }
133
134
135
136
137
138 @Test
139 public void testGetConfigurationListenersAddNew()
140 {
141 Collection<ConfigurationListener> list = source.getConfigurationListeners();
142 source.addConfigurationListener(new TestListener());
143 assertTrue("Listener snapshot not empty", list.isEmpty());
144 }
145
146
147
148
149 @Test
150 public void testSetDetailEvents()
151 {
152 source.setDetailEvents(true);
153 assertTrue("Detail events are disabled", source.isDetailEvents());
154 source.setDetailEvents(true);
155 source.setDetailEvents(false);
156 assertTrue("Detail events are disabled again", source.isDetailEvents());
157 source.setDetailEvents(false);
158 assertFalse("Detail events are still enabled", source.isDetailEvents());
159 }
160
161
162
163
164 @Test
165 public void testFireEvent()
166 {
167 TestListener l = new TestListener();
168 source.addConfigurationListener(l);
169 source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, true);
170 assertEquals("Not 1 event created", 1, source.eventCount);
171 assertEquals("Listener not called once", 1, l.numberOfCalls);
172 assertEquals("Wrong event type", TEST_TYPE, l.lastEvent.getType());
173 assertEquals("Wrong property name", TEST_PROPNAME, l.lastEvent
174 .getPropertyName());
175 assertEquals("Wrong property value", TEST_PROPVALUE, l.lastEvent
176 .getPropertyValue());
177 assertTrue("Wrong before event flag", l.lastEvent.isBeforeUpdate());
178 }
179
180
181
182
183 @Test
184 public void testFireEventNoListeners()
185 {
186 source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
187 assertEquals("An event object was created", 0, source.eventCount);
188 }
189
190
191
192
193 @Test
194 public void testFireEventNoDetails()
195 {
196 TestListener l = new TestListener();
197 source.addConfigurationListener(l);
198 source.setDetailEvents(false);
199 source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
200 assertEquals("Event object was created", 0, source.eventCount);
201 assertEquals("Listener was called", 0, l.numberOfCalls);
202 }
203
204
205
206
207
208 @Test
209 public void testRemoveListenerInFireEvent()
210 {
211 ConfigurationListener lstRemove = new ConfigurationListener()
212 {
213 public void configurationChanged(ConfigurationEvent event)
214 {
215 source.removeConfigurationListener(this);
216 }
217 };
218
219 source.addConfigurationListener(lstRemove);
220 TestListener l = new TestListener();
221 source.addConfigurationListener(l);
222 source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
223 assertEquals("Listener was not called", 1, l.numberOfCalls);
224 assertEquals("Listener was not removed", 1, source
225 .getConfigurationListeners().size());
226 }
227
228
229
230
231 @Test
232 public void testAddErrorListener()
233 {
234 TestListener l = new TestListener();
235 source.addErrorListener(l);
236 Collection<ConfigurationErrorListener> listeners = source.getErrorListeners();
237 assertEquals("Wrong number of listeners", 1, listeners.size());
238 assertTrue("Listener not in list", listeners.contains(l));
239 }
240
241
242
243
244 @Test(expected = IllegalArgumentException.class)
245 public void testAddNullErrorListener()
246 {
247 source.addErrorListener(null);
248 }
249
250
251
252
253 @Test
254 public void testRemoveErrorListener()
255 {
256 TestListener l = new TestListener();
257 assertFalse("Listener can be removed?", source.removeErrorListener(l));
258 source.addErrorListener(l);
259 source.addErrorListener(new TestListener());
260 assertFalse("Unknown listener can be removed", source
261 .removeErrorListener(new TestListener()));
262 assertTrue("Could not remove listener", source.removeErrorListener(l));
263 assertFalse("Listener still in list", source.getErrorListeners()
264 .contains(l));
265 }
266
267
268
269
270 @Test
271 public void testRemoveNullErrorListener()
272 {
273 source.addErrorListener(new TestListener());
274 assertFalse("Null listener can be removed", source
275 .removeErrorListener(null));
276 assertEquals("Listener list was modified", 1, source
277 .getErrorListeners().size());
278 }
279
280
281
282
283 @Test(expected = UnsupportedOperationException.class)
284 public void testGetErrorListenersUpdate()
285 {
286 source.addErrorListener(new TestListener());
287 Collection<ConfigurationErrorListener> list = source.getErrorListeners();
288 list.clear();
289 }
290
291
292
293
294 @Test
295 public void testFireError()
296 {
297 TestListener l = new TestListener();
298 source.addErrorListener(l);
299 Exception testException = new Exception("A test");
300 source.fireError(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE,
301 testException);
302 assertEquals("Not 1 event created", 1, source.errorCount);
303 assertEquals("Error listener not called once", 1, l.numberOfErrors);
304 assertEquals("Normal event was generated", 0, l.numberOfCalls);
305 assertEquals("Wrong event type", TEST_TYPE, l.lastEvent.getType());
306 assertEquals("Wrong property name", TEST_PROPNAME, l.lastEvent
307 .getPropertyName());
308 assertEquals("Wrong property value", TEST_PROPVALUE, l.lastEvent
309 .getPropertyValue());
310 assertEquals("Wrong Throwable object", testException,
311 ((ConfigurationErrorEvent) l.lastEvent).getCause());
312 }
313
314
315
316
317 @Test
318 public void testFireErrorNoListeners()
319 {
320 source.fireError(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE,
321 new Exception());
322 assertEquals("An error event object was created", 0, source.errorCount);
323 }
324
325
326
327
328
329 @Test
330 public void testClone() throws CloneNotSupportedException
331 {
332 source.addConfigurationListener(new TestListener());
333 source.addErrorListener(new TestListener());
334 EventSource copy = (EventSource) source.clone();
335 assertTrue("Configuration listeners registered for clone", copy
336 .getConfigurationListeners().isEmpty());
337 assertTrue("Error listeners registered for clone", copy
338 .getErrorListeners().isEmpty());
339 }
340
341
342
343
344 static class TestListener implements ConfigurationListener,
345 ConfigurationErrorListener
346 {
347 ConfigurationEvent lastEvent;
348
349 int numberOfCalls;
350
351 int numberOfErrors;
352
353 public void configurationChanged(ConfigurationEvent event)
354 {
355 lastEvent = event;
356 numberOfCalls++;
357 }
358
359 public void configurationError(ConfigurationErrorEvent event)
360 {
361 lastEvent = event;
362 numberOfErrors++;
363 }
364 }
365
366
367
368
369
370
371
372 static class CountingEventSource extends EventSource implements Cloneable
373 {
374 int eventCount;
375
376 int errorCount;
377
378 @Override
379 protected ConfigurationEvent createEvent(int type, String propName,
380 Object propValue, boolean before)
381 {
382 eventCount++;
383 return super.createEvent(type, propName, propValue, before);
384 }
385
386 @Override
387 protected ConfigurationErrorEvent createErrorEvent(int type,
388 String propName, Object value, Throwable ex)
389 {
390 errorCount++;
391 return super.createErrorEvent(type, propName, value, ex);
392 }
393 }
394 }