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