1
2
3
4
5 package org.abraracourcix.alipes.pipes;
6 import junit.framework.TestCase;
7 import org.abraracourcix.alipes.common.Event;
8 import org.abraracourcix.alipes.listeners.Listener;
9 import org.abraracourcix.alipes.listeners.ListenerException;
10 import org.abraracourcix.alipes.listeners.TestListener;
11 import org.abraracourcix.alipes.monitors.url.URLEvent;
12 /***
13 * JUnit test
14 * @author jdt
15 */
16 public final class MultiplexorTest extends TestCase {
17 /***
18 * Fire up the textui
19 *
20 * @param args ignored
21 */
22 public static void main(final String[] args) {
23 junit.textui.TestRunner.run(MultiplexorTest.class);
24 }
25 /***
26 * Test the obvious - can we multiplex an event?
27 *
28 * @throws MultiplexorException unexpected in this test
29 */
30 public void testEventOccurred() throws MultiplexorException {
31 final Multiplexor multiplexor = new Multiplexor();
32 final TestListener l1 = new TestListener();
33 final TestListener l2 = new TestListener();
34 multiplexor.addListener(l1);
35 multiplexor.addListener(l2);
36 final Event ev = URLEvent.DOWN;
37 final Object resource = "resource";
38 multiplexor.eventOccurred(ev, resource);
39 assertEquals(ev, l1.getEvent());
40 assertEquals(ev, l2.getEvent());
41 assertEquals(resource, l1.getResource());
42 assertEquals(resource, l2.getResource());
43 }
44
45 /***
46 * Test whether we can clear a list of listeners
47 *
48 * @throws MultiplexorException unexpected in this test
49 */
50 public void testClear() throws MultiplexorException {
51 final Multiplexor multiplexor = new Multiplexor();
52 final TestListener l1 = new TestListener();
53 final TestListener l2 = new TestListener();
54 multiplexor.addListener(l1);
55 multiplexor.addListener(l2);
56 multiplexor.clear();
57 final Event ev = URLEvent.DOWN;
58 final Object resource = "resource";
59 multiplexor.eventOccurred(ev, resource);
60 assertNull("Listener 1 shouldn't receive event",l1.getEvent());
61 assertNull("Listener 2 shouldn't receive event",l2.getEvent());
62 }
63
64 /***
65 * Test that in spite of a Listener exception, processing continues
66 */
67 public void testExceptionHandling() {
68 /***
69 * this class always throws an exception
70 */
71 final class BadListener implements Listener {
72 /***
73 * Always throws an exception
74 *
75 * @throws ListenerException didn't I say so
76 * @param event ignored
77 * @param resource ignored
78 * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event,
79 * java.lang.Object)
80 */
81 public void eventOccurred(final Event event, final Object resource)
82 throws ListenerException {
83 /***
84 * test exception
85 */
86 class TestListenerException extends ListenerException {
87 }
88 throw new TestListenerException();
89 }
90 }
91 /***
92 * a listener that simply logs when it has been notified
93 */
94 final class GoodListener implements Listener {
95 /***
96 * Was our eventOccurred method called?
97 */
98 private boolean called;
99 /***
100 * Logs being called
101 *
102 * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event,
103 * java.lang.Object)
104 */
105 public void eventOccurred(final Event event, final Object resource)
106 throws ListenerException {
107 called = true;
108 }
109 /***
110 * Was our eventOccurred method called?
111 *
112 * @return true if it has
113 */
114 public boolean wasCalled() {
115 return called;
116 }
117 }
118 final Multiplexor multiplexor = new Multiplexor();
119 final BadListener l1 = new BadListener();
120 final GoodListener l2 = new GoodListener();
121 final GoodListener l3 = new GoodListener();
122 final BadListener l4 = new BadListener();
123 multiplexor.addListener(l1);
124 multiplexor.addListener(l2);
125 multiplexor.addListener(l3);
126 multiplexor.addListener(l4);
127 final Event ev = URLEvent.DOWN;
128 final Object resource = "resource";
129 try {
130 multiplexor.eventOccurred(ev, resource);
131 fail("Expected an exception here");
132 } catch (MultiplexorException e) {
133
134 assertTrue(
135 "Expected the other listener to be notifed in spite of exception",
136 l2.wasCalled());
137 assertTrue(
138 "Expected the other listener to be notifed in spite of exception",
139 l3.wasCalled());
140 }
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165