KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > util > ListenerContainerTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.util;
7
8 import org.logicalcobwebs.proxool.AbstractProxoolTest;
9
10 /**
11  * Test {@link AbstractListenerContainer}.
12  *
13  * @version $Revision: 1.6 $, $Date: 2004/03/16 08:48:32 $
14  * @author Christian Nedregaard (christian_nedregaard@email.com)
15  * @author $Author: brenuart $ (current maintainer)
16  * @since Proxool 0.7
17  */

18 public class ListenerContainerTest extends AbstractProxoolTest {
19
20     /**
21      * @see junit.framework.TestCase#TestCase
22      */

23     public ListenerContainerTest(String JavaDoc name) {
24         super(name);
25     }
26
27     /**
28      * Test that added listeners get notified.
29      */

30     public void testAdd() {
31         CompositeTestListener compositeTestListener = new CompositeTestListener();
32         for (int i = 0; i < 10; ++i) {
33             compositeTestListener.addListener(new TestListener());
34         }
35         compositeTestListener.onEvent();
36         assertTrue("Only got " + compositeTestListener.getNumberOfNotifications()
37                 + " notifications but expected 10.", compositeTestListener.getNumberOfNotifications() == 10);
38     }
39
40     /**
41      * Test that removed listeners are not notified, and that the remove method
42      * returns <code>false</code> when trying to removed an unregistered listener.
43      */

44     public void testRemove() {
45         TestListenerIF[] testListeners = new TestListenerIF[]{
46             new TestListener(), new TestListener(), new TestListener(), new TestListener(),
47             new TestListener(), new TestListener(), new TestListener(), new TestListener(),
48             new TestListener(), new TestListener()
49         };
50         CompositeTestListener compositeTestListener = new CompositeTestListener();
51         for (int i = 0; i < 10; ++i) {
52             compositeTestListener.addListener(testListeners[i]);
53         }
54         for (int i = 0; i < 10; ++i) {
55             assertTrue("Removal of a listener failed.", compositeTestListener.removeListener(testListeners[i]));
56         }
57         assertTrue("Removal of unregistered listener returned true",
58                 !compositeTestListener.removeListener(new TestListener()));
59         compositeTestListener.onEvent();
60         assertTrue("Listeners was notified even if all listeners had been removed.",
61                 compositeTestListener.getNumberOfNotifications() == 0);
62     }
63
64 }
65
66 interface TestListenerIF {
67     void onEvent();
68 }
69
70 class CompositeTestListener extends AbstractListenerContainer implements TestListenerIF {
71     private int numberOfNotifications;
72
73     public void onEvent() {
74         Object JavaDoc[] listeners = getListeners();
75         
76         for(int i=0; i<listeners.length; i++) {
77             TestListenerIF testListener = (TestListenerIF) listeners[i];
78             notification();
79         }
80     }
81
82     int getNumberOfNotifications() {
83         return numberOfNotifications;
84     }
85
86     private synchronized void notification() {
87         numberOfNotifications++;
88     }
89 }
90
91 class TestListener implements TestListenerIF {
92     public void onEvent() {
93     }
94 }
95
96 /*
97  Revision history:
98  $Log: ListenerContainerTest.java,v $
99  Revision 1.6 2004/03/16 08:48:32 brenuart
100  Changes in the AbstractListenerContainer:
101  - provide more efficient concurrent handling;
102  - better handling of RuntimeException thrown by external listeners.
103
104  Revision 1.5 2003/03/04 10:58:45 billhorsman
105  checkstyle
106
107  Revision 1.4 2003/03/04 10:24:41 billhorsman
108  removed try blocks around each test
109
110  Revision 1.3 2003/03/03 17:09:18 billhorsman
111  all tests now extend AbstractProxoolTest
112
113  Revision 1.2 2003/03/03 11:12:07 billhorsman
114  fixed licence
115
116  Revision 1.1 2003/02/10 00:14:33 chr32
117  Added tests for AbstractListenerContainer.
118
119 */

120
Popular Tags