KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > event > EventManagerTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.event;
11
12 import java.util.*;
13 import junit.framework.*;
14 import org.jgap.*;
15
16 /**
17  * Tests for EventManager class
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class EventManagerTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(EventManagerTest.class);
29     return suite;
30   }
31
32   /**
33    * @throws Exception
34    *
35    * @author Klaus Meffert
36    * @since 1.1
37    */

38   public void testAddEventListener_0()
39       throws Exception JavaDoc {
40     IEventManager man = new EventManager();
41     GeneticEventListener listener = new EventListener();
42     Map listeners = (Map) privateAccessor.getField(man, "m_listeners");
43     assertTrue(listeners.isEmpty());
44     man.addEventListener("testeventname", listener);
45     List listenersList = (List) listeners.get("testeventname");
46     assertEquals(listener, listenersList.get(0));
47   }
48
49   /**
50    * @throws Exception
51    *
52    * @author Klaus Meffert
53    * @since 2.2
54    */

55   public void testAddEventListener_1()
56       throws Exception JavaDoc {
57     IEventManager man = new EventManager();
58     GeneticEventListener listener = new EventListener();
59     Map listeners = (Map) privateAccessor.getField(man, "m_listeners");
60     man.addEventListener("testeventname", listener);
61     man.addEventListener("testeventname", listener);
62     List listenersList = (List) listeners.get("testeventname");
63     assertEquals(listener, listenersList.get(0));
64   }
65
66   /**
67    * @throws Exception
68    *
69    * @author Klaus Meffert
70    * @since 1.1
71    */

72   public void testRemoveEventListener_0()
73       throws Exception JavaDoc {
74     IEventManager man = new EventManager();
75     GeneticEventListener listener = new EventListener();
76     Map listeners = (Map) privateAccessor.getField(man, "m_listeners");
77     man.addEventListener("testeventname", listener);
78     List listenersList = (List) listeners.get("testeventname");
79     man.removeEventListener("notfound", listener);
80     assertEquals(listener, listenersList.get(0));
81     man.removeEventListener("testeventname", null);
82     assertEquals(listener, listenersList.get(0));
83     man.removeEventListener("testeventname", listener);
84     assertTrue( ( (List) listeners.get("testeventname")).size() == 0);
85   }
86
87   /**
88    * @throws Exception
89    *
90    * @author Klaus Meffert
91    * @since 2.2
92    */

93   public void testFireGeneticeEvent_0()
94       throws Exception JavaDoc {
95     IEventManager man = new EventManager();
96     GeneticEventListener listener = new EventListener();
97     man.addEventListener("testeventname", listener);
98     GeneticEvent genEvent = new GeneticEvent("wrong_name", this);
99     man.fireGeneticEvent(genEvent);
100   }
101
102   /**
103    * @throws Exception
104    *
105    * @author Klaus Meffert
106    * @since 2.2
107    */

108   public void testFireGeneticeEvent_1()
109       throws Exception JavaDoc {
110     IEventManager man = new EventManager();
111     EventListener listener = new EventListener();
112     man.addEventListener("testeventname", listener);
113     GeneticEvent genEvent = new GeneticEvent("testeventname", this);
114     man.fireGeneticEvent(genEvent);
115     assertTrue(listener.m_fired);
116     assertEquals(genEvent, listener.m_event);
117   }
118
119   /**
120    *
121    * @author Klaus Meffert
122    */

123   private class EventListener
124       implements GeneticEventListener {
125     private boolean m_fired;
126
127     private GeneticEvent m_event;
128
129     public void geneticEventFired(GeneticEvent a_firedEvent) {
130       m_fired = true;
131       m_event = a_firedEvent;
132     }
133   }
134 }
135
Popular Tags