Added a number of race tests.

- Still unable to get animation timer tests working. May be necessary to make handle() hold nothing but a method call

#story[881]
main
Erika Savell 9 years ago
parent 0bb18c7957
commit 11f6dee301

@ -208,8 +208,8 @@ public class Race implements Runnable {
//distanceTravelled = velocity (nm p hr) * time taken to update loop
double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000;
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
System.out.println(totalDistanceTravelled);
boolean finish = boat.getCurrentLeg().getName().equals("Finish");
if (!finish) {
@ -225,11 +225,14 @@ public class Race implements Runnable {
protected void checkPosition(Boat boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
//boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished
boatsFinished++;
boat.setTimeFinished(timeElapsed);
boat.setTimeFinished(timeElapsed);
boat.setVelocity(0);
boat.setScaledVelocity(0);
} else if (doNotFinish()) {
boatsFinished++;
boat.setTimeFinished(timeElapsed);
@ -241,10 +244,7 @@ public class Race implements Runnable {
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
//Move boat on to next leg
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg);
//Add overshoot distance into the distance travelled for the next leg
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
}
}
}

@ -1,7 +1,7 @@
package seng302.Model;
import javafx.scene.paint.Color;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
@ -17,11 +17,9 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
@ -30,6 +28,24 @@ import static org.mockito.Mockito.*;
*/
public class RaceTest{
public static final Marker ORIGIN = new Marker(new GPSCoordinate(0, 0));
public static final Marker THREE_NM_FROM_ORIGIN = new Marker(new GPSCoordinate(0.050246769, 0));
public static final Marker FIFTEEN_NM_FROM_ORIGIN = new Marker(new GPSCoordinate(0.251233845, 0));
public static ArrayList<Leg> TEST_LEGS = new ArrayList<>();
public static final int START_LEG_DISTANCE = 3;
public static final int MIDDLE_LEG_DISTANCE = 12;
public static final Leg START_LEG = new Leg("Start", ORIGIN, THREE_NM_FROM_ORIGIN, 0);
public static final Leg MIDDLE_LEG = new Leg("Middle", THREE_NM_FROM_ORIGIN, FIFTEEN_NM_FROM_ORIGIN, 1);
public static final Leg FINISH_LEG = new Leg("Finish", FIFTEEN_NM_FROM_ORIGIN, FIFTEEN_NM_FROM_ORIGIN, 2);
@Before
public void setUp() {
TEST_LEGS.add(START_LEG);
TEST_LEGS.add(MIDDLE_LEG);
TEST_LEGS.add(FINISH_LEG);
}
@Ignore
@Test
public void countdownTimerSendsBoatLocations() {
@ -67,145 +83,208 @@ public class RaceTest{
}
}
// @Test
// public void checkPositionUpdatesNumberFinishedBoats() {
//
// Boat finishedBoat = new Boat(1000, "Name", "Country");
// finishedBoat.setDistanceTravelledInLeg(500);
//
// finishedBoat.setCurrentLeg(FINISH_LEG);
//
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(finishedBoat);
//
// ArrayList<Leg> legs = new ArrayList<>();
// legs.add(FINISH_LEG);
//
// Race race = new Race(boats, legs, 5, mockOutput);
// assertEquals(race.boatsFinished, 0);
//
// race.checkPosition(finishedBoat, 100000);
// assertEquals(race.boatsFinished, 1);
// assertEquals(finishedBoat.getTimeFinished(), 100000);
// }
// @Test
// public void checkPositionDoesntUpdateNumberFinishedBoats() {
//
// Boat unFinishedBoat = new Boat("Test", 10, Color.ALICEBLUE, "tt", 1);
// unFinishedBoat.setDistanceTravelledInLeg(0);
//
// unFinishedBoat.setCurrentLeg(FINISH_LEG);
//
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(unFinishedBoat);
//
// ArrayList<Leg> legs = new ArrayList<>();
// legs.add(FINISH_LEG);
//
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.setDnfChance(0);
// assertEquals(race.boatsFinished, 0);
// race.checkPosition(unFinishedBoat, 100);
// assertEquals(race.boatsFinished, 0);
//
// }
//
// @Test
// public void distanceTravelledBeforeUpdatingLegIsRetained() {
//
// ArrayList<Boat> boats = new ArrayList<>();
//
// ArrayList<Leg> legs = new ArrayList<>();
//
// legs.add(START_LEG);
// legs.add(FINISH_LEG);
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.setDnfChance(0);
//
// Boat unFinishedBoat = new Boat("Test", 10, Color.ALICEBLUE, "tt", 4);
// unFinishedBoat.setDistanceTravelledInLeg(100);
// unFinishedBoat.setCurrentLeg(START_LEG);
//
// race.checkPosition(unFinishedBoat, 100);
// assertEquals(unFinishedBoat.getCurrentLeg().getName(), "Finish");
// assertTrue(unFinishedBoat.getDistanceTravelledInLeg() > 0);
// assertTrue(unFinishedBoat.getDistanceTravelledInLeg() < 100);
// }
//
// @Ignore
// @Test
// public void timerDelaysByHalfSecond() throws InterruptedException {
//
// ArrayList<Boat> boats = new ArrayList<>();
//
// ArrayList<Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
//
// Race race = new Race(boats, legs, 1, mockOutput);
// race.PRERACE_TIME = 500;
// race.runRace = false;
//
// race.countdownTimer();
//
// long timeStarted = System.currentTimeMillis();
// long currentTime = System.currentTimeMillis();
// while (!race.countdownFinish) {
// currentTime = System.currentTimeMillis();
// }
//
// assertTrue(currentTime - timeStarted > 500);
// }
//
// @Test
// public void scalerScalesVelocityCorrectly() {
//
// int scaleFactor = 3;
// float vel1 = 0;
// float vel2 = (float) 1.999;
// float vel3 = (float) 32.5;
// float vel4 = 500;
// Boat boat1 = new Boat("test", vel1, Color.ALICEBLUE, "tt",1);
// Boat boat2 = new Boat("test", vel2, Color.ALICEBLUE, "tt", 2);
// Boat boat3 = new Boat("test", vel3, Color.ALICEBLUE, "tt", 3);
// Boat boat4 = new Boat("test", vel4, Color.ALICEBLUE, "tt", 4);
// ArrayList<Boat> boats = new ArrayList<>();
// boats.add(boat1);
// boats.add(boat2);
// boats.add(boat3);
// boats.add(boat4);
//
// ArrayList<Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
//
//
// Race race = new Race(boats, legs, scaleFactor, mockOutput);
// race.setDnfChance(0);
//
// assertEquals(race.getStartingBoats().get(0).getScaledVelocity(), vel1 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(1).getScaledVelocity(), vel2 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(2).getScaledVelocity(), vel3 * scaleFactor, 1e-6);
// assertEquals(race.getStartingBoats().get(3).getScaledVelocity(), vel4 * scaleFactor, 1e-6);
//
// }
// @Test
// public void doNotFinishCorrectly() {
// ArrayList<Boat> boats = new ArrayList<>();
//
// ArrayList<Leg> legs = new ArrayList<>();
// legs.add(START_LEG);
//
// Race race = new Race(boats, legs, 1, mockOutput);
//
// race.setDnfChance(100);
// assertTrue(race.doNotFinish());
//
// race.setDnfChance(0);
// assertTrue(!race.doNotFinish());
// }
@Test
public void checkPositionFinishedUpdatesNumberFinishedBoats() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
RaceDataSource dataSource = new RaceXMLReader("mockXML/raceTest.xml", new BoatXMLReader("mockXML/boatTest.xml"));
Race testRace = new Race(dataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(FINISH_LEG);
testBoat.setDistanceTravelledInLeg(1);
testRace.checkPosition(testBoat, 1);
assertEquals(testRace.boatsFinished, 1);
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void checkPositionSetFinishedBoatVelocityTo0() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
RaceDataSource dataSource = new RaceXMLReader("mockXML/raceTest.xml", new BoatXMLReader("mockXML/boatTest.xml"));
Race testRace = new Race(dataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(FINISH_LEG);
testBoat.setDistanceTravelledInLeg(1);
testRace.checkPosition(testBoat, 1);
assertEquals(testBoat.getVelocity(), 0, 1e-8);
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void checkPositionSetsFinishTime() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
RaceDataSource dataSource = new RaceXMLReader("mockXML/raceTest.xml", new BoatXMLReader("mockXML/boatTest.xml"));
Race testRace = new Race(dataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(FINISH_LEG);
testBoat.setDistanceTravelledInLeg(1);
testRace.checkPosition(testBoat, 1);
assertEquals(testBoat.getTimeFinished(), 1, 1e-8);
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void checkPositionUnfinishedDoesntUpdateNumberFinishedBoats() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
RaceDataSource dataSource = new RaceXMLReader("mockXML/raceTest.xml", new BoatXMLReader("mockXML/boatTest.xml"));
Race testRace = new Race(dataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(START_LEG);
testBoat.setDistanceTravelledInLeg(START_LEG_DISTANCE);
testRace.checkPosition(testBoat, 1);
assertEquals(testRace.boatsFinished, 0);
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void distanceTravelledBeforeUpdatingLegIsRetained() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
RaceDataSource dataSource = new RaceXMLReader("mockXML/raceTest.xml", new BoatXMLReader("mockXML/boatTest.xml"));
Race testRace = new Race(dataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(START_LEG);
testBoat.setDistanceTravelledInLeg(START_LEG_DISTANCE + 1);
testRace.checkPosition(testBoat, 0);
assertEquals(testBoat.getDistanceTravelledInLeg(), 1, 1e-7);
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void doNotFinishAnswersYesIf100PercentChance() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
BoatDataSource boatDataSource = new BoatXMLReader("mockXML/boatTest.xml");
RaceDataSource raceDataSource = new RaceXMLReader("mockXML/raceTest.xml", boatDataSource);
Race testRace = new Race(raceDataSource, mockOutput);
testRace.setDnfChance(100);
assertTrue(testRace.doNotFinish());
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void doNotFinishAnswersNoIf0PercentChance() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
BoatDataSource boatDataSource = new BoatXMLReader("mockXML/boatTest.xml");
RaceDataSource raceDataSource = new RaceXMLReader("mockXML/raceTest.xml", boatDataSource);
Race testRace = new Race(raceDataSource, mockOutput);
testRace.setDnfChance(0);
assertFalse(testRace.doNotFinish());
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void boatsAreSetToDNF() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
BoatDataSource boatDataSource = new BoatXMLReader("mockXML/boatTest.xml");
RaceDataSource raceDataSource = new RaceXMLReader("mockXML/raceTest.xml", boatDataSource);
Race testRace = new Race(raceDataSource, mockOutput);
testRace.setDnfChance(100);
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(START_LEG);
testBoat.setDistanceTravelledInLeg(START_LEG_DISTANCE + 1);
testRace.checkPosition(testBoat, 1);
assertEquals(testBoat.getCurrentLeg().getName(), "DNF");
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void updatePositionIgnoresFinishedBoats() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
BoatDataSource boatDataSource = new BoatXMLReader("mockXML/boatTest.xml");
RaceDataSource raceDataSource = new RaceXMLReader("mockXML/raceTest.xml", boatDataSource);
Race testRace = new Race(raceDataSource, mockOutput);
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(FINISH_LEG);
testBoat.setCurrentPosition(ORIGIN.getAverageGPSCoordinate());
testRace.updatePosition(testBoat, 1);
assertEquals(testBoat.getCurrentPosition(), ORIGIN.getAverageGPSCoordinate());
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
@Test
public void updatePositionChangesBoatPosition() {
try {
MockOutput mockOutput = Mockito.mock(MockOutput.class);
BoatDataSource boatDataSource = new BoatXMLReader("mockXML/boatTest.xml");
RaceDataSource raceDataSource = new RaceXMLReader("mockXML/raceTest.xml", boatDataSource);
Race testRace = new Race(raceDataSource, mockOutput);
testRace.initialiseBoats();
Boat testBoat = testRace.startingBoats.get(0);
testBoat.setCurrentLeg(START_LEG);
testBoat.setDistanceTravelledInLeg(START_LEG_DISTANCE - 1);
testBoat.setCurrentPosition(ORIGIN.getAverageGPSCoordinate());
testRace.updatePosition(testBoat, 100);
assertFalse(testBoat.getCurrentPosition() == ORIGIN.getAverageGPSCoordinate());
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
fail();
}
}
}

Loading…
Cancel
Save