001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool.util;
007:
008: import org.logicalcobwebs.proxool.AbstractProxoolTest;
009:
010: /**
011: * Test {@link AbstractListenerContainer}.
012: *
013: * @version $Revision: 1.6 $, $Date: 2004/03/16 08:48:32 $
014: * @author Christian Nedregaard (christian_nedregaard@email.com)
015: * @author $Author: brenuart $ (current maintainer)
016: * @since Proxool 0.7
017: */
018: public class ListenerContainerTest extends AbstractProxoolTest {
019:
020: /**
021: * @see junit.framework.TestCase#TestCase
022: */
023: public ListenerContainerTest(String name) {
024: super (name);
025: }
026:
027: /**
028: * Test that added listeners get notified.
029: */
030: public void testAdd() {
031: CompositeTestListener compositeTestListener = new CompositeTestListener();
032: for (int i = 0; i < 10; ++i) {
033: compositeTestListener.addListener(new TestListener());
034: }
035: compositeTestListener.onEvent();
036: assertTrue("Only got "
037: + compositeTestListener.getNumberOfNotifications()
038: + " notifications but expected 10.",
039: compositeTestListener.getNumberOfNotifications() == 10);
040: }
041:
042: /**
043: * Test that removed listeners are not notified, and that the remove method
044: * returns <code>false</code> when trying to removed an unregistered listener.
045: */
046: public void testRemove() {
047: TestListenerIF[] testListeners = new TestListenerIF[] {
048: new TestListener(), new TestListener(),
049: new TestListener(), new TestListener(),
050: new TestListener(), new TestListener(),
051: new TestListener(), new TestListener(),
052: new TestListener(), new TestListener() };
053: CompositeTestListener compositeTestListener = new CompositeTestListener();
054: for (int i = 0; i < 10; ++i) {
055: compositeTestListener.addListener(testListeners[i]);
056: }
057: for (int i = 0; i < 10; ++i) {
058: assertTrue("Removal of a listener failed.",
059: compositeTestListener
060: .removeListener(testListeners[i]));
061: }
062: assertTrue("Removal of unregistered listener returned true",
063: !compositeTestListener
064: .removeListener(new TestListener()));
065: compositeTestListener.onEvent();
066: assertTrue(
067: "Listeners was notified even if all listeners had been removed.",
068: compositeTestListener.getNumberOfNotifications() == 0);
069: }
070:
071: }
072:
073: interface TestListenerIF {
074: void onEvent();
075: }
076:
077: class CompositeTestListener extends AbstractListenerContainer implements
078: TestListenerIF {
079: private int numberOfNotifications;
080:
081: public void onEvent() {
082: Object[] listeners = getListeners();
083:
084: for (int i = 0; i < listeners.length; i++) {
085: TestListenerIF testListener = (TestListenerIF) listeners[i];
086: notification();
087: }
088: }
089:
090: int getNumberOfNotifications() {
091: return numberOfNotifications;
092: }
093:
094: private synchronized void notification() {
095: numberOfNotifications++;
096: }
097: }
098:
099: class TestListener implements TestListenerIF {
100: public void onEvent() {
101: }
102: }
103:
104: /*
105: Revision history:
106: $Log: ListenerContainerTest.java,v $
107: Revision 1.6 2004/03/16 08:48:32 brenuart
108: Changes in the AbstractListenerContainer:
109: - provide more efficient concurrent handling;
110: - better handling of RuntimeException thrown by external listeners.
111:
112: Revision 1.5 2003/03/04 10:58:45 billhorsman
113: checkstyle
114:
115: Revision 1.4 2003/03/04 10:24:41 billhorsman
116: removed try blocks around each test
117:
118: Revision 1.3 2003/03/03 17:09:18 billhorsman
119: all tests now extend AbstractProxoolTest
120:
121: Revision 1.2 2003/03/03 11:12:07 billhorsman
122: fixed licence
123:
124: Revision 1.1 2003/02/10 00:14:33 chr32
125: Added tests for AbstractListenerContainer.
126:
127: */
|