From c92f8e5c547499e33b38dd3540f988c07a20520e Mon Sep 17 00:00:00 2001 From: YaFedImYaEatIm Date: Sun, 18 Sep 2016 21:22:35 +1200 Subject: [PATCH] Added test cases for missing Functiosn in Airport --- .../java/seng202/group9/Core/Airport.java | 15 ++-- .../java/seng202/group9/AirportUnitTest.java | 88 +++++++++++++++++-- 2 files changed, 89 insertions(+), 14 deletions(-) diff --git a/src/main/java/seng202/group9/Core/Airport.java b/src/main/java/seng202/group9/Core/Airport.java index f252c55..bb9dba5 100644 --- a/src/main/java/seng202/group9/Core/Airport.java +++ b/src/main/java/seng202/group9/Core/Airport.java @@ -376,8 +376,10 @@ public class Airport { double distance = 0; double dLong = this.longitude - airport.getLongitude(); double dLat = this.latitude - airport.getLatitude(); - double a = Math.pow((Math.sin(dLat/2)), 2) + Math.cos(this.latitude) * Math.cos(airport.getLatitude()) * Math.pow(Math.sin(dLong/2), 2); - double c = a * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + dLong = Math.toRadians(dLong); + dLat = Math.toRadians(dLat); + double a = Math.pow((Math.sin(dLat/2)), 2) + Math.cos(Math.toRadians(this.latitude)) * Math.cos(Math.toRadians(airport.getLatitude())) * Math.pow(Math.sin(dLong/2), 2); + double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); distance = 6371 * c; return distance; } @@ -388,13 +390,16 @@ public class Airport { * @throws DataException */ public void hasDuplicate(Airport airport) throws DataException{ - if (airport.getName().equals("") || airport.getName().equals(this.name)){ + if (airport.getName().equals(this.name)){ throw new DataException("Airport Name already Exists, Please Choose Another."); } - if (!airport.getIATA_FFA().equals("") && airport.getIATA_FFA().equals(this.name)){ + if (airport.getName().equals("")){ + throw new DataException("Airport name cannot be Empty. Please Choose Another."); + } + if (!airport.getIATA_FFA().equals("") && airport.getIATA_FFA().equals(this.IATA_FFA)){ throw new DataException("Airport IATA/FFA already Exists, Please Choose Another."); } - if (!airport.getICAO().equals("") && airport.getICAO().equals(this.name)){ + if (!airport.getICAO().equals("") && airport.getICAO().equals(this.ICAO)){ throw new DataException("Airport ICAO already Exists, Please Choose Another."); } } diff --git a/src/test/java/seng202/group9/AirportUnitTest.java b/src/test/java/seng202/group9/AirportUnitTest.java index 66177d8..625eae8 100644 --- a/src/test/java/seng202/group9/AirportUnitTest.java +++ b/src/test/java/seng202/group9/AirportUnitTest.java @@ -7,6 +7,8 @@ import org.junit.rules.ExpectedException; import seng202.group9.Controller.DataException; import seng202.group9.Core.Airport; +import seng202.group9.Core.City; +import seng202.group9.Core.Country; import seng202.group9.Core.Route; import java.util.ArrayList; @@ -121,17 +123,85 @@ public class AirportUnitTest { } @Test - public void checkIDException(){ + public void testCountryAndCity(){ + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Country UnitedKing = new Country("E", "United Kingdom"); + heathrow.setCountry(UnitedKing); + City london = new City("London", "United Kingdom", 0, "Europe/London"); + UnitedKing.addCities(london); + heathrow.setCity(london); + assertEquals(heathrow.getCity().getName(), "London"); + assertEquals(heathrow.getCity().getCountry(), "United Kingdom"); + assertEquals(heathrow.getCity().getTimeOlson(), "Europe/London"); + assertTrue(heathrow.getCity().getTimezone() == 0); + //country tests + assertEquals(heathrow.getCountry().getDST(), "E"); + assertEquals(heathrow.getCountry().getName(), "United Kingdom"); + //check if the country has city london + assertEquals(heathrow.getCountry().getCities().get(0), london); + //check the get commands from the airport city + assertEquals(heathrow.getDST(), "E"); + assertEquals(heathrow.getDST(), heathrow.getCountry().getDST()); + assertEquals(heathrow.getCityName(), heathrow.getCity().getName()); + assertEquals(heathrow.getCity().getCountry(), heathrow.getCountryName()); + assertEquals(heathrow.getCity().getCountry(), "United Kingdom"); + assertEquals(heathrow.getTz(), "Europe/London"); + assertEquals(heathrow.getTz(), heathrow.getCity().getTimeOlson()); + assertTrue(heathrow.getTimezone() == 0); + assertTrue(heathrow.getTimezone() == heathrow.getCity().getTimezone()); + //check the get commands for airport countries + assertEquals(heathrow.getCountryName(), heathrow.getCountry().getName()); + assertEquals(heathrow.getCountry().getDST(), heathrow.getDST()); + assertEquals(heathrow.getCountry().getDST(), "E"); + } + + @Test + public void checkCalculateDistance(){ + //check whether the airport distance between teh same airport is 0 + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Airport heathrow2 = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + assertTrue(heathrow.calculateDistance(heathrow2) == 0); + //check the correct distance between this and Edmonton + Airport edmonton = new Airport(26,"Kugaaruk","Pelly Bay","Canada","YBB","CYBB",68.534444,-89.808056,56); + //must be correct with a 0.3% margin of error calculations from http://www.movable-type.co.uk/scripts/latlong.html + assertTrue(heathrow.calculateDistance(edmonton) <= 4789 * 1.003); + assertTrue(heathrow.calculateDistance(edmonton) >= 4789 * 0.997); + } + + @Test(expected = DataException.class) + public void checkNameDuplicate() throws DataException{ + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Airport heathrow2 = new Airport(507, "Heathrow", "Londons", "United Ksingdom", "LRR", "EPLL", 51.4775, -0.41389, 83); + heathrow.hasDuplicate(heathrow2); + } + + @Test(expected = DataException.class) + public void checkEmptyNameDuplicate() throws DataException{ + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Airport heathrow2 = new Airport(507, "", "Londons", "United Ksingdom", "LRR", "EPLL", 51.4775, -0.41389, 83); + heathrow.hasDuplicate(heathrow2); + } + + @Test(expected = DataException.class) + public void checkIATADuplicate() throws DataException{ + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Airport heathrow2 = new Airport(507, "Heathrsow", "Lossndon", "United Kisssngdom", "LHR", "EKLL", 51.4775, -0.41389, 83); + heathrow.hasDuplicate(heathrow2); + } + + @Test(expected = DataException.class) + public void checkICAODuplicate() throws DataException{ + Airport heathrow = new Airport(507, "Heathrow", "London", "United Kingdom", "LHR", "EGLL", 51.4775, -0.41389, 83); + Airport heathrow2 = new Airport(507, "Heathrows", "Lonsdon", "United Kisngdom", "LJR", "EGLL", 51.4775, -0.41389, 83); + heathrow.hasDuplicate(heathrow2); + } + + @Test(expected = DataException.class) + public void checkIDException() throws DataException{ //507,"Heathrow","London","United Kingdom","LHR","EGLL",51.4775,-0.461389,83,0,"E","Europe/London" - //ID, Name, City, Country, IATA/FFA, ICAO, Latitude, Longitude, Altitude, Timezone, DST, Tz Data + //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); - thrown.expect(DataException.class); - thrown.expectMessage("ID not set."); - try { - assertEquals(heathrow.getID(), 544);//check ID no id should be thrown - } catch (DataException e) { - - } + assertEquals(heathrow.getID(), 544);//check ID no id should be thrown } }