Added some index checks and relavent Tests for them
This commit is contained in:
@@ -2,6 +2,7 @@ package seng202.group9.Core;
|
||||
|
||||
import seng202.group9.Controller.DataException;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@@ -251,8 +252,12 @@ public class Airline{
|
||||
* deletes a route by matching index keep in mind that the array position will change for all indexs after this.
|
||||
* @param index
|
||||
*/
|
||||
public void delRoutes(int index){
|
||||
public void delRoutes(int index) throws DataException{
|
||||
if (routes.size() > index && index >= 0) {
|
||||
routes.remove(index);
|
||||
}else{
|
||||
throw new DataException("The Route at index "+index+ "does not exist to be deleted.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@ package seng202.group9.Core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javafx.scene.chart.PieChart;
|
||||
import seng202.group9.Controller.DataException;
|
||||
/**
|
||||
* Airport Class
|
||||
@@ -363,8 +364,12 @@ public class Airport {
|
||||
* deletes a member of arrival routes by index
|
||||
* @param index
|
||||
*/
|
||||
public void delArrivalRoutes(int index){
|
||||
public void delArrivalRoutes(int index) throws DataException{
|
||||
if (arrivalRoutes.size() > index && index >= 0) {
|
||||
arrivalRoutes.remove(index);
|
||||
}else{
|
||||
throw new DataException("Index "+index+" of number of Arrival Routes array size.");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* deletes a member of departure routes by matching route pointer
|
||||
@@ -378,8 +383,12 @@ public class Airport {
|
||||
* deletes a member of departure routes by index
|
||||
* @param index
|
||||
*/
|
||||
public void delDepartureRoutes(int index){
|
||||
public void delDepartureRoutes(int index) throws DataException{
|
||||
if (departureRoutes.size() > index && index >= 0) {
|
||||
departureRoutes.remove(index);
|
||||
}else{
|
||||
throw new DataException("Index "+index+" is out of number of Departure Routes array size.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package seng202.group9.Core;
|
||||
|
||||
import seng202.group9.Controller.DataException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class City {
|
||||
@@ -137,9 +139,11 @@ public class City {
|
||||
* Deletes an Airport by Index from this City.
|
||||
* @param index
|
||||
*/
|
||||
public void delAirport(int index) {
|
||||
if (airports.size() > index) {
|
||||
public void delAirport(int index) throws DataException{
|
||||
if (airports.size() > index && index >= 0) {
|
||||
airports.remove(index);
|
||||
}else{
|
||||
throw new DataException("Index "+index+" is out of number of City Airports array size.");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -11,6 +11,7 @@ import seng202.group9.Core.Route;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Test the functions for the Airline
|
||||
@@ -102,13 +103,33 @@ public class AirlineTest{
|
||||
assertEquals(allNipponAirways.getRoutes().get(1), route2);
|
||||
assertEquals(allNipponAirways.getRoutes().get(2), route1);
|
||||
assertEquals(allNipponAirways.getRoutes().get(3), route2);
|
||||
|
||||
try {
|
||||
allNipponAirways.delRoutes(3);
|
||||
}catch (DataException e){
|
||||
fail("3 is a valid deletable index.");
|
||||
}
|
||||
assertTrue(allNipponAirways.getRoutes().size() == 3);
|
||||
|
||||
allNipponAirways.delRoutes(route2);
|
||||
assertTrue(allNipponAirways.getRoutes().size() == 2);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void testInvalidRouteDelete() throws DataException{
|
||||
//ID, Name, Alias, IATA, ICAO, CallSign, Country, Active
|
||||
//324,"All Nippon Airways","ANA All Nippon Airways","NH","ANA","ALL NIPPON","Japan","Y"
|
||||
Airline allNipponAirways = new Airline(324, "All Nippon Airways", "ANA All Nippon Airways",
|
||||
"NH", "ANA", "ALL NIPPON", "Japan", "Y");
|
||||
allNipponAirways.delRoutes(0);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void testInvalidIndexRouteDelete() throws DataException{
|
||||
//ID, Name, Alias, IATA, ICAO, CallSign, Country, Active
|
||||
//324,"All Nippon Airways","ANA All Nippon Airways","NH","ANA","ALL NIPPON","Japan","Y"
|
||||
Airline allNipponAirways = new Airline(324, "All Nippon Airways", "ANA All Nippon Airways",
|
||||
"NH", "ANA", "ALL NIPPON", "Japan", "Y");
|
||||
allNipponAirways.delRoutes(-1);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
|
||||
@@ -100,7 +100,12 @@ public class AirportUnitTest {
|
||||
assertEquals(heathrow.getArrivalRoutes().get(2).getArrivalAirport(), "LHR2");
|
||||
assertEquals(heathrow.getArrivalRoutes().get(3).getArrivalAirport(), "LHR3");
|
||||
|
||||
try {
|
||||
heathrow.delArrivalRoutes(0);
|
||||
} catch (DataException e) {
|
||||
|
||||
}
|
||||
|
||||
assertTrue(heathrow.getArrivalRoutes().size() == 3);
|
||||
heathrow.delArrivalRoutes(heathrow.getArrivalRoutes().get(0));
|
||||
assertTrue(heathrow.getArrivalRoutes().size() == 2);
|
||||
@@ -114,7 +119,11 @@ public class AirportUnitTest {
|
||||
assertEquals(heathrow.getDepartureRoutes().get(2).getDepartureAirport(), "SIN2");
|
||||
assertEquals(heathrow.getDepartureRoutes().get(3).getDepartureAirport(), "SIN3");
|
||||
|
||||
try {
|
||||
heathrow.delDepartureRoutes(0);
|
||||
} catch (DataException e) {
|
||||
|
||||
}
|
||||
assertTrue(heathrow.getDepartureRoutes().size() == 3);
|
||||
heathrow.delDepartureRoutes(heathrow.getDepartureRoutes().get(0));
|
||||
assertTrue(heathrow.getDepartureRoutes().size() == 2);
|
||||
@@ -214,6 +223,38 @@ public class AirportUnitTest {
|
||||
assertEquals(heathrow.getID(), 544);//check ID no id should be thrown
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void checkDelArrivalNoIndexFailure() throws DataException{
|
||||
//507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
|
||||
//ID, NaWme, City, Country, IATA/FFA, ICAO, Latitude, Longitude, Altitude, Timezone, DST, Tz Data
|
||||
Airport heathrow = new Airport("Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83);
|
||||
heathrow.delArrivalRoutes(0);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void checkDelArrivalNegativeIndexFailure() throws DataException{
|
||||
//507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
|
||||
//ID, NaWme, City, Country, IATA/FFA, ICAO, Latitude, Longitude, Altitude, Timezone, DST, Tz Data
|
||||
Airport heathrow = new Airport("Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83);
|
||||
heathrow.delArrivalRoutes(-1);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void checkDelDepartureNoIndexFailure() throws DataException{
|
||||
//507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
|
||||
//ID, NaWme, City, Country, IATA/FFA, ICAO, Latitude, Longitude, Altitude, Timezone, DST, Tz Data
|
||||
Airport heathrow = new Airport("Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83);
|
||||
heathrow.delDepartureRoutes(0);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void checkDelDepatureNegativeIndexFailure() throws DataException{
|
||||
//507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
|
||||
//ID, NaWme, City, Country, IATA/FFA, ICAO, Latitude, Longitude, Altitude, Timezone, DST, Tz Data
|
||||
Airport heathrow = new Airport("Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83);
|
||||
heathrow.delDepartureRoutes(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkToString(){
|
||||
//507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London"
|
||||
|
||||
@@ -5,6 +5,7 @@ package seng202.group9;/**
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import seng202.group9.Controller.DataException;
|
||||
import seng202.group9.Core.Airport;
|
||||
import seng202.group9.Core.City;
|
||||
|
||||
@@ -49,17 +50,31 @@ public class CityTest {
|
||||
airports.add(matsumotoAirport);
|
||||
matsumoto.addAirport(airports);
|
||||
assertTrue(matsumoto.getAirports().size() == 2);
|
||||
try {
|
||||
matsumoto.delAirport(0);
|
||||
assertTrue(matsumoto.getAirports().size() == 1);
|
||||
matsumoto.delAirport(0);
|
||||
//try to remove more than the city has for airports
|
||||
matsumoto.delAirport(0);
|
||||
}catch (DataException e){
|
||||
|
||||
}
|
||||
matsumoto.delAirport(matsumotoAirport);
|
||||
//set the airports
|
||||
matsumoto.setAirports(airports);
|
||||
assertTrue(matsumoto.getAirports().size() == 2);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void testDelAirportsMissingIndexErrors() throws DataException{
|
||||
City matsumoto = new City("Matsumoto", "Japan", 9, "Asia/Tokyo");
|
||||
matsumoto.delAirport(0);
|
||||
}
|
||||
|
||||
@Test(expected = DataException.class)
|
||||
public void testDelAirportsNegativeIndexErrors() throws DataException{
|
||||
City matsumoto = new City("Matsumoto", "Japan", 9, "Asia/Tokyo");
|
||||
matsumoto.delAirport(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString(){
|
||||
City matsumoto = new City("Matsumoto", "Japan", 9, "Asia/Tokyo");
|
||||
|
||||
Reference in New Issue
Block a user