| 1 | package tmcsim.highwaymodel; |
|---|
| 2 | |
|---|
| 3 | import tmcsim.highwaymodel.LoopDetector; |
|---|
| 4 | import tmcsim.highwaymodel.Station; |
|---|
| 5 | import tmcsim.highwaymodel.Highways; |
|---|
| 6 | import tmcsim.highwaymodel.Station.DIRECTION; |
|---|
| 7 | import java.io.FileWriter; |
|---|
| 8 | import java.io.PrintWriter; |
|---|
| 9 | import java.nio.file.FileSystems; |
|---|
| 10 | import java.nio.file.Files; |
|---|
| 11 | import java.nio.file.Path; |
|---|
| 12 | import junit.framework.TestCase; |
|---|
| 13 | import org.json.simple.JSONArray; |
|---|
| 14 | import org.json.simple.JSONObject; |
|---|
| 15 | import org.json.simple.parser.JSONParser; |
|---|
| 16 | import org.json.simple.parser.ParseException; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * |
|---|
| 20 | * @author jdalbey |
|---|
| 21 | */ |
|---|
| 22 | public class HighwaysTest extends TestCase { |
|---|
| 23 | |
|---|
| 24 | public HighwaysTest(String testName) { |
|---|
| 25 | super(testName); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | @Override |
|---|
| 29 | protected void setUp() throws Exception { |
|---|
| 30 | super.setUp(); |
|---|
| 31 | PrintWriter writer = null; |
|---|
| 32 | try { |
|---|
| 33 | writer = new PrintWriter(new FileWriter("test/atmsdriver/postmiles1.txt")); |
|---|
| 34 | writer.println("5 S 0.9,33.408425,-117.599923,CALAFIA,0,0"); |
|---|
| 35 | writer.println("5 N 1.24,33.413051,-117.601964,MAGDALENA,0,0"); |
|---|
| 36 | writer.println("5 S 1.49,33.416348,-117.603827,EL CAMINO REAL,0,0"); |
|---|
| 37 | writer.close(); |
|---|
| 38 | |
|---|
| 39 | writer = new PrintWriter(new FileWriter("test/atmsdriver/postmiles2.txt")); |
|---|
| 40 | writer.println("5 N 5.89,33.459637,-117.657305,ESTRELLA2,0,0"); |
|---|
| 41 | writer.println("5 S 6.47,33.464281,-117.665631,SACRAMENTO,-0.56275,-0.826627"); |
|---|
| 42 | writer.println("5 N 6.47,33.464404,-117.665509,SACRAMENTO,0.523797,0.851843"); |
|---|
| 43 | writer.println("5 S 7.46,33.475102,-117.674584,CAPISTRANO,-0.815879,0.578222"); |
|---|
| 44 | writer.println("5 N 7.46,33.475073,-117.674271,CAPISTRANO,0.816519,-0.577318"); |
|---|
| 45 | writer.println("5 S 7.99,33.481738,-117.669881,AEROPUERTO,-0.815879,0.578222"); |
|---|
| 46 | writer.println("5 N 7.99,33.481685,-117.669596,AEROPUERTO,0.816519,-0.577318"); |
|---|
| 47 | writer.close(); |
|---|
| 48 | |
|---|
| 49 | writer = new PrintWriter(new FileWriter("test/atmsdriver/postmiles3.txt")); |
|---|
| 50 | writer.println("73 N 23.9,33.643656,-117.859875,BISON 2,0.976131,0.217185"); |
|---|
| 51 | writer.println("55 S 6.88,33.697495,-117.862677,MACARTHU1,-0.710326,0.703873"); |
|---|
| 52 | writer.println("5 N 5.73,33.458342,-117.655008,ESTRELLA1,0,0"); |
|---|
| 53 | writer.println("5 N 6.47,33.464404,-117.665509,SACRAMENTO,0.523797,0.851843"); |
|---|
| 54 | writer.println("405 N 6.21,33.672851,-117.832271,HARVARD,0.320278,0.947323"); |
|---|
| 55 | writer.println("405 S 6.8,33.675863,-117.84179,JAMBOREE1,-0.402558,-0.915394"); |
|---|
| 56 | writer.close(); |
|---|
| 57 | } catch (Exception e) { |
|---|
| 58 | e.printStackTrace(); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | @Override |
|---|
| 63 | protected void tearDown() throws Exception { |
|---|
| 64 | super.tearDown(); |
|---|
| 65 | Path path = FileSystems.getDefault().getPath("test/atmsdriver", "postmiles1.txt"); |
|---|
| 66 | Files.delete(path); |
|---|
| 67 | path = FileSystems.getDefault().getPath("test/atmsdriver", "postmiles2.txt"); |
|---|
| 68 | Files.delete(path); |
|---|
| 69 | path = FileSystems.getDefault().getPath("test/atmsdriver", "postmiles3.txt"); |
|---|
| 70 | Files.delete(path); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public void testFindStation() { |
|---|
| 74 | System.out.println("test FindStation()"); |
|---|
| 75 | Highways highways = new Highways( |
|---|
| 76 | "test/atmsdriver/postmiles1.txt"); |
|---|
| 77 | assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 0.9)); |
|---|
| 78 | assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 1.49)); |
|---|
| 79 | assertTrue(null != highways.findStation(5, DIRECTION.NORTH, 1.24)); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Test of toString method |
|---|
| 84 | */ |
|---|
| 85 | public void testToString() { |
|---|
| 86 | System.out.println("toString"); |
|---|
| 87 | Highways highways = new Highways( |
|---|
| 88 | "test/atmsdriver/postmiles2.txt"); |
|---|
| 89 | highways.getHighwayByRouteNumber(5).stations.get(0).loops.get(0).vol = 1; |
|---|
| 90 | String result = highways.toString(); |
|---|
| 91 | String[] resultLines = result.split("\n"); |
|---|
| 92 | String actual = resultLines[0]; |
|---|
| 93 | assertEquals(" 5 N @.-.-.-", actual); |
|---|
| 94 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.NORTH, 7.99, 2.0, |
|---|
| 95 | LoopDetector.DOTCOLOR.YELLOW); |
|---|
| 96 | result = highways.toString(); |
|---|
| 97 | System.out.println("bravo:\n" + result); |
|---|
| 98 | assertEquals("5 N @.+.+.+\n 5 S .-.-.-.", result.trim()); |
|---|
| 99 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 6.47, 1.51, |
|---|
| 100 | LoopDetector.DOTCOLOR.YELLOW); |
|---|
| 101 | result = highways.toString(); |
|---|
| 102 | System.out.println("charly:\n" + result); |
|---|
| 103 | actual = result.trim(); |
|---|
| 104 | assertEquals("5 N @.+.+.+\n 5 S .+.+.-.", actual); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public void testToJson() throws ParseException { |
|---|
| 108 | System.out.println("toJson"); |
|---|
| 109 | Highways highways = new Highways( |
|---|
| 110 | "test/atmsdriver/postmiles1.txt"); |
|---|
| 111 | String result = highways.toJson(); |
|---|
| 112 | System.out.println(result); |
|---|
| 113 | assertTrue(result.indexOf("33.416348") > 0); |
|---|
| 114 | JSONParser parser = new JSONParser(); |
|---|
| 115 | JSONObject obj = (JSONObject) parser.parse(result); |
|---|
| 116 | System.out.println(obj); |
|---|
| 117 | JSONArray array = (JSONArray) obj.get("features"); |
|---|
| 118 | JSONObject item1 = (JSONObject) array.get(0); |
|---|
| 119 | String id1 = (String) item1.get("id"); |
|---|
| 120 | JSONObject item2 = (JSONObject) array.get(1); |
|---|
| 121 | String id2 = (String) item2.get("id"); |
|---|
| 122 | JSONObject item3 = (JSONObject) array.get(2); |
|---|
| 123 | String id3 = (String) item3.get("id"); |
|---|
| 124 | assertEquals("5 N 1.24", id1); |
|---|
| 125 | assertEquals("5 S 0.9", id2); |
|---|
| 126 | assertEquals("5 S 1.49", id3); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public void testStationSort() throws ParseException { |
|---|
| 130 | System.out.println("stationSort"); |
|---|
| 131 | Highways highways = new Highways( |
|---|
| 132 | "test/atmsdriver/postmiles3.txt"); |
|---|
| 133 | String result = highways.toJson(); |
|---|
| 134 | JSONParser parser = new JSONParser(); |
|---|
| 135 | JSONObject obj = (JSONObject) parser.parse(result); |
|---|
| 136 | JSONArray array = (JSONArray) obj.get("features"); |
|---|
| 137 | JSONObject item1 = (JSONObject) array.get(0); |
|---|
| 138 | String id1 = (String) item1.get("id"); |
|---|
| 139 | JSONObject item2 = (JSONObject) array.get(1); |
|---|
| 140 | String id2 = (String) item2.get("id"); |
|---|
| 141 | JSONObject item3 = (JSONObject) array.get(2); |
|---|
| 142 | String id3 = (String) item3.get("id"); |
|---|
| 143 | assertEquals("5 N 5.73", id1); |
|---|
| 144 | assertEquals("5 N 6.47", id2); |
|---|
| 145 | assertEquals("55 S 6.88", id3); |
|---|
| 146 | JSONObject item4 = (JSONObject) array.get(3); |
|---|
| 147 | String id4 = (String) item4.get("id"); |
|---|
| 148 | JSONObject item5 = (JSONObject) array.get(4); |
|---|
| 149 | String id5 = (String) item5.get("id"); |
|---|
| 150 | assertEquals("73 N 23.9", id4); |
|---|
| 151 | assertEquals("405 N 6.21", id5); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | public void testRouteSort() throws ParseException { |
|---|
| 155 | System.out.println("routeSort"); |
|---|
| 156 | Highways highways = new Highways( |
|---|
| 157 | "test/atmsdriver/postmiles3.txt"); |
|---|
| 158 | String result = highways.toJson(); |
|---|
| 159 | JSONParser parser = new JSONParser(); |
|---|
| 160 | JSONObject obj = (JSONObject) parser.parse(result); |
|---|
| 161 | JSONArray array = (JSONArray) obj.get("features"); |
|---|
| 162 | JSONObject item1 = (JSONObject) array.get(0); |
|---|
| 163 | String id1 = (String) item1.get("id"); |
|---|
| 164 | JSONObject item2 = (JSONObject) array.get(1); |
|---|
| 165 | String id2 = (String) item2.get("id"); |
|---|
| 166 | JSONObject item3 = (JSONObject) array.get(2); |
|---|
| 167 | String id3 = (String) item3.get("id"); |
|---|
| 168 | JSONObject item4 = (JSONObject) array.get(3); |
|---|
| 169 | String id4 = (String) item4.get("id"); |
|---|
| 170 | JSONObject item5 = (JSONObject) array.get(4); |
|---|
| 171 | String id5 = (String) item5.get("id"); |
|---|
| 172 | assertEquals("5 N 5.73", id1); |
|---|
| 173 | assertEquals("5 N 6.47", id2); |
|---|
| 174 | assertEquals("55 S 6.88", id3); |
|---|
| 175 | assertEquals("73 N 23.9", id4); |
|---|
| 176 | assertEquals("405 N 6.21", id5); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public void testApplyColor() { |
|---|
| 180 | System.out.println("apply color"); |
|---|
| 181 | Highways highways = new Highways( |
|---|
| 182 | "test/atmsdriver/postmiles1.txt"); |
|---|
| 183 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 0.9, 2.0, |
|---|
| 184 | LoopDetector.DOTCOLOR.RED); |
|---|
| 185 | Station target = highways.findStation(5, DIRECTION.SOUTH, 0.9); |
|---|
| 186 | assertEquals('@', target.getColor().symbol()); |
|---|
| 187 | String result = highways.toString(); |
|---|
| 188 | System.out.println("applyto:\n" + result); |
|---|
| 189 | assertEquals("5 N .-.\n 5 S @.@", result.trim()); |
|---|
| 190 | |
|---|
| 191 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 2.0, |
|---|
| 192 | LoopDetector.DOTCOLOR.YELLOW); |
|---|
| 193 | result = highways.toString(); |
|---|
| 194 | assertEquals("5 N .-.\n 5 S @.+", result.trim()); |
|---|
| 195 | |
|---|
| 196 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 0.59, |
|---|
| 197 | LoopDetector.DOTCOLOR.GREEN); |
|---|
| 198 | result = highways.toString(); |
|---|
| 199 | assertEquals("5 N .-.\n 5 S @.-", result.trim()); |
|---|
| 200 | |
|---|
| 201 | highways.applyColorToHighwayStretch(5, Station.DIRECTION.NORTH, 1.83, 2.0, |
|---|
| 202 | LoopDetector.DOTCOLOR.RED); |
|---|
| 203 | result = highways.toString(); |
|---|
| 204 | assertEquals("5 N .@.\n 5 S @.-", result.trim()); |
|---|
| 205 | |
|---|
| 206 | //highways.applyColorToHighwayStretch(241, Station.DIRECTION.NORTH, 20.13, 4.0, LoopDetector.DOTCOLOR.RED); |
|---|
| 207 | //result = highways.toString(); |
|---|
| 208 | //assertTrue(result.length()>0); //"241 N @@ @@@@@-",result.substring(0,15)); |
|---|
| 209 | |
|---|
| 210 | highways.reset(); |
|---|
| 211 | result = highways.toString(); |
|---|
| 212 | assertEquals("5 N .-.\n 5 S -.-", result.trim()); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | } |
|---|