1 package org.djutils.event;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.io.Serializable;
9
10 import org.djutils.exceptions.Try;
11 import org.djutils.metadata.MetaData;
12 import org.djutils.metadata.ObjectDescriptor;
13 import org.junit.Test;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class EventTest
28 {
29
30
31
32 @Test
33 public void testNoMetaData()
34 {
35 EventType noMetaDataEventType = new EventType("No Meta Data", MetaData.NO_META_DATA);
36 new Event(noMetaDataEventType, "sender", new Object[] {"abc", 123, 0.6});
37
38 EventType withMetaDataEventType =
39 new EventType("With Meta Data", new MetaData("Almost identical to NO_META_DATA", "Any Object is accepted",
40 new ObjectDescriptor("Almost identical to NO_META_DATA", "Any Object is accepted", Object.class)));
41 try
42 {
43 new Event(withMetaDataEventType, "sender", new Object[] {"abc", 123, 0.6});
44 fail("imitation of NO_META_DATA does not work for Object[] payload");
45 }
46 catch (IndexOutOfBoundsException ioobe)
47 {
48
49 }
50
51 }
52
53
54
55
56 @Test
57 public void testNoName()
58 {
59 MetaData metaData =
60 new MetaData("INT_EVENT", "event with integer payload", new ObjectDescriptor("int", "integer", Integer.class));
61 EventType eventType = new EventType(metaData);
62 assertEquals(eventType, eventType);
63 assertEquals(Event.class, eventType.getValidEventType());
64 Object content = new SerializableObject();
65 assertNotEquals(eventType, content);
66 assertNotEquals(eventType, null);
67 assertEquals(eventType, new EventType(metaData));
68 assertEquals(eventType.getName(), "INT_EVENT");
69 assertEquals(eventType.getMetaData().getName(), "INT_EVENT");
70 assertEquals(eventType.getMetaData().getDescription(), "event with integer payload");
71 assertEquals(eventType.toString(), "INT_EVENT");
72 assertEquals(1, eventType.getMetaData().getObjectDescriptors().length);
73 Try.testFail(new Try.Execution()
74 {
75 @Override
76 public void execute() throws Throwable
77 {
78 new EventType((MetaData) null);
79 }
80 }, "Constructing EventType with null metadata should have failed");
81
82 new Event(eventType, "source", 5);
83 new Event(eventType, "source", null);
84 Try.testFail(new Try.Execution()
85 {
86 @Override
87 public void execute() throws Throwable
88 {
89 new Event(eventType, "source", 1.2);
90 }
91 }, "Constructing Integer Event with double content should have failed");
92 new Event(eventType, "source", 1.2, false);
93
94 EventType eventType2 = new EventType(MetaData.EMPTY);
95 assertEquals(eventType2, eventType2);
96 assertNotEquals(eventType, eventType2);
97 Object content2 = new SerializableObject();
98 assertNotEquals(eventType2, content2);
99 assertNotEquals(eventType2, null);
100 assertEquals(eventType2, new EventType(MetaData.EMPTY));
101 assertEquals(eventType2.getName(), "No data");
102 assertEquals(eventType2.getMetaData().getName(), "No data");
103 assertEquals(eventType2.getMetaData().getDescription(), "No data");
104 assertEquals(eventType2.toString(), "No data");
105 assertEquals(1, eventType.getMetaData().getObjectDescriptors().length);
106
107 new Event(eventType2, "source", null);
108 Try.testFail(new Try.Execution()
109 {
110 @Override
111 public void execute() throws Throwable
112 {
113 new Event(eventType2, "source", 1.2);
114 }
115 }, "Constructing EMPTY Event with double content should have failed");
116 }
117
118
119
120
121 @SuppressWarnings("deprecation")
122 @Test
123 public void testEventType()
124 {
125 EventType eventType = new EventType("TEST_TYPE", MetaData.NO_META_DATA);
126 assertEquals(eventType, eventType);
127 assertEquals(Event.class, eventType.getValidEventType());
128 Object content = new SerializableObject();
129 assertNotEquals(eventType, content);
130 assertNotEquals(eventType, null);
131 assertNotEquals(eventType, new EventType("TEST_TYPE2", MetaData.NO_META_DATA));
132 assertEquals(eventType.getName(), "TEST_TYPE");
133 assertEquals(eventType.toString(), "TEST_TYPE");
134 TimedEventType timedEventType = new TimedEventType("TEST_TYPE", MetaData.NO_META_DATA);
135 assertEquals(TimedEvent.class, timedEventType.getValidEventType());
136
137 Try.testFail(new Try.Execution()
138 {
139 @Override
140 public void execute() throws Throwable
141 {
142 new EventType("", null);
143 }
144 });
145 Try.testFail(new Try.Execution()
146 {
147 @Override
148 public void execute() throws Throwable
149 {
150 new EventType((String) null, (MetaData) null);
151 }
152 });
153
154
155 eventType = new EventType("event with unspecified meta data");
156 assertEquals("Deprecated constructor uses NO_META_DATA for the meta data", MetaData.NO_META_DATA,
157 eventType.getMetaData());
158 assertEquals("Name is correctly used", "event with unspecified meta data", eventType.getName());
159 assertEquals(Event.class, eventType.getValidEventType());
160 }
161
162
163
164
165 @Test
166 public void testEvent()
167 {
168 Serializable source = "source_id";
169 Serializable source2 = new SerializableObject();
170 EventType eventType = new EventType("TEST_TYPE", MetaData.NO_META_DATA);
171 EventType eventType2 = new EventType("TEST_TYPE2", MetaData.NO_META_DATA);
172 Serializable content = new SerializableObject();
173 Serializable content2 = new SerializableObject();
174 EventInterface event = new Event(eventType, source, content);
175 assertEquals(event.getContent(), content);
176 assertEquals(event.getSourceId(), source);
177 assertEquals(event.getType(), eventType);
178
179 assertEquals(event, event);
180 assertEquals(event, new Event(eventType, source, content));
181 assertNotEquals(event, source);
182 assertNotEquals(event, null);
183
184 assertNotEquals(event, new Event(eventType2, source, content));
185 assertNotEquals(event, new Event(eventType, source2, content));
186 assertNotEquals(event, new Event(eventType, source, content2));
187
188 assertNotEquals(event, new Event(null, source, content));
189 assertNotEquals(event, new Event(eventType, null, content));
190 assertNotEquals(event, new Event(eventType, source, null));
191
192 assertNotEquals(new Event(null, source, content), event);
193 assertNotEquals(new Event(eventType, null, content), event);
194 assertNotEquals(new Event(eventType, source, null), event);
195
196 assertEquals(new Event(null, source, content), new Event(null, source, content));
197 assertEquals(new Event(eventType, null, content), new Event(eventType, null, content));
198 assertEquals(new Event(eventType, source, null), new Event(eventType, source, null));
199
200 assertEquals(event.hashCode(), event.hashCode());
201 assertEquals(event.hashCode(), new Event(eventType, source, content).hashCode());
202 assertNotEquals(event.hashCode(), source.hashCode());
203
204 assertNotEquals(event.hashCode(), new Event(eventType2, source, content).hashCode());
205 assertNotEquals(event.hashCode(), new Event(eventType, source2, content).hashCode());
206 assertNotEquals(event.hashCode(), new Event(eventType, source, content2).hashCode());
207
208 assertNotEquals(event.hashCode(), new Event(null, source, content).hashCode());
209 assertNotEquals(event.hashCode(), new Event(eventType, null, content).hashCode());
210 assertNotEquals(event.hashCode(), new Event(eventType, source, null).hashCode());
211
212 assertTrue(event.toString().contains("TEST_TYPE"));
213 }
214
215
216
217
218 @Test
219 public void testTimedEvent()
220 {
221 Serializable source = "timed_source_id";
222 Serializable source2 = new SerializableObject();
223 TimedEventType timedEventType = new TimedEventType("TEST_TYPE", MetaData.NO_META_DATA);
224 TimedEventType timedEventType2 = new TimedEventType("TEST_TYPE2", MetaData.NO_META_DATA);
225 Serializable content = new SerializableObject();
226 Serializable content2 = new SerializableObject();
227 long time = 123L;
228 long time2 = 456L;
229 TimedEvent<Long> timedEvent = new TimedEvent<>(timedEventType, source, content, time);
230 TimedEvent<Long> timedEvent2 = new TimedEvent<>(timedEventType2, source2, content2, time2);
231 assertEquals(content, timedEvent.getContent());
232 assertEquals(source, timedEvent.getSourceId());
233 assertEquals(timedEventType, timedEvent.getType());
234 assertEquals(time, timedEvent.getTimeStamp().longValue());
235
236 assertEquals(timedEvent, timedEvent);
237 assertEquals(new TimedEvent<Long>(timedEventType, source, content, time), timedEvent);
238 assertNotEquals(timedEvent, source);
239 assertNotEquals(timedEvent, null);
240
241 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType2, source, content, time));
242 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, source2, content, time));
243 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, source, content2, time));
244 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, source, content, time2));
245
246 assertNotEquals(timedEvent, new TimedEvent<Long>(null, source, content, time));
247 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, null, content, time));
248 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, source, null, time));
249 assertNotEquals(timedEvent, new TimedEvent<Long>(timedEventType, source, content, null));
250
251 assertNotEquals(new TimedEvent<Long>(null, source, content, time), timedEvent);
252 assertNotEquals(new TimedEvent<Long>(timedEventType, null, content, time), timedEvent);
253 assertNotEquals(new TimedEvent<Long>(timedEventType, source, null, time), timedEvent);
254 assertNotEquals(new TimedEvent<Long>(timedEventType, source, content, null), timedEvent);
255
256 assertEquals(new TimedEvent<Long>(null, source, content, time), new TimedEvent<Long>(null, source, content, time));
257 assertEquals(new TimedEvent<Long>(timedEventType, null, content, time),
258 new TimedEvent<Long>(timedEventType, null, content, time));
259 assertEquals(new TimedEvent<Long>(timedEventType, source, null, time),
260 new TimedEvent<Long>(timedEventType, source, null, time));
261 assertEquals(new TimedEvent<Long>(timedEventType, source, content, null),
262 new TimedEvent<Long>(timedEventType, source, content, null));
263
264 assertEquals(timedEvent.hashCode(), timedEvent.hashCode());
265 assertEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source, content, time).hashCode());
266 assertNotEquals(timedEvent.hashCode(), source.hashCode());
267
268 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType2, source, content, time).hashCode());
269 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source2, content, time).hashCode());
270 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source, content2, time).hashCode());
271 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source, content, time2).hashCode());
272
273 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(null, source, content, time).hashCode());
274 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, null, content, time).hashCode());
275 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source, null, time).hashCode());
276 assertNotEquals(timedEvent.hashCode(), new TimedEvent<Long>(timedEventType, source, content, null).hashCode());
277
278 assertTrue(timedEvent.toString().contains("TEST_TYPE"));
279 assertTrue(timedEvent.toString().contains("123"));
280
281 assertTrue(timedEvent.compareTo(timedEvent) == 0);
282 assertTrue(timedEvent.compareTo(timedEvent2) < 0);
283 assertTrue(timedEvent2.compareTo(timedEvent) > 0);
284
285 MetaData metaData =
286 new MetaData("INT_EVENT", "event with integer payload", new ObjectDescriptor("int", "integer", Integer.class));
287 TimedEventType intEventType = new TimedEventType(metaData);
288 Try.testFail(new Try.Execution()
289 {
290 @Override
291 public void execute() throws Throwable
292 {
293 new TimedEvent<Double>(intEventType, "source", 1.2, 3.4);
294 }
295 }, "Constructing Integer TimedEvent with double content should have failed");
296 new TimedEvent<Double>(intEventType, "source", 1.2, 3.4, false);
297 }
298
299
300 class SerializableObject extends Object implements Serializable
301 {
302
303 private static final long serialVersionUID = 1L;
304 }
305 }