1   /*
2    * $Id: MultiplexorTest.java,v 1.2 2004/05/21 23:29:45 johndavidtaylor Exp $
3    * Created on Feb 20, 2004 by John Taylor jdt@roe.ac.uk .
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          } //end BadListener
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         } //end GoodListener
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             //even so, the other listener should still have been notified
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  * $Log: MultiplexorTest.java,v $
145  * Revision 1.2  2004/05/21 23:29:45  johndavidtaylor
146  * Added a clear() method
147  *
148  * Revision 1.1  2004/03/17 21:15:43  johndavidtaylor
149  * Copied across from incubation in beanpeeler
150  *
151  * Revision 1.7  2004/03/14 18:49:14  johndavidtaylor
152  * corrected comments
153  *
154  * Revision 1.6  2004/03/14 02:03:18  johndavidtaylor
155  * improved test coverage
156  *
157  * Revision 1.5  2004/03/14 00:44:22  johndavidtaylor
158  * Added ListenerException
159  * Revision 1.4 2004/03/11 16:35:47
160  * johndavidtaylor brought up to standard Revision 1.3 2004/03/11 09:49:23
161  * johndavidtaylor Loads of updates Revision 1.2 2004/03/08 20:16:44
162  * johndavidtaylor refactored to new package name Revision 1.1 2004/03/08
163  * 19:58:24 johndavidtaylor Initial commit -transfer from previous repository
164  * Revision 1.1 2004/03/02 23:04:37 jdt new
165  */