package planner.test; import org.junit.*; import java.util.*; import planner.*; /** * Basic tests for the {@link Route} implementation class. * * A more extensive test suite will be performed for assessment of your code, * but this should get you started writing your own unit tests. */ public class RouteTestAdvanced { private String[] nullStationsElement = {"One", "Two", "Three", null, "Five"}; private String[] zeroStations = {}; private String[] oneStations = {"One"}; private String[] twoStations = {"One", "Two"}; private String[] twoStationsSame = {"One", "One"}; private String[] threeStationsSame = {"One", "One", "Two"}; private String[] tenStations = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}; private String[] tenStationsSame = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Nine"}; private String name = "Some Route Name"; private int tenStationsCount = 10; private Station notIn = new Station("Eleven"); private Station in = new Station("Six"); private int zerothStop = 0; private int firstStop = 1; private int tenthStop = 10; private int eleventhStop = 11; private Station s1 = new Station("One"); private Station s2 = new Station("Two"); private String outputOne = "Some Route Name: One, Two"; private String outputTwo = "Some Route Name: One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten"; @Test(expected = NullPointerException.class) public void nullNameTest() { ArrayList properStationsList = getStations(twoStations, 0, 2); Route testRoute = new Route(null, properStationsList); } @Test(expected = NullPointerException.class) public void nullStationListTest() { Route testRoute = new Route(name, null); } @Test(expected = NullPointerException.class) public void nullStationListElementTest() { ArrayList improperStationsList = getStations(nullStationsElement, 0, 5); Route testRoute = new Route(name, improperStationsList); } @Test(expected = InvalidRouteException.class) public void NoStopsTest() { ArrayList zeroStationsList = getStations(zeroStations, 0, 0); Route testRoute = new Route(name, zeroStationsList); } @Test(expected = InvalidRouteException.class) public void OneStopsTest() { ArrayList oneStationsList = getStations(oneStations, 0, 1); Route testRoute = new Route(name, oneStationsList); } @Test public void TwoStopsTest() { ArrayList twoStationsList = getStations(twoStations, 0, 2); Route testRoute = new Route(name, twoStationsList); } @Test(expected = InvalidRouteException.class) public void TwoStopsSameTest() { ArrayList twoStationsSameList = getStations(twoStationsSame, 0, 2); Route testRoute = new Route(name, twoStationsSameList); } @Test(expected = InvalidRouteException.class) public void ThreeStopsSameTest() { ArrayList threeStationsSameList = getStations(threeStationsSame, 0, 3); Route testRoute = new Route(name, threeStationsSameList); } @Test public void TenStopsTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); } @Test(expected = InvalidRouteException.class) public void TenStopsSameTest() { ArrayList tenStationsSameList = getStations(tenStationsSame, 0, 10); Route testRoute = new Route(name, tenStationsSameList); } @Test public void ReturnNameTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect Name", "Some Route Name", testRoute.name()); } @Test public void ReturnNumberStopsTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect Size", 10, testRoute.numStops()); } @Test public void StopsAtTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, stop is in this route.", true, testRoute.stopsAt(in)); } @Test public void DoesNotStopAtTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, stop is not in this route.", false, testRoute.stopsAt(notIn)); } @Test(expected = NoSuchStopException.class) public void InvalidMinGetStopNumberTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); testRoute.getStopNumber(new Station("Zero")); } @Test public void ValidMinGetStopNumberTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, stop number should be 1", 1, testRoute.getStopNumber(new Station("One"))); } @Test(expected = NoSuchStopException.class) public void InvalidMaxGetStopNumberTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); testRoute.getStopNumber(new Station("Eleven")); } @Test public void ValidMaxGetStopNumberTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, stop number should be 10", 10, testRoute.getStopNumber(new Station("Ten"))); } @Test public void SameStopCanTravelTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, cannot travel from one stop to the same stop", false, testRoute.canTravelFrom(s1, s1)); } @Test public void BeforeStopCanTravelTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, cannot travel from one stop to a stop previous in the route", false, testRoute.canTravelFrom(s2, s1)); } @Test public void AfterStopCanTravelTest() { ArrayList tenStationsList = getStations(tenStations, 0, 10); Route testRoute = new Route(name, tenStationsList); Assert.assertEquals("Incorrect, can travel from one stop to the next stop", true, testRoute.canTravelFrom(s1, s2)); } // Other helper methods /** * Creates a list stations from the names stationNames[first] to * stationNames[last-1], where first >= 0 and last <= the length of * stationNames. */ private ArrayList getStations(String[] stationNames, int first, int last) { // the stations to return ArrayList stations = new ArrayList(); for (int i = first; i < last; i++) { stations.add(new Station(stationNames[i])); } return stations; } }