1   /*
2    * @(#) EventProducerTest.java Sep 1, 2003 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.event;
8   
9   import java.io.File;
10  import java.io.FileInputStream;
11  import java.io.FileOutputStream;
12  import java.io.ObjectInputStream;
13  import java.io.ObjectOutputStream;
14  import java.io.Serializable;
15  import java.rmi.RemoteException;
16  
17  import junit.framework.Assert;
18  import junit.framework.TestCase;
19  
20  /***
21   * The test script for the EventProducer class.
22   * <p>
23   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
24   * University of Technology </a>, the Netherlands. <br>
25   * See for project information <a
26   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
27   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
28   * General Public License (LGPL) </a>, no warranty.
29   * 
30   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
31   * @version $Revision: 1.9 $ $Date: 2005/08/04 12:08:32 $
32   * @since 1.5
33   */
34  public class EventProducerTest extends TestCase
35  {
36      /*** TEST_METHOD is the name of the test method */
37      public static final String TEST_METHOD = "test";
38  
39      /***
40       * constructs a new EventIteratorTest
41       */
42      public EventProducerTest()
43      {
44          this(TEST_METHOD);
45      }
46  
47      /***
48       * constructs a new EventIteratorTest
49       * 
50       * @param arg0 the name of the test method
51       */
52      public EventProducerTest(final String arg0)
53      {
54          super(arg0);
55      }
56  
57      /***
58       * tests the EventProducer
59       */
60      public void test()
61      {
62          this.basic();
63          this.serialize();
64      }
65  
66      /***
67       * tests the EventProducer for Serializability
68       */
69      public void serialize()
70      {
71          try
72          {
73              File file = File.createTempFile("dsol", ".tmp", new File(System
74                      .getProperty("java.io.tmpdir")));
75              ObjectOutputStream output = new ObjectOutputStream(
76                      new FileOutputStream(file));
77  
78              // Let's test for Serializability
79              EventProducerInterface producer = new EventProducerChild();
80              output.writeObject(producer);
81              ObjectInputStream input = new ObjectInputStream(
82                      new FileInputStream(file));
83              producer = (EventProducerInterface) input.readObject();
84              Assert.assertNotNull(producer);
85  
86              // Now we start testing the persistency of listeners after
87              // Serialization
88              SimpleListener listener = new SimpleListener("Listener");
89              producer = new EventProducerChild();
90              producer.addListener(listener, EventProducerChild.EVENT_A);
91              output.writeObject(producer);
92  
93              input = new ObjectInputStream(new FileInputStream(file));
94              producer = (EventProducerInterface) input.readObject();
95  
96              output.close();
97              input.close();
98          } catch (Exception exception)
99          {
100             Assert.fail();
101         }
102     }
103 
104     /***
105      * tests the basic behavior of the eventProducer. All but Serializability,
106      * and concurrency is tested.
107      */
108     public void basic()
109     {
110         // We start with a basic eventProducer
111         EventProducerInterface producer = new EventProducerParent();
112 
113         // Now we create some listeners
114         EventListenerInterface listener1 = new RemoveListener("listener1");
115         EventListenerInterface listener2 = new SimpleListener("listener2");
116         EventListenerInterface listener3 = new SimpleListener("listener3");
117 
118         // Now we change the EVENT_D and see if the runTimeException occurs
119         // This procedure checks for doublures in eventType values
120         EventProducerParent.eventD = EventProducerParent.EVENT_C;
121         try
122         {
123             new EventProducerParent();
124             // This must fail since EVENT_D and EVENT_C have the same value
125             Assert.fail("double eventType values");
126         } catch (Exception exception)
127         {
128             Assert.assertTrue(exception.getClass().equals(
129                     RuntimeException.class));
130         }
131 
132         // Now we test the eventProducer
133         try
134         {
135             producer.addListener(listener2, EventProducerParent.EVENT_E);
136             producer.addListener(listener1, EventProducerChild.EVENT_B);
137             producer.addListener(listener1, EventProducerParent.EVENT_C);
138             producer.addListener(listener1, EventProducerParent.EVENT_E);
139             producer.addListener(listener3, EventProducerParent.EVENT_E);
140 
141             ((EventProducerParent) producer).fireEvent(new Event(
142                     EventProducerParent.EVENT_E, producer, "HI"));
143 
144             ((EventProducerParent) producer).fireEvent(new Event(
145                     EventProducerParent.EVENT_E, producer, "HI"));
146 
147             ((EventProducerParent) producer).fireEvent(new Event(
148                     EventProducerChild.EVENT_A, producer, "HI"));
149 
150             // we try to remove the listener from a wrong eventType.
151             Assert.assertFalse(producer.removeListener(listener1,
152                     EventProducerChild.EVENT_A));
153 
154             // now we try to remove the same listener again.
155             Assert.assertFalse(producer.removeListener(listener1,
156                     EventProducerParent.EVENT_E));
157 
158             // Now we subscribe twice on the same event. The first time should
159             // succeed. The second fail.
160             Assert.assertFalse(producer.addListener(listener1,
161                     EventProducerChild.EVENT_B));
162 
163             // Now we add a null listener
164             Assert.assertFalse(producer.addListener(null,
165                     EventProducerChild.EVENT_A));
166 
167             // Now we add some random listeners
168             Assert.assertTrue(producer.addListener(listener1,
169                     EventProducerChild.EVENT_A));
170 
171             // Assert.assertTrue(producer.removeAllListeners() == 4);
172         } catch (Exception exception)
173         {
174             Assert.fail(exception.getMessage());
175         }
176     }
177 
178     /***
179      * A basic listener class
180      */
181     private static class RemoveListener implements EventListenerInterface
182     {
183         /*** name is the name of the listener */
184         private String name;
185 
186         /***
187          * constructs a new Listener1
188          * 
189          * @param name the name of the listener
190          */
191         public RemoveListener(final String name)
192         {
193             this.name = name;
194         }
195 
196         /***
197          * @see nl.tudelft.simulation.event.EventListenerInterface
198          *      #notify(nl.tudelft.simulation.event.EventInterface)
199          */
200         public void notify(final EventInterface event) throws RemoteException
201         {
202             Assert.assertTrue(event.getType().equals(
203                     EventProducerParent.EVENT_E));
204             EventProducerInterface producer = (EventProducerInterface) event
205                     .getSource();
206             Assert.assertTrue(producer.removeListener(this,
207                     EventProducerParent.eventD));
208             Assert.assertTrue(producer.removeListener(this,
209                     EventProducerParent.EVENT_E));
210             Assert.assertFalse(producer.removeListener(this,
211                     EventProducerParent.EVENT_E));
212         }
213 
214         /***
215          * @see java.lang.Object#toString()
216          */
217         @Override
218 		public String toString()
219         {
220             return this.name;
221         }
222     }
223 
224     /***
225      * A basic listener class
226      */
227     private static class SimpleListener implements EventListenerInterface,
228             Serializable
229     {
230         /*** name is the name of the listener */
231         private String name;
232 
233         /***
234          * constructs a new Listener1
235          * 
236          * @param name the name of the listener
237          */
238         public SimpleListener(final String name)
239         {
240             this.name = name;
241         }
242 
243         /***
244          * @see nl.tudelft.simulation.event.EventListenerInterface
245          *      #notify(nl.tudelft.simulation.event.EventInterface)
246          */
247         public void notify(final EventInterface event)
248         {
249             Assert.assertTrue(event != null);
250         }
251 
252         /***
253          * @see java.lang.Object#toString()
254          */
255         @Override
256 		public String toString()
257         {
258             return this.name;
259         }
260     }
261 }