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