1 package org.opentrafficsim.core.network.route; 2 3 import org.junit.Test; 4 import org.opentrafficsim.core.network.NetworkException; 5 6 /** 7 * Test the Route class. 8 * <p> 9 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 10 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 11 * <p> 12 * $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $, 13 * initial version 20 jan. 2015 <br> 14 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 15 */ 16 public class RouteTest 17 { 18 /** 19 * Test the Route class. 20 * @throws NetworkException on Network inconsistency (should not happen in this test) 21 */ 22 @Test 23 public void routeTest() throws NetworkException 24 { 25 /*- 26 27 TODO THE ROUTE CLASSES HAVE CHANGED SO THE TESTS HAVE TO BE ADAPTED 28 29 Route route = new Route("name"); 30 assertEquals("No arguments constructor creates an empty Route", 0, route.size()); 31 try 32 { 33 route.getNode(0); 34 fail("Should have thrown an Exception"); 35 } 36 catch (NetworkException ne) 37 { 38 // ignore expected exception 39 } 40 try 41 { 42 route.originNode(); 43 fail("Should have thrown an Exception"); 44 } 45 catch (NetworkException ne) 46 { 47 // ignore expected exception 48 } 49 try 50 { 51 route.destinationNode(); 52 fail("Should have thrown an Exception"); 53 } 54 catch (NetworkException ne) 55 { 56 // ignore expected exception 57 } 58 assertEquals("lastVisitedNode should return null", null, route.lastVisitedNode()); 59 assertEquals("visitNextNode should return null", null, route.visitNextNode()); 60 assertEquals("nextNodeToVisit should return null", null, route.nextNodeToVisit()); 61 OTSNode n1 = new OTSNode("N1", new OTSPoint3D(12, 34)); 62 assertEquals("indexOf should return -1 for an empty Route", -1, route.indexOf(n1)); 63 try 64 { 65 route.addNode(1, n1); 66 fail("addNode with out of range index should have thrown a NetworkException"); 67 } 68 catch (NetworkException ne) 69 { 70 // Ignore expected exception 71 } 72 try 73 { 74 route.addNode(-1, n1); 75 fail("addNode with negative index should have thrown a NetworkException"); 76 } 77 catch (NetworkException ne) 78 { 79 // Ignore expected exception 80 } 81 route.addNode(n1); 82 assertEquals("Route should now contain one Node", 1, route.size()); 83 assertEquals("Node 0 of route should be N1", n1, route.getNode(0)); 84 assertEquals("OriginNode should be N1", n1, route.originNode()); 85 assertEquals("indexOf N1 should return 0", 0, route.indexOf(n1)); 86 OTSNode n2 = new OTSNode("N2", new OTSPoint3D(56, 78)); 87 route.addNode(n2); 88 assertEquals("Route should now contain two Nodes", 2, route.size()); 89 assertEquals("Node 0 of route should be N1", n1, route.getNode(0)); 90 assertEquals("Node 1 of route should be N2", n2, route.getNode(1)); 91 assertEquals("OriginNode should be N1", n1, route.originNode()); 92 assertEquals("indexOf N1 should return 0", 0, route.indexOf(n1)); 93 assertEquals("indexOf N2 should return 1", 1, route.indexOf(n2)); 94 try 95 { 96 route.getNode(-1); 97 fail("Should have thrown an Exception"); 98 } 99 catch (NetworkException ne) 100 { 101 // ignore expected exception 102 } 103 try 104 { 105 route.getNode(2); 106 fail("Should have thrown an Exception"); 107 } 108 catch (NetworkException ne) 109 { 110 // ignore expected exception 111 } 112 // System.out.println(route); 113 assertEquals("nextNodeToVisit should be n1", n1, route.nextNodeToVisit()); 114 Node nextNode = route.visitNextNode(); 115 assertEquals("vistNextNode should have returned n1", n1, nextNode); 116 assertEquals("lastVisitedNode should return n1", n1, route.lastVisitedNode()); 117 assertEquals("nextNodeToVisit should be n2", n2, route.nextNodeToVisit()); 118 assertEquals("lastVisitedNode should return n1", n1, route.lastVisitedNode()); 119 // Currently insertion before the "current" node is allowed and increments the internal lastNode index. 120 OTSNode n0 = new OTSNode("n0", new OTSPoint3D(0, 0)); 121 route.addNode(0, n0); 122 assertEquals("size should now be 3", 3, route.size()); 123 assertEquals("nextNodeToVisit should still be n2", n2, route.nextNodeToVisit()); 124 OTSNode n3 = new OTSNode("n3", new OTSPoint3D(0, 0)); 125 route.addNode(n3); 126 assertEquals("size should now be 4", 4, route.size()); 127 assertEquals("destinationNode should now be n3", n3, route.destinationNode()); 128 assertEquals("nextNodeToVisit should still be n2", n2, route.nextNodeToVisit()); 129 nextNode = route.visitNextNode(); 130 assertEquals("vistNextNode should have returned n2", n2, nextNode); 131 assertEquals("lastVisitedNode should return n2", n2, route.lastVisitedNode()); 132 nextNode = route.visitNextNode(); 133 assertEquals("vistNextNode should have returned n3", n3, nextNode); 134 nextNode = route.visitNextNode(); 135 assertEquals("vistNextNode should have returned null", null, nextNode); 136 137 List<Node> list = new ArrayList<Node>(); 138 list.add(n0); 139 list.add(n1); 140 list.add(n2); 141 list.add(n3); 142 route = new Route(list); 143 assertEquals("Route element 0 is n0", n0, route.getNode(0)); 144 assertEquals("Route element 1 is n1", n1, route.getNode(1)); 145 assertEquals("Route element 2 is n2", n2, route.getNode(2)); 146 assertEquals("Route element 3 is n3", n3, route.getNode(3)); 147 assertEquals("OriginNode should be n0", n0, route.originNode()); 148 CrossSectionLink l01 = 149 new CrossSectionLink("name", n0, n1, new Length(200, 150 METER)); 151 assertTrue("Route contains Link l01", route.containsLink(l01)); 152 CrossSectionLink l10 = 153 new CrossSectionLink("name", n1, n0, new Length(200, 154 METER)); 155 assertFalse("Route contains Link l10", route.containsLink(l10)); 156 Node removedNode = route.removeNode(2); 157 assertEquals("removeNode should return the removed node; i.c. n2", n2, removedNode); 158 assertEquals("Route element 0 is n0", n0, route.getNode(0)); 159 assertEquals("Route element 1 is n1", n1, route.getNode(1)); 160 assertEquals("Route element 2 is n3", n3, route.getNode(2)); 161 assertEquals("OriginNode should be n0", n0, route.originNode()); 162 try 163 { 164 route.removeNode(-1); 165 fail("Negative index should have thrown a NetworkException"); 166 } 167 catch (NetworkException ne) 168 { 169 // Ignore expected exception 170 } 171 try 172 { 173 route.removeNode(4); 174 fail("Too high index should have thrown a NetworkException"); 175 } 176 catch (NetworkException ne) 177 { 178 // Ignore expected exception 179 } 180 // System.out.println(route); 181 nextNode = route.visitNextNode(); 182 // System.out.println(route); 183 assertEquals("visitNextNode should return n0", n0, nextNode); 184 nextNode = route.visitNextNode(); 185 // System.out.println(route); 186 assertEquals("visitNextNode should return n1", n1, nextNode); 187 removedNode = route.removeNode(0); 188 assertEquals("removed node should be n0", n0, removedNode); 189 // System.out.println(route); 190 try 191 { 192 route.removeNode(0); 193 fail("Removing the last visited node on the route should have thrown a NetworkException"); 194 } 195 catch (NetworkException ne) 196 { 197 // Ignore expected exception 198 } 199 200 */ 201 } 202 203 // TODO write JUnit test for the suitability method (has to wait for the XML network lane parser). 204 205 }