source: tmcsimulator/trunk/test/atmsdriver/model/HighwaysTest.java @ 343

Revision 343, 8.1 KB checked in by jdalbey, 7 years ago (diff)

Fix defect #117. Update HighwaysTest?.java for new highway model.

Line 
1package atmsdriver.model;
2
3import archive.ATMSDriver;
4import atmsdriver.model.LoopDetector.DOTCOLOR;
5import atmsdriver.model.Station.DIRECTION;
6import java.io.File;
7import java.io.FileWriter;
8import java.io.PrintWriter;
9import java.nio.file.FileSystems;
10import java.nio.file.Files;
11import java.nio.file.Path;
12import junit.framework.TestCase;
13import org.json.simple.JSONArray;
14import org.json.simple.JSONObject;
15import org.json.simple.parser.JSONParser;
16import org.json.simple.parser.ParseException;
17
18/**
19 *
20 * @author jdalbey
21 */
22public 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/model/ldssample.txt"));
34            writer.println("2");
35            writer.println("32 0 2");
36            writer.println("1210831 1 5 S 0.9 4 CALAFIA");
37            writer.println("1210832 ML ML_1");
38            writer.println("1210833 ML ML_2");
39            writer.println("1210834 ML ML_3");
40            writer.println("1210835 ML ML_4");
41            writer.println("1210845 2 5 S 1.49 4 EL CAMINO REAL");
42            writer.println("1210846 ML ML_1");
43            writer.println("1210847 ML ML_2");
44            writer.println("1210848 ML ML_3");
45            writer.println("1210849 ML ML_4");
46            writer.println("74 0 1");
47            writer.println("1204203 2 5 N 1.26 4 MAGDALENA");
48            writer.println("1204212 ML ML_1");
49            writer.println("1204213 ML ML_2");
50            writer.println("1204214 ML ML_3");
51            writer.println("1204215 ML ML_4");
52            writer.close();
53        } catch (Exception e) {
54            e.printStackTrace();
55        }
56    }
57
58    @Override
59    protected void tearDown() throws Exception {
60        super.tearDown();
61        Path path = FileSystems.getDefault().getPath("test/atmsdriver/model", "ldssample.txt");
62        Files.delete(path);
63    }
64
65    public void testFindStation()
66    {
67        System.out.println("test FindStation()");
68        Highways highways = new Highways(
69                "test/atmsdriver/model/ldssample.txt",
70                "localhost", 8080);
71        assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 0.9));
72        assertTrue(null != highways.findStation(5, DIRECTION.SOUTH, 1.49));
73        assertTrue(null != highways.findStation(5, DIRECTION.NORTH, 1.26));
74        assertTrue(null == highways.findStation(5, DIRECTION.SOUTH, 1.1));
75    }
76    /**
77     * Test of toString method
78     */
79    public void testToString() {
80        System.out.println("toString");
81        Highways highways = new Highways(
82                "config/vds_data/highways_fullmap.txt",
83                "localhost", 8080);
84        highways.getHighwayByRouteNumber(5).stations.get(0).loops.get(0).vol = 1;
85        String result = highways.toString();
86        System.out.println(result);
87        String[] resultLines = result.split("\n");
88        String actual = resultLines[3].substring(0,9);
89        assertEquals("  5 S @.-",actual);
90        highways.applyColorToHighwayStretch(241, Station.DIRECTION.NORTH, 18.08, 1.0, 
91                LoopDetector.DOTCOLOR.RED);
92        result = highways.toString();
93        System.out.println("bravo:\n"+result);
94        assertEquals("241 N -@.@@.-",result.substring(0,13));
95        highways.applyColorToHighwayStretch(241, Station.DIRECTION.SOUTH, 20.13, 2.0, 
96                LoopDetector.DOTCOLOR.YELLOW);
97        result = highways.toString();
98        System.out.println("charly:\n"+result);
99        resultLines = result.split("\n");
100        actual = resultLines[1].substring(0,16);
101        assertEquals("241 S ..+..+.++.",actual);
102    }
103
104
105    public void testToJson() throws ParseException {
106        System.out.println("toJson");
107        Highways highways = new Highways(
108                "test/atmsdriver/model/ldssample.txt",
109                "localhost", 8080);
110        String result = highways.toJson();
111        System.out.println(result);
112        assertTrue(result.indexOf("33.416348") > 0);
113        JSONParser parser = new JSONParser();
114        JSONObject obj = (JSONObject) parser.parse(result);
115        System.out.println(obj);
116        JSONArray array = (JSONArray)obj.get("features");
117        JSONObject item1 = (JSONObject)array.get(0);
118        String id1 = (String) item1.get("id");
119        assertEquals("5 S 0.9", id1);
120        JSONObject item2 = (JSONObject)array.get(1);
121        String id2 = (String) item2.get("id");
122        assertEquals("5 N 1.26", id2);
123        JSONObject item3 = (JSONObject)array.get(2);
124        String id3 = (String) item3.get("id");
125        assertEquals("5 S 1.49", id3);
126    }
127   
128    public void testApplyColor()
129    {
130        System.out.println("apply color");
131        Highways highways = new Highways(
132            "test/atmsdriver/model/ldssample.txt",
133            "localhost", 8080);
134        highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 0.9, 2.0, 
135                LoopDetector.DOTCOLOR.RED);
136        Station target = highways.findStation(5, DIRECTION.SOUTH, 0.9);
137        assertEquals('@',target.getColor());
138        String result = highways.toString();
139        System.out.println("applyto:\n"+result);
140        assertEquals("5 N .-.\n  5 S @.-", result.trim());
141       
142        highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 2.0, 
143                LoopDetector.DOTCOLOR.YELLOW);
144        result = highways.toString();
145        assertEquals("5 N .-.\n  5 S +.+", result.trim());
146       
147        highways.applyColorToHighwayStretch(5, Station.DIRECTION.SOUTH, 1.49, 0.59, 
148                LoopDetector.DOTCOLOR.GREEN);
149        result = highways.toString();
150        assertEquals("5 N .-.\n  5 S -.-", result.trim());
151
152        highways.applyColorToHighwayStretch(5, Station.DIRECTION.NORTH, 0.1, 2.0, 
153                LoopDetector.DOTCOLOR.RED);
154        result = highways.toString();
155        assertEquals("5 N .@.\n  5 S -.-", result.trim());
156       
157        //highways.applyColorToHighwayStretch(241, Station.DIRECTION.NORTH, 20.13, 4.0, LoopDetector.DOTCOLOR.RED);
158        //result = highways.toString();
159        //assertTrue(result.length()>0); //"241 N @@ @@@@@-",result.substring(0,15));
160
161    }           
162   
163    public void testToCondensedFormat()
164    {
165        System.out.println("ToCondensedFormat");
166        Highways highways = new Highways(
167                "test/atmsdriver/model/ldssample.txt",
168                "localhost", 8080);
169
170        String actualCondensedFormatMeta = highways.toCondensedFormat(true);
171        String actualCondensedFormatFEP = highways.toCondensedFormat(false);
172        System.out.println(actualCondensedFormatMeta);
173       
174        assertEquals(expectedCondensedFormatFEP, actualCondensedFormatFEP);
175        assertEquals(expectedCondensedFormatMeta, actualCondensedFormatMeta);
176    }
177    String expectedCondensedFormatMeta = 
178            "2\n" +
179            "32 0 2\n" +
180            "1210831 1 5 S 0.9 4 CALAFIA\n" +
181            "1210832 ML ML_1\n" +
182            "1210833 ML ML_2\n" +
183            "1210834 ML ML_3\n" +
184            "1210835 ML ML_4\n" +
185            "1210845 2 5 S 1.49 4 EL CAMINO REAL\n" +
186            "1210846 ML ML_1\n" +
187            "1210847 ML ML_2\n" +
188            "1210848 ML ML_3\n" +
189            "1210849 ML ML_4\n" +
190            "74 0 1\n" +
191            "1204203 2 5 N 1.26 4 MAGDALENA\n" +
192            "1204212 ML ML_1\n" +
193            "1204213 ML ML_2\n" +
194            "1204214 ML ML_3\n" +
195            "1204215 ML ML_4\n";
196    String expectedCondensedFormatFEP = 
197            "2\n" +
198            "32 0 2\n" +
199            "1210831 1 5 S 0.9 4 \n" +
200            "1210832  0.0 0 ML_1\n" +
201            "1210833  0.0 0 ML_2\n" +
202            "1210834  0.0 0 ML_3\n" +
203            "1210835  0.0 0 ML_4\n" +
204            "1210845 2 5 S 1.49 4 \n" +
205            "1210846  0.0 0 ML_1\n" +
206            "1210847  0.0 0 ML_2\n" +
207            "1210848  0.0 0 ML_3\n" +
208            "1210849  0.0 0 ML_4\n" +
209            "74 0 1\n" +
210            "1204203 2 5 N 1.26 4 \n" +
211            "1204212  0.0 0 ML_1\n" +
212            "1204213  0.0 0 ML_2\n" +
213            "1204214  0.0 0 ML_3\n" +
214            "1204215  0.0 0 ML_4\n";
215   
216}
Note: See TracBrowser for help on using the repository browser.