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.dsol.OTSDEVSSimulator;
12  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
13  import org.opentrafficsim.core.geometry.OTSGeometryException;
14  import org.opentrafficsim.core.geometry.OTSLine3D;
15  import org.opentrafficsim.core.geometry.OTSPoint3D;
16  import org.opentrafficsim.core.gtu.GTUType;
17  
18  import mockit.MockUp;
19  
20  /**
21   * Test the CapacityOTSLink class.
22   * <p>
23   * Copyright (c) 2013-2017 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      /**
35       * Test the constructor and all getters.
36       * @throws NetworkException this test has failed if this exception occurs uncaught
37       * @throws OTSGeometryException this test has failed if this exception occurs uncaught
38       */
39      @Test
40      public final void constructorTest() throws NetworkException, OTSGeometryException
41      {
42          OTSPoint3D fromPoint = new OTSPoint3D(100, 200, 300);
43          OTSPoint3D toPoint = new OTSPoint3D(1000, 2000, 330);
44          Network network = new OTSNetwork("testNetworkForCapacityOTSLink");
45          Node fromNode = new OTSNode(network, "startNode", fromPoint);
46          Node toNode = new OTSNode(network, "endNode", toPoint);
47          LinkType linkType = LinkType.ALL;
48          OTSLine3D designLine = new OTSLine3D(fromPoint, toPoint);
49          OTSSimulatorInterface simulator = new MockUp<OTSSimulatorInterface>()
50          {
51              // no implementation needed.
52          }.getMockInstance();
53          Frequency initialCapacity = new Frequency(1234, FrequencyUnit.PER_HOUR);
54          Frequency finalCapacity = new Frequency(1234, FrequencyUnit.PER_HOUR);
55          Map<GTUType, LongitudinalDirectionality> directionalityMap = new HashMap<>();
56          directionalityMap.put(GTUType.ALL, LongitudinalDirectionality.DIR_PLUS);
57          CapacityOTSLink link = new CapacityOTSLink(network, "link", fromNode, toNode, linkType, designLine, simulator,
58                  initialCapacity, directionalityMap);
59          assertTrue("from point matches", fromPoint.equals(link.getDesignLine().get(0)));
60          assertTrue("to point matches", toPoint.equals(link.getDesignLine().get(1)));
61          assertTrue("from node matches", fromNode.equals(link.getStartNode()));
62          assertTrue("to node matches", toNode.equals(link.getEndNode()));
63          assertTrue("capacity mathes", initialCapacity.equals(link.getCapacity()));
64          link.setCapacity(finalCapacity);
65          assertTrue("capacity mathes", finalCapacity.equals(link.getCapacity()));
66  
67          link = new CapacityOTSLink(network, "link2", fromNode, toNode, linkType, designLine, simulator, initialCapacity,
68                  LongitudinalDirectionality.DIR_PLUS);
69          assertTrue("from point matches", fromPoint.equals(link.getDesignLine().get(0)));
70          assertTrue("to point matches", toPoint.equals(link.getDesignLine().get(1)));
71          assertTrue("from node matches", fromNode.equals(link.getStartNode()));
72          assertTrue("to node matches", toNode.equals(link.getEndNode()));
73          assertTrue("capacity mathes", initialCapacity.equals(link.getCapacity()));
74          link.setCapacity(finalCapacity);
75          assertTrue("capacity mathes", finalCapacity.equals(link.getCapacity()));
76  
77          Network newNetwork = new OTSNetwork("clonedNetworkForCapacityOTSLink");
78          // Create nodes with matching IDs in the new network
79          new OTSNode(newNetwork, fromNode.getId(), fromPoint);
80          new OTSNode(newNetwork, toNode.getId(), toPoint);
81          OTSSimulatorInterface newSimulator = new OTSDEVSSimulator();
82          CapacityOTSLink clonedLink = new CapacityOTSLink(newNetwork, newSimulator, true, link);
83          assertTrue("from point matches", fromPoint.equals(clonedLink.getDesignLine().get(0)));
84          assertTrue("to point matches", toPoint.equals(clonedLink.getDesignLine().get(1)));
85          // XXXX is it really intentional that the equals method of Node does NOT check equality of the network field?
86          assertTrue("from node matches", fromNode.equals(clonedLink.getStartNode()));
87          assertTrue("to node matches", toNode.equals(clonedLink.getEndNode()));
88          assertTrue("capacity mathes", finalCapacity.equals(clonedLink.getCapacity()));
89          clonedLink.setCapacity(initialCapacity);
90          assertTrue("capacity mathes", initialCapacity.equals(clonedLink.getCapacity()));
91          newNetwork.removeLink(clonedLink);
92          clonedLink = link.clone(newNetwork, newSimulator, true);
93          assertTrue("from point matches", fromPoint.equals(clonedLink.getDesignLine().get(0)));
94          assertTrue("to point matches", toPoint.equals(clonedLink.getDesignLine().get(1)));
95          // XXXX is it really intentional that the equals method of Node does NOT check equality of the network field?
96          assertTrue("from node matches", fromNode.equals(clonedLink.getStartNode()));
97          assertTrue("to node matches", toNode.equals(clonedLink.getEndNode()));
98          assertTrue("capacity mathes", finalCapacity.equals(clonedLink.getCapacity()));
99          clonedLink.setCapacity(initialCapacity);
100         assertTrue("capacity mathes", initialCapacity.equals(clonedLink.getCapacity()));
101         assertTrue("toString method returns something with the class name in it", link.toString().contains("CapacityOTSLink"));
102     }
103 }