package tmcsim.highwaymodel; import tmcsim.highwaymodel.LoopDetector; import tmcsim.highwaymodel.Station; import tmcsim.highwaymodel.Highways; import tmcsim.highwaymodel.Station.DIRECTION; import java.io.FileWriter; import java.io.PrintWriter; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import junit.framework.TestCase; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * * @author jdalbey */ public class HighwaysTest extends TestCase { // Setup a path for temp files static String tmpPath = "/tmp/";// default path is for linux static { String myOs = System.getProperty("os.name").toLowerCase(); if (myOs.indexOf("win") >= 0) { tmpPath = "C:/TEMP/"; // alt path for Windows } } public HighwaysTest(String testName) { super(testName); } @Override protected void setUp() throws Exception { super.setUp(); PrintWriter writer = null; try { writer = new PrintWriter(new FileWriter(tmpPath + "postmiles1.txt")); writer.println("5 S 0.9,33.408425,-117.599923,CALAFIA,0,0"); writer.println("5 N 1.24,33.413051,-117.601964,MAGDALENA,0,0"); writer.println("5 S 1.49,33.416348,-117.603827,EL CAMINO REAL,0,0"); writer.close(); writer = new PrintWriter(new FileWriter(tmpPath + "postmiles2.txt")); writer.println("5 N 5.89,33.459637,-117.657305,ESTRELLA2,0,0"); writer.println("5 S 6.47,33.464281,-117.665631,SACRAMENTO,-0.56275,-0.826627"); writer.println("5 N 6.47,33.464404,-117.665509,SACRAMENTO,0.523797,0.851843"); writer.println("5 S 7.46,33.475102,-117.674584,CAPISTRANO,-0.815879,0.578222"); writer.println("5 N 7.46,33.475073,-117.674271,CAPISTRANO,0.816519,-0.577318"); writer.println("5 S 7.99,33.481738,-117.669881,AEROPUERTO,-0.815879,0.578222"); writer.println("5 N 7.99,33.481685,-117.669596,AEROPUERTO,0.816519,-0.577318"); writer.close(); writer = new PrintWriter(new FileWriter(tmpPath + "/postmiles3.txt")); writer.println("73 N 23.9,33.643656,-117.859875,BISON 2,0.976131,0.217185"); writer.println("55 S 6.88,33.697495,-117.862677,MACARTHU1,-0.710326,0.703873"); writer.println("5 N 5.73,33.458342,-117.655008,ESTRELLA1,0,0"); writer.println("5 N 6.47,33.464404,-117.665509,SACRAMENTO,0.523797,0.851843"); writer.println("405 N 6.21,33.672851,-117.832271,HARVARD,0.320278,0.947323"); writer.println("405 S 6.8,33.675863,-117.84179,JAMBOREE1,-0.402558,-0.915394"); writer.close(); } catch (Exception e) { e.printStackTrace(); } } @Override protected void tearDown() throws Exception { super.tearDown(); Path path = FileSystems.getDefault().getPath(tmpPath, "postmiles1.txt"); Files.delete(path); path = FileSystems.getDefault().getPath(tmpPath, "postmiles2.txt"); Files.delete(path); path = FileSystems.getDefault().getPath(tmpPath, "postmiles3.txt"); Files.delete(path); } public void testFindStation() { System.out.println("test FindStation()"); Highways highways = new Highways( tmpPath + "postmiles1.txt"); assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 0.9)); assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 1.49)); assertTrue(null != highways.findStation(5, DIRECTION.NORTH, 1.24)); } /** * Test of toString method */ public void testToString() { System.out.println("toString"); Highways highways = new Highways( tmpPath + "postmiles2.txt"); highways.getHighwayByRouteNumber(5).stations.get(0).loops.get(0).vol = 1; String result = highways.toString(); String[] resultLines = result.split("\n"); String actual = resultLines[0]; assertEquals(" 5 N @.-.-.-", actual); highways.applyColorToHighwayStretch(5, Station.DIRECTION.NORTH, 7.99, 2.0, LoopDetector.DOTCOLOR.YELLOW); result = highways.toString(); System.out.println("bravo:\n" + result); assertEquals("5 N @.+.+.+\n 5 S .-.-.-.", result.trim()); highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 6.47, 1.51, LoopDetector.DOTCOLOR.YELLOW); result = highways.toString(); System.out.println("charly:\n" + result); actual = result.trim(); assertEquals("5 N @.+.+.+\n 5 S .+.+.-.", actual); } public void testToJson() throws ParseException { System.out.println("toJson"); Highways highways = new Highways( tmpPath + "postmiles1.txt"); String result = highways.toJson(); System.out.println(result); assertTrue(result.indexOf("33.416348") > 0); JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(result); System.out.println(obj); JSONArray array = (JSONArray) obj.get("features"); JSONObject item1 = (JSONObject) array.get(0); String id1 = (String) item1.get("id"); JSONObject item2 = (JSONObject) array.get(1); String id2 = (String) item2.get("id"); JSONObject item3 = (JSONObject) array.get(2); String id3 = (String) item3.get("id"); assertEquals("5 N 1.24", id1); assertEquals("5 S 0.9", id2); assertEquals("5 S 1.49", id3); } public void testStationSort() throws ParseException { System.out.println("stationSort"); Highways highways = new Highways( tmpPath + "postmiles3.txt"); String result = highways.toJson(); JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(result); JSONArray array = (JSONArray) obj.get("features"); JSONObject item1 = (JSONObject) array.get(0); String id1 = (String) item1.get("id"); JSONObject item2 = (JSONObject) array.get(1); String id2 = (String) item2.get("id"); JSONObject item3 = (JSONObject) array.get(2); String id3 = (String) item3.get("id"); assertEquals("5 N 5.73", id1); assertEquals("5 N 6.47", id2); assertEquals("55 S 6.88", id3); JSONObject item4 = (JSONObject) array.get(3); String id4 = (String) item4.get("id"); JSONObject item5 = (JSONObject) array.get(4); String id5 = (String) item5.get("id"); assertEquals("73 N 23.9", id4); assertEquals("405 N 6.21", id5); } public void testRouteSort() throws ParseException { System.out.println("routeSort"); Highways highways = new Highways( tmpPath + "postmiles3.txt"); String result = highways.toJson(); JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(result); JSONArray array = (JSONArray) obj.get("features"); JSONObject item1 = (JSONObject) array.get(0); String id1 = (String) item1.get("id"); JSONObject item2 = (JSONObject) array.get(1); String id2 = (String) item2.get("id"); JSONObject item3 = (JSONObject) array.get(2); String id3 = (String) item3.get("id"); JSONObject item4 = (JSONObject) array.get(3); String id4 = (String) item4.get("id"); JSONObject item5 = (JSONObject) array.get(4); String id5 = (String) item5.get("id"); assertEquals("5 N 5.73", id1); assertEquals("5 N 6.47", id2); assertEquals("55 S 6.88", id3); assertEquals("73 N 23.9", id4); assertEquals("405 N 6.21", id5); } public void testApplyColor() { System.out.println("apply color"); Highways highways = new Highways( tmpPath + "postmiles1.txt"); highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 0.9, 2.0, LoopDetector.DOTCOLOR.RED); Station target = highways.findStation(5, DIRECTION.SOUTH, 0.9); assertEquals('@', target.getColor().symbol()); String result = highways.toString(); System.out.println("applyto:\n" + result); assertEquals("5 N .-.\n 5 S @.@", result.trim()); highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 2.0, LoopDetector.DOTCOLOR.YELLOW); result = highways.toString(); assertEquals("5 N .-.\n 5 S @.+", result.trim()); highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 0.59, LoopDetector.DOTCOLOR.GREEN); result = highways.toString(); assertEquals("5 N .-.\n 5 S @.-", result.trim()); highways.applyColorToHighwayStretch(5, Station.DIRECTION.NORTH, 1.83, 2.0, LoopDetector.DOTCOLOR.RED); result = highways.toString(); assertEquals("5 N .@.\n 5 S @.-", result.trim()); //highways.applyColorToHighwayStretch(241, Station.DIRECTION.NORTH, 20.13, 4.0, LoopDetector.DOTCOLOR.RED); //result = highways.toString(); //assertTrue(result.length()>0); //"241 N @@ @@@@@-",result.substring(0,15)); highways.reset(); result = highways.toString(); assertEquals("5 N .-.\n 5 S -.-", result.trim()); } }