| 1 | package atmsdriver.model; |
|---|
| 2 | |
|---|
| 3 | import java.io.FileWriter; |
|---|
| 4 | import java.io.PrintWriter; |
|---|
| 5 | import java.nio.file.FileSystems; |
|---|
| 6 | import java.nio.file.Files; |
|---|
| 7 | import java.nio.file.Path; |
|---|
| 8 | import java.util.ArrayList; |
|---|
| 9 | import java.util.List; |
|---|
| 10 | import junit.framework.TestCase; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * |
|---|
| 14 | * @author jdalbey |
|---|
| 15 | */ |
|---|
| 16 | public class LoadHighwaysTest extends TestCase { |
|---|
| 17 | |
|---|
| 18 | public LoadHighwaysTest(String testName) { |
|---|
| 19 | super(testName); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | @Override |
|---|
| 23 | protected void setUp() throws Exception { |
|---|
| 24 | super.setUp(); |
|---|
| 25 | PrintWriter writer = null; |
|---|
| 26 | try { |
|---|
| 27 | writer = new PrintWriter(new FileWriter("test/atmsdriver/model/lds_loadhighways_sample.txt")); |
|---|
| 28 | |
|---|
| 29 | writer.println("2"); |
|---|
| 30 | writer.println("32 0 2"); |
|---|
| 31 | writer.println("1210831 1 5 S 0.9 4 CALAFIA"); |
|---|
| 32 | writer.println("1210832 ML ML_1"); |
|---|
| 33 | writer.println("1210833 ML ML_2"); |
|---|
| 34 | writer.println("1210834 ML ML_3"); |
|---|
| 35 | writer.println("1210835 ML ML_4"); |
|---|
| 36 | writer.println("1210845 2 5 S 1.49 4 EL CAMINO REAL"); |
|---|
| 37 | writer.println("1210846 ML ML_1"); |
|---|
| 38 | writer.println("1210847 ML ML_2"); |
|---|
| 39 | writer.println("1210848 ML ML_3"); |
|---|
| 40 | writer.println("1210849 ML ML_4"); |
|---|
| 41 | writer.println("74 0 1"); |
|---|
| 42 | writer.println("1204203 2 5 N 1.26 4 MAGDALENA"); |
|---|
| 43 | writer.println("1204212 ML ML_1"); |
|---|
| 44 | writer.println("1204213 ML ML_2"); |
|---|
| 45 | writer.println("1204214 ML ML_3"); |
|---|
| 46 | writer.println("1204215 ML ML_4"); |
|---|
| 47 | writer.close(); |
|---|
| 48 | } catch (Exception e) { |
|---|
| 49 | e.printStackTrace(); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | @Override |
|---|
| 54 | protected void tearDown() throws Exception { |
|---|
| 55 | super.tearDown(); |
|---|
| 56 | Path path = FileSystems.getDefault().getPath("test/atmsdriver/model", "lds_loadhighways_sample.txt"); |
|---|
| 57 | Files.delete(path); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Test of Highways constructor |
|---|
| 62 | */ |
|---|
| 63 | public void testLoadHighways() { |
|---|
| 64 | System.out.println("toXML"); |
|---|
| 65 | Highways highways = new Highways( |
|---|
| 66 | "test/atmsdriver/model/lds_loadhighways_sample.txt", |
|---|
| 67 | "localhost", 8080); |
|---|
| 68 | |
|---|
| 69 | // Test for correct number of highways |
|---|
| 70 | List<Highway> result = (ArrayList) highways.highways; |
|---|
| 71 | assertEquals(1, result.size()); |
|---|
| 72 | |
|---|
| 73 | // Test 5 was loaded |
|---|
| 74 | Highway fiveS = result.get(0); |
|---|
| 75 | assertEquals(new Integer(5), fiveS.routeNumber); |
|---|
| 76 | |
|---|
| 77 | // Test for correct number of stations |
|---|
| 78 | assertEquals(new Integer(3), new Integer(fiveS.stations.size())); |
|---|
| 79 | |
|---|
| 80 | // Test 5 stations are sorted by postmile |
|---|
| 81 | List<Station> stations = (ArrayList) fiveS.stations; |
|---|
| 82 | ArrayList<Double> stationsPostmiles = new ArrayList<>(); |
|---|
| 83 | for(Station station : stations) |
|---|
| 84 | { |
|---|
| 85 | stationsPostmiles.add(station.postmile); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // Create expected station postmile list (sorted) |
|---|
| 89 | ArrayList<Double> expectedStationsPostmiles = new ArrayList<>(); |
|---|
| 90 | expectedStationsPostmiles.add(new Double(0.9)); |
|---|
| 91 | expectedStationsPostmiles.add(new Double(1.26)); |
|---|
| 92 | expectedStationsPostmiles.add(new Double(1.49)); |
|---|
| 93 | |
|---|
| 94 | for(int i = 0; i < 3; i++) |
|---|
| 95 | { |
|---|
| 96 | assertEquals(expectedStationsPostmiles.get(i), |
|---|
| 97 | stationsPostmiles.get(i)); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // Test for correct number of loops |
|---|
| 101 | assertEquals(new Integer(12), new Integer(fiveS.stations.get(0).loops.size() |
|---|
| 102 | + fiveS.stations.get(1).loops.size() + fiveS.stations.get(2).loops.size())); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|