View Javadoc
1   package org.opentrafficsim.core.network;
2   
3   import static org.junit.Assert.assertTrue;
4   
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import org.djunits.unit.FrequencyUnit;
9   import org.djunits.value.vdouble.scalar.Frequency;
10  import org.junit.Test;
11  import org.opentrafficsim.core.geometry.OTSGeometryException;
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.core.geometry.OTSPoint3D;
14  import org.opentrafficsim.core.gtu.GTUType;
15  import org.opentrafficsim.core.mock.MockSimulator;
16  
17  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19  
20  /**
21   * Test the CapacityOTSLink class.
22   * <p>
23   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 2, 2017 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public class CapacityOTSLinkTest
32  {
33      /**
34       * Test the constructor and all getters.
35       * @throws NetworkException this test has failed if this exception occurs uncaught
36       * @throws OTSGeometryException this test has failed if this exception occurs uncaught
37       */
38      @Test
39      public final void constructorTest() throws NetworkException, OTSGeometryException
40      {
41          OTSPoint3D fromPoint = new OTSPoint3D(100, 200, 300);
42          OTSPoint3D toPoint = new OTSPoint3D(1000, 2000, 330);
43          Network network = new OTSNetwork("testNetworkForCapacityOTSLink");
44          Node fromNode = new OTSNode(network, "startNode", fromPoint);
45          Node toNode = new OTSNode(network, "endNode", toPoint);
46          LinkType linkType = LinkType.ROAD;
47          OTSLine3D designLine = new OTSLine3D(fromPoint, toPoint);
48          SimulatorInterface.TimeDoubleUnit simulator = MockSimulator.createMock();
49          Frequency initialCapacity = new Frequency(1234, FrequencyUnit.PER_HOUR);
50          Frequency finalCapacity = new Frequency(1234, FrequencyUnit.PER_HOUR);
51          Map<GTUType, LongitudinalDirectionality> directionalityMap = new HashMap<>();
52          directionalityMap.put(GTUType.VEHICLE, LongitudinalDirectionality.DIR_PLUS);
53          CapacityOTSLink link =
54                  new CapacityOTSLink(network, "link", fromNode, toNode, linkType, designLine, simulator, initialCapacity);
55          assertTrue("from point matches", fromPoint.equals(link.getDesignLine().get(0)));
56          assertTrue("to point matches", toPoint.equals(link.getDesignLine().get(1)));
57          assertTrue("from node matches", fromNode.equals(link.getStartNode()));
58          assertTrue("to node matches", toNode.equals(link.getEndNode()));
59          assertTrue("capacity mathes", initialCapacity.equals(link.getCapacity()));
60          link.setCapacity(finalCapacity);
61          assertTrue("capacity mathes", finalCapacity.equals(link.getCapacity()));
62  
63          Network newNetwork = new OTSNetwork("clonedNetworkForCapacityOTSLink");
64          // Create nodes with matching IDs in the new network
65          new OTSNode(newNetwork, fromNode.getId(), fromPoint);
66          new OTSNode(newNetwork, toNode.getId(), toPoint);
67          SimulatorInterface.TimeDoubleUnit newSimulator = new DEVSSimulator.TimeDoubleUnit();
68          CapacityOTSLink clonedLink = new CapacityOTSLink(newNetwork, newSimulator, true, link);
69          assertTrue("from point matches", fromPoint.equals(clonedLink.getDesignLine().get(0)));
70          assertTrue("to point matches", toPoint.equals(clonedLink.getDesignLine().get(1)));
71          // XXXX is it really intentional that the equals method of Node does NOT check equality of the network field?
72          assertTrue("from node matches", fromNode.equals(clonedLink.getStartNode()));
73          assertTrue("to node matches", toNode.equals(clonedLink.getEndNode()));
74          assertTrue("capacity mathes", finalCapacity.equals(clonedLink.getCapacity()));
75          clonedLink.setCapacity(initialCapacity);
76          assertTrue("capacity mathes", initialCapacity.equals(clonedLink.getCapacity()));
77          newNetwork.removeLink(clonedLink);
78          clonedLink = link.clone(newNetwork, newSimulator, true);
79          assertTrue("from point matches", fromPoint.equals(clonedLink.getDesignLine().get(0)));
80          assertTrue("to point matches", toPoint.equals(clonedLink.getDesignLine().get(1)));
81          // XXXX is it really intentional that the equals method of Node does NOT check equality of the network field?
82          assertTrue("from node matches", fromNode.equals(clonedLink.getStartNode()));
83          assertTrue("to node matches", toNode.equals(clonedLink.getEndNode()));
84          assertTrue("capacity mathes", finalCapacity.equals(clonedLink.getCapacity()));
85          clonedLink.setCapacity(initialCapacity);
86          assertTrue("capacity mathes", initialCapacity.equals(clonedLink.getCapacity()));
87          assertTrue("toString method returns something with the class name in it", link.toString().contains("CapacityOTSLink"));
88      }
89  }