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