From 548b5fe321beac1a9a28102bfa84f32acdfd9dd9 Mon Sep 17 00:00:00 2001 From: YaFedImYaEatIm Date: Mon, 19 Sep 2016 01:24:15 +1200 Subject: [PATCH] Added Flight Path Tests --- .../java/seng202/group9/Core/FlightPath.java | 25 +++- .../group9/GUI/FlightRDController.java | 7 +- .../java/seng202/group9/FlightPathTest.java | 115 ++++++++++++++++++ 3 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 src/test/java/seng202/group9/FlightPathTest.java diff --git a/src/main/java/seng202/group9/Core/FlightPath.java b/src/main/java/seng202/group9/Core/FlightPath.java index b3c60ab..15e4234 100644 --- a/src/main/java/seng202/group9/Core/FlightPath.java +++ b/src/main/java/seng202/group9/Core/FlightPath.java @@ -1,5 +1,7 @@ package seng202.group9.Core; +import seng202.group9.Controller.DataException; + import java.util.ArrayList; public class FlightPath { @@ -49,7 +51,7 @@ public class FlightPath { public void setFlightPoints(ArrayList flightPoints) { this.flightPoints = new ArrayList(); for (int i = 0; i < flightPoints.size(); i ++) { - this.flightPoints = flightPoints; + this.flightPoints.add(flightPoints.get(i)); } } @@ -82,7 +84,10 @@ public class FlightPath { * gets the ID of the Flight Path. * @return */ - public int getID(){ + public int getID() throws DataException{ + if (ID == -1){ + throw new DataException("This Flight Path has no ID set yet"); + } return ID; } @@ -125,7 +130,13 @@ public class FlightPath { * @param index */ public void addFlightPoint(FlightPoint flightPoint, int index){ - flightPoints.add(index, flightPoint); + if (index >= flightPoints.size()){ + flightPoints.add(flightPoint); + }else if (index < 0){ + flightPoints.add(0, flightPoint); + }else { + flightPoints.add(index, flightPoint); + } } /** @@ -140,8 +151,12 @@ public class FlightPath { * delets a point from the flight at a specific index. * @param index */ - public void delFlightPoint(int index){ - flightPoints.remove(index); + public void delFlightPoint(int index) throws DataException{ + if (flightPoints.size() > index && index >= 0) { + flightPoints.remove(index); + }else{ + throw new DataException("Flight Point at Index " + index + " does not exist for this flight Path."); + } } /** diff --git a/src/main/java/seng202/group9/GUI/FlightRDController.java b/src/main/java/seng202/group9/GUI/FlightRDController.java index 2ade98d..223c675 100644 --- a/src/main/java/seng202/group9/GUI/FlightRDController.java +++ b/src/main/java/seng202/group9/GUI/FlightRDController.java @@ -9,6 +9,7 @@ import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.MouseEvent; import seng202.group9.Controller.App; +import seng202.group9.Controller.DataException; import seng202.group9.Controller.Dataset; import seng202.group9.Core.FlightPath; import seng202.group9.Core.FlightPoint; @@ -121,7 +122,11 @@ public class FlightRDController extends Controller { */ public void load() { theDataSet = getParent().getCurrentDataset(); - currentPathId = theDataSet.getFlightPaths().get(0).getID(); //Sets the default to the 1st Path + try { + currentPathId = theDataSet.getFlightPaths().get(0).getID(); //Sets the default to the 1st Path + } catch (DataException e) { + e.printStackTrace(); + } flightIdCol.setCellValueFactory(new PropertyValueFactory("ID")); flightNameCol.setCellValueFactory(new PropertyValueFactory("Name")); flightTypeCol.setCellValueFactory(new PropertyValueFactory("Type")); diff --git a/src/test/java/seng202/group9/FlightPathTest.java b/src/test/java/seng202/group9/FlightPathTest.java new file mode 100644 index 0000000..066dab2 --- /dev/null +++ b/src/test/java/seng202/group9/FlightPathTest.java @@ -0,0 +1,115 @@ +package seng202.group9;/** + * Created by Gondr on 19/09/2016. + */ + +import static org.junit.Assert.*; + +import org.junit.Test; +import seng202.group9.Controller.DataException; +import seng202.group9.Core.FlightPath; +import seng202.group9.Core.FlightPoint; +import seng202.group9.Core.RoutePath; + +import java.lang.reflect.Array; +import java.util.ArrayList; + +public class FlightPathTest { + + @Test + public void testConstructor(){ + FlightPath flightPath = new FlightPath(1, "NZCH", "WSSS"); + + try { + assertTrue(flightPath.getID() == 1); + } catch (DataException e) { + fail("There is an ID set already."); + } + assertEquals(flightPath.arrivesAt(), "WSSS"); + assertEquals(flightPath.departsFrom(), "NZCH"); + + flightPath.setArrivalAirport("WSSI"); + flightPath.setDepartureAirport("NZAK"); + flightPath.setID(2); + + try { + assertTrue(flightPath.getID() == 2); + } catch (DataException e) { + fail("There is an ID set already."); + } + assertEquals(flightPath.arrivesAt(), "WSSI"); + assertEquals(flightPath.departsFrom(), "NZAK"); + } + + @Test(expected = DataException.class) + public void testIDError() throws DataException{ + FlightPath flightPath = new FlightPath("NZCH", "WSSS"); + flightPath.getID(); + } + + @Test + public void testFlightPoints(){ + FlightPath flightPath = new FlightPath(1, "NZCH", "WSSS"); + FlightPoint startPoint = new FlightPoint("NZCH", 1, 1, "APT", "", 0, 0, 0, 0, 172.5336822, -43.48664019); + FlightPoint endPoint = new FlightPoint("WSSS", 2, 1, "APT", "", 0, 0, 0, 0, 103.995603, 1.35191714); + + ArrayList flightPoints = new ArrayList(); + flightPoints.add(startPoint); + flightPoints.add(endPoint); + + flightPath.setFlightPoints(flightPoints); + assertTrue(flightPath.getFlightPoints().size() == 2); + assertTrue(flightPath.getFlight().size() == 2); + + flightPath.delFlightPoint(endPoint); + assertTrue(flightPath.getFlightPoints().size() == 1); + + flightPath.addFlightPoint(endPoint); + assertTrue(flightPath.getFlightPoints().size() == 2); + assertEquals(flightPath.getFlightPoints().get(1), endPoint); + + try { + flightPath.delFlightPoint(0); + } catch (DataException e) { + fail("There is an existing point therefore it should be deletable"); + } + + flightPath.addFlightPoint(startPoint, 0); + assertEquals(flightPath.getFlightPoints().get(0), startPoint); + + flightPath.addFlightPoint(flightPoints); + assertTrue(flightPath.getFlightPoints().size() == 4); + } + + @Test + public void testAddingFlightPointAtInvalidPoints(){ + FlightPath flightPath = new FlightPath(1, "NZCH", "WSSS"); + FlightPoint startPoint = new FlightPoint("NZCH", 1, 1, "APT", "", 0, 0, 0, 0, 172.5336822, -43.48664019); + FlightPoint endPoint = new FlightPoint("WSSS", 2, 1, "APT", "", 0, 0, 0, 0, 103.995603, 1.35191714); + + flightPath.addFlightPoint(startPoint); + flightPath.addFlightPoint(startPoint); + flightPath.addFlightPoint(startPoint); + + flightPath.addFlightPoint(endPoint, -1); + flightPath.addFlightPoint(endPoint, 100); + + assertEquals(flightPath.getFlightPoints().get(0), endPoint); + assertEquals(flightPath.getFlightPoints().get(4), endPoint); + } + + @Test + public void getRoutePath(){ + FlightPath flightPath = new FlightPath(1, "NZCH", "WSSS"); + FlightPoint startPoint = new FlightPoint("NZCH", 1, 1, "APT", "", 0, 0, 0, 0, 172.5336822, -43.48664019); + FlightPoint endPoint = new FlightPoint("WSSS", 2, 1, "APT", "", 0, 0, 0, 0, 103.995603, 1.35191714); + + flightPath.addFlightPoint(startPoint); + flightPath.addFlightPoint(endPoint); + + RoutePath routePath = flightPath.getRoutePath(); + assertTrue(routePath.getRoute().get(0).lat == 172.5336822); + assertTrue(routePath.getRoute().get(0).lng == -43.48664019); + assertTrue(routePath.getRoute().get(1).lat == 103.995603); + assertTrue(routePath.getRoute().get(1).lng == 1.35191714); + } +} \ No newline at end of file