1 package org.opentrafficsim.core.gtu;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.rmi.RemoteException;
9
10 import javax.media.j3d.Bounds;
11 import javax.naming.NamingException;
12
13 import org.djunits.unit.DurationUnit;
14 import org.djunits.unit.SpeedUnit;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Length;
17 import org.djunits.value.vdouble.scalar.Speed;
18 import org.djunits.value.vdouble.scalar.Time;
19 import org.djutils.immutablecollections.ImmutableMap;
20 import org.djutils.immutablecollections.ImmutableSet;
21 import org.junit.Test;
22 import org.opentrafficsim.base.parameters.ParameterSet;
23 import org.opentrafficsim.base.parameters.Parameters;
24 import org.opentrafficsim.core.dsol.AbstractOTSModel;
25 import org.opentrafficsim.core.dsol.OTSSimulator;
26 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
27 import org.opentrafficsim.core.geometry.OTSGeometryException;
28 import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
29 import org.opentrafficsim.core.gtu.plan.strategical.StrategicalPlanner;
30 import org.opentrafficsim.core.gtu.plan.tactical.TacticalPlanner;
31 import org.opentrafficsim.core.idgenerator.IdGenerator;
32 import org.opentrafficsim.core.network.Link;
33 import org.opentrafficsim.core.network.LinkDirection;
34 import org.opentrafficsim.core.network.Network;
35 import org.opentrafficsim.core.network.NetworkException;
36 import org.opentrafficsim.core.network.Node;
37 import org.opentrafficsim.core.network.OTSNetwork;
38 import org.opentrafficsim.core.network.route.Route;
39 import org.opentrafficsim.core.perception.PerceivableContext;
40
41 import nl.tudelft.simulation.dsol.SimRuntimeException;
42 import nl.tudelft.simulation.language.d3.DirectedPoint;
43
44
45
46
47
48
49
50
51
52
53
54 public class GTUTest
55 {
56
57 public GTU gtuOfStrategicalPlanner = null;
58
59
60 private static final long serialVersionUID = 20151217L;
61
62
63
64
65
66
67
68
69
70
71 @Test
72 public final void testAbstractGTU()
73 throws GTUException, SimRuntimeException, NetworkException, NamingException, RemoteException, OTSGeometryException
74 {
75 TestGTU firstGTU = null;
76 TestGTU lastGTU = null;
77 OTSNetwork perceivableContext = new OTSNetwork("network", true);
78 OTSSimulatorInterface simulator = new OTSSimulator();
79 GTUModel model = new GTUModel(simulator);
80 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(9999, DurationUnit.SI), model);
81 StrategicalPlanner strategicalPlanner = new StrategicalPlanner()
82 {
83
84 @Override
85 public Node nextNode(final Node node, final Link previousLink, final GTUType gtuType) throws NetworkException
86 {
87 return null;
88 }
89
90 @Override
91 public Node nextNode(final Link link, final GTUDirectionality direction, final GTUType gtuType)
92 throws NetworkException
93 {
94 return null;
95 }
96
97 @Override
98 public LinkDirection nextLinkDirection(final Node node, final Link previousLink, final GTUType gtuType)
99 throws NetworkException
100 {
101 return null;
102 }
103
104 @Override
105 public LinkDirection nextLinkDirection(final Link link, final GTUDirectionality direction, final GTUType gtuType)
106 throws NetworkException
107 {
108 return null;
109 }
110
111 @Override
112 public TacticalPlanner<?, ?> getTacticalPlanner()
113 {
114 return null;
115 }
116
117 @Override
118 public TacticalPlanner<?, ?> getTacticalPlanner(final Time time)
119 {
120 return null;
121 }
122
123 @Override
124 public Route getRoute()
125 {
126 return null;
127 }
128
129 @Override
130 public GTU getGtu()
131 {
132 return GTUTest.this.gtuOfStrategicalPlanner;
133 }
134
135 @Override
136 public Node getOrigin()
137 {
138 return null;
139 }
140
141 @Override
142 public Node getDestination()
143 {
144 return null;
145 }
146
147 };
148 Parameters parameters = new ParameterSet();
149 DirectedPoint initialLocation =
150 new DirectedPoint(10, 20, 30, Math.toRadians(10), Math.toRadians(20), Math.toRadians(30));
151 GTUType gtuType1 = new GTUType("gtu type 1", perceivableContext.getGtuType(GTUType.DEFAULTS.VEHICLE));
152 GTUType gtuType2 = new GTUType("gtu type 2", perceivableContext.getGtuType(GTUType.DEFAULTS.VEHICLE));
153 for (String id : new String[] {"id1", "id2"})
154 {
155 for (GTUType gtuType : new GTUType[] {gtuType1, gtuType2})
156 {
157 String gtuId = id + " " + gtuType.getId();
158 TestGTU gtu = new TestGTU(gtuId, gtuType, simulator, perceivableContext);
159 assertEquals("new GTU has correct id", gtuId, gtu.getId());
160 assertEquals("new GTU has correct GTUType", gtuType, gtu.getGTUType());
161 assertEquals("new GTU has correct reference position", RelativePosition.REFERENCE_POSITION, gtu.getReference());
162 assertEquals("new GTU has correct simulator", simulator, gtu.getSimulator());
163 assertEquals("new GTU has odometer value 0", 0, gtu.getOdometer().si, 0);
164 assertTrue("new GTU is stored in the perceivable context", perceivableContext.getGTUs().contains(gtu));
165 lastGTU = gtu;
166 if (null == firstGTU)
167 {
168 firstGTU = gtu;
169 }
170 }
171 }
172 assertFalse("first GTU and last GTU have different id", firstGTU.getId().equals(lastGTU.getId()));
173 assertFalse("first GTU and last GTU have different GTUType", firstGTU.getGTUType().equals(lastGTU.getGTUType()));
174 TestGTU gtu = new TestGTU("id3", gtuType1, simulator, perceivableContext);
175 assertEquals("perceivable context now contains 5 GTUs", 5, perceivableContext.getGTUs().size());
176 gtu.destroy();
177 assertFalse("perceivable context no longer contains the destroyed GTU", perceivableContext.containsGTU(gtu));
178 try
179 {
180 new TestGTU((String) null, gtuType1, simulator, perceivableContext);
181 fail("null id should have thrown a GTUException");
182 }
183 catch (GTUException ge)
184 {
185
186 }
187 try
188 {
189 new TestGTU("IdOfGTU", null, simulator, perceivableContext);
190 fail("null gtuType should have thrown a GTUException");
191 }
192 catch (GTUException ge)
193 {
194
195 }
196 try
197 {
198 new TestGTU("IdOfGTU", gtuType1, null, perceivableContext);
199 fail("null simulator should have thrown a GTUException");
200 }
201 catch (GTUException ge)
202 {
203
204 }
205 try
206 {
207 new TestGTU("IdOfGTU", gtuType1, simulator, null);
208 fail("null perceivableContext should have thrown a GTUException");
209 }
210 catch (GTUException ge)
211 {
212
213 }
214 IdGenerator idGenerator = new IdGenerator("baseName");
215 int lastBeforeId = Integer.parseInt(idGenerator.nextId().substring(8));
216 gtu = new TestGTU(idGenerator, gtuType1, simulator, perceivableContext);
217 int firstAfterId = Integer.parseInt(idGenerator.nextId().substring(8));
218 assertEquals("Id generator was called once in the constructor", 1 + 1, firstAfterId - lastBeforeId);
219 try
220 {
221 new TestGTU((IdGenerator) null, gtuType1, simulator, perceivableContext);
222 fail("null idGenerator should have thrown a GTUException");
223 }
224 catch (GTUException ge)
225 {
226
227 }
228 Speed initialSpeed = new Speed(10, SpeedUnit.KM_PER_HOUR);
229 try
230 {
231 gtu.init(null, initialLocation, initialSpeed);
232 fail("null strategicalPlanner should have thrown a GTUException");
233 }
234 catch (GTUException ge)
235 {
236
237 }
238
239 try
240 {
241 gtu.init(strategicalPlanner, null, initialSpeed);
242 fail("null initialLocation should have thrown a NullPointerException");
243 }
244 catch (NullPointerException ne)
245 {
246
247 }
248 try
249 {
250 gtu.init(strategicalPlanner, new DirectedPoint(Double.NaN, 20, 30), initialSpeed);
251 fail("null initialSpeed should have thrown a GTUException");
252 }
253 catch (GTUException ge)
254 {
255
256 }
257 try
258 {
259 gtu.init(strategicalPlanner, new DirectedPoint(10, Double.NaN, 30), initialSpeed);
260 fail("null initialSpeed should have thrown a GTUException");
261 }
262 catch (GTUException ge)
263 {
264
265 }
266 try
267 {
268 gtu.init(strategicalPlanner, new DirectedPoint(10, 20, Double.NaN), initialSpeed);
269 fail("null initialSpeed should have thrown a GTUException");
270 }
271 catch (GTUException ge)
272 {
273
274 }
275 try
276 {
277 gtu.init(strategicalPlanner, initialLocation, null);
278 fail("null initialSpeed should have thrown a GTUException");
279 }
280 catch (GTUException ge)
281 {
282
283 }
284
285
286 try
287 {
288 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
289 fail("strategicalPlanner that returns a null pointer should have thrown a NullPointerException");
290 }
291 catch (NullPointerException ne)
292 {
293
294 }
295 this.gtuOfStrategicalPlanner = firstGTU;
296 try
297 {
298 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
299 fail("wrong strategicalPlanner should have thrown a GTUException");
300 }
301 catch (GTUException ge)
302 {
303
304 }
305 this.gtuOfStrategicalPlanner = gtu;
306
307
308 try
309 {
310 gtu.init(strategicalPlanner, initialLocation, initialSpeed);
311 fail("init with fake strategical planner should have caused a NullPointerExeption in move");
312 }
313 catch (NullPointerException ne)
314 {
315
316 }
317 }
318
319
320 class GTUModel extends AbstractOTSModel
321 {
322
323 private static final long serialVersionUID = 1L;
324
325
326
327
328 GTUModel(final OTSSimulatorInterface simulator)
329 {
330 super(simulator);
331 }
332
333
334 @Override
335 public void constructModel() throws SimRuntimeException
336 {
337
338 }
339
340
341 @Override
342 public final OTSNetwork getNetwork()
343 {
344 return null;
345 }
346 }
347
348
349 class TestGTU extends AbstractGTU
350 {
351
352 private static final long serialVersionUID = 20151111L;
353
354
355
356
357
358
359
360
361
362 TestGTU(final String id, final GTUType gtuType, final OTSSimulatorInterface simulator,
363 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
364 {
365 super(id, gtuType, simulator, perceivableContext);
366 }
367
368
369
370
371
372
373
374
375
376 TestGTU(final IdGenerator idGenerator, final GTUType gtuType, final OTSSimulatorInterface simulator,
377
378 final PerceivableContext perceivableContext) throws SimRuntimeException, GTUException
379 {
380 super(idGenerator, gtuType, simulator, perceivableContext);
381 }
382
383
384 @Override
385 public Length getLength()
386 {
387 return null;
388 }
389
390
391 @Override
392 public Length getWidth()
393 {
394 return null;
395 }
396
397
398 @Override
399 public Speed getMaximumSpeed()
400 {
401 return null;
402 }
403
404
405 @Override
406 public RelativePosition getFront()
407 {
408 return null;
409 }
410
411
412 @Override
413 public RelativePosition getRear()
414 {
415 return null;
416 }
417
418
419 @Override
420 public RelativePosition getCenter()
421 {
422 return null;
423 }
424
425
426 @Override
427 public ImmutableMap<TYPE, RelativePosition> getRelativePositions()
428 {
429 return null;
430 }
431
432
433 @Override
434 public Bounds getBounds()
435 {
436 return null;
437 }
438
439
440 @Override
441 public ImmutableSet<RelativePosition> getContourPoints()
442 {
443 return null;
444 }
445 }
446
447 }