Merge branch 'master' into MapWindRotation

# Conflicts:
#	racevisionGame/src/main/java/mock/app/Event.java
#	racevisionGame/src/main/java/mock/model/MockRace.java
#	racevisionGame/src/main/java/mock/model/commandFactory/CommandFactory.java
#	racevisionGame/src/main/java/shared/dataInput/RaceXMLReader.java
#	racevisionGame/src/main/java/shared/model/CompoundMark.java
#	racevisionGame/src/main/java/shared/model/Mark.java
#	racevisionGame/src/main/java/visualiser/Controllers/ConnectionController.java
#	racevisionGame/src/main/java/visualiser/Controllers/HostController.java
#	racevisionGame/src/main/java/visualiser/app/VisualiserInput.java
#	racevisionGame/src/main/java/visualiser/gameController/ControllerServer.java
#	racevisionGame/src/main/java/visualiser/model/VisualiserRace.java
This commit is contained in:
Joseph Gardner
2017-08-17 11:46:09 +12:00
115 changed files with 7745 additions and 2288 deletions
@@ -14,6 +14,13 @@ import static org.junit.Assert.fail;
public class PolarParserTest {
public static Polars createPolars() {
Polars polars = PolarParser.parse("mock/polars/acc_polars.csv");
return polars;
}
/**
* Tests if we can parse a valid polar data file (stored in a string), and create a polar table.
*/
@@ -0,0 +1,72 @@
package mock.model;
import org.junit.Before;
import org.junit.Test;
import shared.model.Bearing;
import shared.model.Wind;
import static org.junit.Assert.*;
/**
* Tests the {@link ConstantWindGenerator}.
*/
public class ConstantWindGeneratorTest {
WindGenerator windGenerator;
Bearing windBearing;
double windSpeedKnots;
@Before
public void setUp() throws Exception {
windBearing = Bearing.fromDegrees(78.5);
windSpeedKnots = 18.54;
windGenerator = new ConstantWindGenerator(windBearing, windSpeedKnots);
}
/**
* Tests if the {@link WindGenerator#generateBaselineWind()} function works.
*/
@Test
public void generateBaselineTest() {
int repetitions = 100;
for (int i = 0; i < repetitions; i++) {
Wind wind = windGenerator.generateBaselineWind();
assertEquals(windBearing.degrees(), wind.getWindDirection().degrees(), 0.01);
assertEquals(windSpeedKnots, wind.getWindSpeed(), 0.01);
}
}
/**
* Tests if the {@link WindGenerator#generateNextWind(Wind)} ()} function works.
*/
@Test
public void generateNextWindTest() {
int repetitions = 100;
Wind wind = windGenerator.generateBaselineWind();
for (int i = 0; i < repetitions; i++) {
wind = windGenerator.generateNextWind(wind);
assertEquals(windBearing.degrees(), wind.getWindDirection().degrees(), 0.01);
assertEquals(windSpeedKnots, wind.getWindSpeed(), 0.01);
}
}
}
@@ -1,7 +1,174 @@
package mock.model;
import static org.junit.Assert.*;
import mock.dataInput.PolarParser;
import mock.exceptions.InvalidPolarFileException;
import org.junit.Before;
import org.junit.Test;
import shared.model.Bearing;
import shared.model.CompoundMark;
import shared.model.GPSCoordinate;
import shared.model.Mark;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class MockBoatTest {
//TODO
/**
* boat made for testing
*/
private MockBoat firstTestBoat;
private Mark markToTest;
private Mark markToTest2;
private GPSCoordinate highGPS;
private GPSCoordinate middleGPS;
private GPSCoordinate lowGPS;
/**
* Creates the Polars object for the tests.
*/
@Before
public void setUp() {
//Read in polars.
try {
//Parse data file.
Polars polars = PolarParser.parse("mock/polars/acc_polars.csv");
firstTestBoat = new MockBoat(1, "test", "NZ", polars);
highGPS = new GPSCoordinate(32.296577, -64.854000);
middleGPS = new GPSCoordinate(32.292500, -64.854000);
lowGPS = new GPSCoordinate(32.290000, -64.854000);
markToTest = new Mark(1, "test MARK", middleGPS);
markToTest2 = new Mark(2, "test MARK2", middleGPS);
}
catch (InvalidPolarFileException e) {
fail("Couldn't parse polar file.");
}
}
//////////////////////////////Mark Higher////////////////////////////////
/**
* Tests if the boat is lower than the mark that the port side method works if
* boat is facing east
*/
@Test
public void testIsPortSide() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), true);
}
/**
* Tests if the boat is lower than the mark that the port side method works if
* boat is facing west
*/
@Test
public void testIsPortSideWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), false);
}
/**
* Tests if the boat is lower than the mark that the starboard side method works if
* boat is facing east
*/
@Test
public void testIsStarboardSideWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), false);
}
/**
* Tests if the boat is lower than the mark that the starboard side method works if
* boat is facing west
*/
@Test
public void testIsStarboardSide() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), true);
}
//////////////////////////////Mark Lower////////////////////////////////
/**
* Tests if the boat is higher than the mark that the port side method works if
* boat is facing east
*/
@Test
public void testIsPortSideHigherWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), false);
}
/**
* Tests if the boat is higher than the mark that the port side method works if
* boat is facing west
*/
@Test
public void testIsPortSideHigher() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), true);
}
/**
* Tests if the boat is higher than the mark that the starboard side method works if
* boat is facing east
*/
@Test
public void testIsStarboardSideHigher() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), true);
}
/**
* Tests if the boat is higher than the mark that the starboard side method works if
* boat is facing west
*/
@Test
public void testIsStarboardSideHigherWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), false);
}
/**
* Tests if a boat is between a gate
*/
@Test
public void testIsBetweenGate(){
markToTest.setPosition(highGPS);
markToTest2.setPosition(lowGPS);
CompoundMark testGate = new CompoundMark(1, "test GATE", markToTest, markToTest2);
firstTestBoat.setCurrentPosition(middleGPS);
assertEquals(firstTestBoat.isBetweenGate(testGate), true);
}
}
@@ -1,7 +1,43 @@
package mock.model;
import mock.dataInput.PolarParserTest;
import network.Messages.LatestMessages;
import shared.dataInput.*;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.model.Bearing;
import shared.model.Constants;
import static org.junit.Assert.*;
public class MockRaceTest {
//TODO
/**
* Creates a MockRace for use in testing.
* Has a constant wind generator.
* @return MockRace for testing.
* @throws InvalidBoatDataException If the BoatDataSource cannot be created.
* @throws InvalidRaceDataException If the RaceDataSource cannot be created.
* @throws InvalidRegattaDataException If the RegattaDataSource cannot be created.
*/
public static MockRace createMockRace() throws InvalidBoatDataException, InvalidRaceDataException, InvalidRegattaDataException {
BoatDataSource boatDataSource = BoatXMLReaderTest.createBoatDataSource();
RaceDataSource raceDataSource = RaceXMLReaderTest.createRaceDataSource();
RegattaDataSource regattaDataSource = RegattaXMLReaderTest.createRegattaDataSource();
Polars polars = PolarParserTest.createPolars();
WindGenerator windGenerator = new ConstantWindGenerator(Bearing.fromDegrees(230), 10);
MockRace mockRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, polars, Constants.RaceTimeScale, windGenerator);
return mockRace;
}
}
@@ -8,10 +8,10 @@ import shared.model.Wind;
import static org.junit.Assert.*;
public class WindGeneratorTest {
public class RandomWindGeneratorTest {
private WindGenerator windGenerator;
private RandomWindGenerator randomWindGenerator;
private Bearing windBaselineBearing;
private Bearing windBearingLowerBound;
@@ -36,7 +36,7 @@ public class WindGeneratorTest {
this.windSpeedLowerBound = 7;
this.windSpeedUpperBound = 20;
this.windGenerator = new WindGenerator(
this.randomWindGenerator = new RandomWindGenerator(
windBaselineBearing,
windBearingLowerBound,
windBearingUpperBound,
@@ -55,7 +55,7 @@ public class WindGeneratorTest {
@Test
public void generateBaselineWindTest() {
Wind wind = windGenerator.generateBaselineWind();
Wind wind = randomWindGenerator.generateBaselineWind();
assertEquals(windBaselineSpeed, wind.getWindSpeed(), speedKnotsEpsilon);
assertEquals(windBaselineBearing.degrees(), wind.getWindDirection().degrees(), bearingDegreeEpsilon);
@@ -72,7 +72,7 @@ public class WindGeneratorTest {
for (int i = 0; i < randomWindCount; i++) {
Wind wind = windGenerator.generateRandomWind();
Wind wind = randomWindGenerator.generateRandomWind();
assertTrue(wind.getWindSpeed() >= windSpeedLowerBound);
assertTrue(wind.getWindSpeed() <= windSpeedUpperBound);
@@ -91,13 +91,13 @@ public class WindGeneratorTest {
@Test
public void generateNextWindTest() {
Wind wind = windGenerator.generateBaselineWind();
Wind wind = randomWindGenerator.generateBaselineWind();
int randomWindCount = 1000;
for (int i = 0; i < randomWindCount; i++) {
wind = windGenerator.generateNextWind(wind);
wind = randomWindGenerator.generateNextWind(wind);
assertTrue(wind.getWindSpeed() >= windSpeedLowerBound);
assertTrue(wind.getWindSpeed() <= windSpeedUpperBound);
@@ -0,0 +1,126 @@
package mock.model;
import mock.exceptions.SourceIDAllocationException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/**
* Tests if allocating source IDs works.
*/
public class SourceIdAllocatorTest {
/**
* This is the list of source IDs that we start with.
*/
private List<Integer> originalSourceIDs;
/**
* Used to allocate source IDs.
*/
private SourceIdAllocator sourceIdAllocator;
@Before
public void setUp() throws Exception {
originalSourceIDs = new ArrayList<>();
originalSourceIDs.add(120);
originalSourceIDs.add(121);
originalSourceIDs.add(122);
originalSourceIDs.add(123);
originalSourceIDs.add(124);
originalSourceIDs.add(125);
sourceIdAllocator = new SourceIdAllocator(originalSourceIDs);
}
/**
* Tests that allocation fails when we don't have any source IDs to allocate.
*/
@Test
public void emptyAllocationTest() {
SourceIdAllocator allocator = new SourceIdAllocator(new ArrayList<>());
try {
int sourceID = allocator.allocateSourceID();
fail("Exception should have been thrown, but wasn't.");
} catch (SourceIDAllocationException e) {
//We expect this exception to be thrown - success.
}
}
/**
* Tests that we can allocate a source ID.
* @throws Exception Thrown in case of error.
*/
@Test
public void allocationTest() throws Exception {
int sourceID = sourceIdAllocator.allocateSourceID();
}
/**
* Tests that we can allocate source IDs, but it will eventually be unable to allocate source IDs.
*/
@Test
public void allocationEventuallyFailsTest() {
while (true) {
try {
int sourceID = sourceIdAllocator.allocateSourceID();
} catch (SourceIDAllocationException e) {
//We expect to encounter this exception after enough allocations - success.
break;
}
}
}
/**
* Tests if we can allocate a source ID, return it, and reallocate it.
* @throws Exception Thrown in case of error.
*/
@Test
public void reallocationTest() throws Exception {
List<Integer> sourceIDList = new ArrayList<>();
sourceIDList.add(123);
SourceIdAllocator sourceIdAllocator = new SourceIdAllocator(sourceIDList);
//Allocate.
int sourceID = sourceIdAllocator.allocateSourceID();
//Return.
sourceIdAllocator.returnSourceID(sourceID);
//Reallocate.
int sourceID2 = sourceIdAllocator.allocateSourceID();
}
}
@@ -1,17 +1,18 @@
package mock.model.commandFactory;
import mock.exceptions.CommandConstructionException;
import mock.model.MockBoat;
import mock.model.MockRace;
import mock.model.MockRaceTest;
import network.Messages.BoatAction;
import network.Messages.Enums.BoatActionEnum;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.model.Bearing;
import shared.model.Boat;
import shared.model.Race;
import visualiser.model.VisualiserRace;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;
import static org.mockito.Mockito.mock;
@@ -28,15 +29,23 @@ public class WindCommandTest {
private double offset = 3.0;
@Before
public void setUp() {
race = mock(MockRace.class);
boat = new MockBoat(0, "Bob", "NZ", null);
public void setUp() throws CommandConstructionException, InvalidBoatDataException, InvalidRegattaDataException, InvalidRaceDataException {
race = MockRaceTest.createMockRace();
when(race.getWindDirection()).thenReturn(Bearing.fromDegrees(0.0));
boat = race.getBoats().get(0);
//when(race.getWindDirection()).thenReturn(Bearing.fromDegrees(0.0));
boat.setBearing(Bearing.fromDegrees(45.0));
upwind = CommandFactory.createCommand(race, boat, BoatActionEnum.UPWIND);
downwind = CommandFactory.createCommand(race, boat, BoatActionEnum.DOWNWIND);
BoatAction upwindAction = new BoatAction(BoatActionEnum.UPWIND);
upwindAction.setSourceID(boat.getSourceID());
BoatAction downwindAction = new BoatAction(BoatActionEnum.DOWNWIND);
downwindAction.setSourceID(boat.getSourceID());
upwind = CommandFactory.createCommand(race, upwindAction);
downwind = CommandFactory.createCommand(race, downwindAction);
initial = boat.getBearing().degrees();
}
@@ -55,4 +64,4 @@ public class WindCommandTest {
downwind.execute();
assertEquals(initial - boat.getBearing().degrees(), offset, 1e-5);
}
}
}
@@ -91,7 +91,7 @@ public class BinaryMessageDecoderTest {
XMLMessageDecoderTest.compareXMLMessages(xmlMessage, xmlMessageDecoded);
} catch (XMLReaderException | TransformerException e){
} catch (XMLReaderException e){
fail("couldn't read file" + e.getMessage());
}
}
@@ -64,7 +64,7 @@ public class JoinAcceptanceDecoderTest {
*/
@Test
public void joinSuccessSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 12345);
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, 12345);
}
/**
@@ -73,26 +73,9 @@ public class JoinAcceptanceDecoderTest {
*/
@Test
public void joinSuccessNoSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 0);
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, 0);
}
/**
* Tests if a participants full message can be encoded and decoded correctly.
* @throws Exception if test fails.
*/
@Test
public void participantFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.RACE_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a ghosts full message can be encoded and decoded correctly.
* @throws Exception if test fails.
*/
@Test
public void ghostFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.GHOST_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a server full message can be encoded and decoded correctly.
@@ -53,7 +53,7 @@ public class XMLMessageDecoderTest {
compareXMLMessages(message, decodedMessage);
} catch (XMLReaderException | TransformerException e){
} catch (XMLReaderException e){
fail("couldn't read file" + e.getMessage());
}
@@ -31,31 +31,40 @@ public class BoatXMLReaderTest {
private List<Mark> marks;
@Before
public void setUp() {
public static BoatDataSource createBoatDataSource() throws InvalidBoatDataException {
String boatXMLString = null;
try {
boatXMLString = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
fail("could not read boat xml file into a string");
} catch (XMLReaderException e) {
throw new InvalidBoatDataException("Could not read boat XML file into a string.", e);
}
try {
boatData = new BoatXMLReader(boatXMLString, XMLFileType.Contents);
BoatDataSource boatData = new BoatXMLReader(boatXMLString, XMLFileType.Contents);
return boatData;
boats = new ArrayList<>(boatData.getBoats().values());
marks = new ArrayList<>(boatData.getMarkerBoats().values());
} catch (XMLReaderException | InvalidBoatDataException e) {
e.printStackTrace();
assertTrue(false);
} catch (XMLReaderException e) {
throw new InvalidBoatDataException("Could not parse boat XML file.", e);
}
}
@Before
public void setUp() throws InvalidBoatDataException {
boatData = createBoatDataSource();
boats = new ArrayList<>(boatData.getBoats().values());
marks = new ArrayList<>(boatData.getMarkerBoats().values());
}
@Test
@@ -1,5 +1,39 @@
package shared.dataInput;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.XMLReaderException;
import javax.xml.transform.TransformerException;
import java.nio.charset.StandardCharsets;
public class RaceXMLReaderTest {
//TODO
public static RaceDataSource createRaceDataSource() throws InvalidRaceDataException {
String raceXMLString = null;
try {
raceXMLString = XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8);
} catch (XMLReaderException e) {
throw new InvalidRaceDataException("Could not read race XML file into a string.", e);
}
try {
RaceXMLReader raceData = new RaceXMLReader(raceXMLString, XMLFileType.Contents);
return raceData;
} catch (XMLReaderException e) {
throw new InvalidRaceDataException("Could not parse race XML file.", e);
}
}
}
@@ -3,6 +3,11 @@ package shared.dataInput;
import org.junit.Before;
import org.junit.Test;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import javax.xml.transform.TransformerException;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -14,6 +19,34 @@ import static org.junit.Assert.fail;
public class RegattaXMLReaderTest {
private RegattaXMLReader regatta;
public static RegattaDataSource createRegattaDataSource() throws InvalidRegattaDataException {
String regattaXMLString = null;
try {
regattaXMLString = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
} catch (XMLReaderException e) {
throw new InvalidRegattaDataException("Could not read regatta XML file into a string.", e);
}
try {
RegattaDataSource raceData = new RegattaXMLReader(regattaXMLString, XMLFileType.Contents);
return raceData;
} catch (XMLReaderException e) {
throw new InvalidRegattaDataException("Could not parse regatta XML file.", e);
}
}
@Before
public void findFile() {
try {
@@ -0,0 +1,157 @@
package visualiser.network;
import mock.model.commandFactory.Command;
import network.Messages.AC35Data;
import network.Messages.Enums.JoinAcceptanceEnum;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.JoinAcceptance;
import org.junit.Before;
import org.junit.Test;
import visualiser.Commands.ConnectionToServerCommands.JoinSuccessParticipantCommand;
import visualiser.Commands.ConnectionToServerCommands.JoinFailureCommand;
import visualiser.Commands.ConnectionToServerCommands.ServerFullCommand;
import visualiser.enums.ConnectionToServerState;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.Assert.*;
/**
* Tests the {@link ConnectionToServer} class with a Participant request, and how it reacts to various commands.
*/
public class ConnectionToServerParticipantTest {
private ConnectionToServer connectionToServer;
private Thread connectionToServerThread;
private BlockingQueue<AC35Data> outgoingMessages;
private BlockingQueue<Command> incomingCommands;
@Before
public void setUp() throws Exception {
incomingCommands = new LinkedBlockingQueue<>();
outgoingMessages = new LinkedBlockingQueue<>();
connectionToServer = new ConnectionToServer(ConnectionToServerState.UNKNOWN, RequestToJoinEnum.PARTICIPANT, incomingCommands, outgoingMessages);
connectionToServerThread = new Thread(connectionToServer);
connectionToServerThread.start();
}
/**
* When a connection to server is created, is it expected that it will have sent a request and be in the Request_sent state.
* @throws Exception On error.
*/
@Test
public void expectRequestSent() throws Exception {
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.REQUEST_SENT, connectionToServer.getConnectionState());
}
/**
* When the connection to server thread is interrupted, it is expected the connection state will become TimedOut.
* @throws Exception On error.
*/
@Test
public void interruptTimedOut() throws Exception {
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
//Disable logging as we know this will log but we don't care.
Logger.getGlobal().setLevel(Level.OFF);
connectionToServerThread.interrupt();
Logger.getGlobal().setLevel(null);
connectionToServerThread.join();
assertEquals(ConnectionToServerState.TIMED_OUT, connectionToServer.getConnectionState());
}
/**
* Sends a join successful command. Expects that the connection becomes Connected.
* @throws Exception On error.
*/
@Test
public void sendJoinSuccessCommand() throws Exception {
int sourceID = 123;
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, sourceID);
Command command = new JoinSuccessParticipantCommand(joinAcceptance, connectionToServer);
incomingCommands.put(command);
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.CONNECTED, connectionToServer.getConnectionState());
assertTrue(connectionToServer.getJoinAcceptance() != null);
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
assertNotEquals(0, connectionToServer.getJoinAcceptance().getSourceID());
assertEquals(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, connectionToServer.getJoinAcceptance().getAcceptanceType());
}
/**
* Sends a participants full command. Expects that the connection becomes Declined.
* @throws Exception On error.
*/
@Test
public void sendJoinFailureCommand() throws Exception {
int sourceID = 0;
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.JOIN_FAILURE, sourceID);
Command command = new JoinFailureCommand(joinAcceptance, connectionToServer);
incomingCommands.put(command);
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.DECLINED, connectionToServer.getConnectionState());
assertTrue(connectionToServer.getJoinAcceptance() != null);
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
assertEquals(JoinAcceptanceEnum.JOIN_FAILURE, connectionToServer.getJoinAcceptance().getAcceptanceType());
}
/**
* Sends a server full command. Expects that the connection becomes Declined.
* @throws Exception On error.
*/
@Test
public void sendServerFullCommand() throws Exception {
int sourceID = 0;
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.SERVER_FULL, sourceID);
Command command = new ServerFullCommand(joinAcceptance, connectionToServer);
incomingCommands.put(command);
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.DECLINED, connectionToServer.getConnectionState());
assertTrue(connectionToServer.getJoinAcceptance() != null);
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
assertEquals(JoinAcceptanceEnum.SERVER_FULL, connectionToServer.getJoinAcceptance().getAcceptanceType());
}
}
@@ -0,0 +1,108 @@
package visualiser.network;
import mock.model.commandFactory.Command;
import network.Messages.AC35Data;
import network.Messages.Enums.JoinAcceptanceEnum;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.JoinAcceptance;
import org.junit.Before;
import org.junit.Test;
import visualiser.Commands.ConnectionToServerCommands.JoinSuccessParticipantCommand;
import visualiser.Commands.ConnectionToServerCommands.ServerFullCommand;
import visualiser.enums.ConnectionToServerState;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.junit.Assert.*;
/**
* Tests the {@link ConnectionToServer} class with a Spectator request, and how it reacts to various commands.
*/
public class ConnectionToServerSpectatorTest {
private ConnectionToServer connectionToServer;
private Thread connectionToServerThread;
private BlockingQueue<AC35Data> outgoingMessages;
private BlockingQueue<Command> incomingCommands;
@Before
public void setUp() throws Exception {
incomingCommands = new LinkedBlockingQueue<>();
outgoingMessages = new LinkedBlockingQueue<>();
connectionToServer = new ConnectionToServer(ConnectionToServerState.UNKNOWN, RequestToJoinEnum.SPECTATOR, incomingCommands, outgoingMessages);
connectionToServerThread = new Thread(connectionToServer);
connectionToServerThread.start();
}
/**
* When a connection to server is created, is it expected that it will have sent a request and be in the Request_sent state.
* @throws Exception On error.
*/
@Test
public void expectRequestSent() throws Exception {
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.REQUEST_SENT, connectionToServer.getConnectionState());
}
/**
* Sends a join successful command. Expects that the connection becomes Connected.
* @throws Exception On error.
*/
@Test
public void sendJoinSuccessCommand() throws Exception {
int sourceID = 0;
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, sourceID);
Command command = new JoinSuccessParticipantCommand(joinAcceptance, connectionToServer);
incomingCommands.put(command);
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.CONNECTED, connectionToServer.getConnectionState());
assertTrue(connectionToServer.getJoinAcceptance() != null);
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
assertEquals(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, connectionToServer.getJoinAcceptance().getAcceptanceType());
}
/**
* Sends a server full command. Expects that the connection becomes Declined.
* @throws Exception On error.
*/
@Test
public void sendServerFullCommand() throws Exception {
int sourceID = 0;
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.SERVER_FULL, sourceID);
Command command = new ServerFullCommand(joinAcceptance, connectionToServer);
incomingCommands.put(command);
//Need to wait for connection thread to execute commands.
Thread.sleep(250);
assertEquals(ConnectionToServerState.DECLINED, connectionToServer.getConnectionState());
assertTrue(connectionToServer.getJoinAcceptance() != null);
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
assertEquals(JoinAcceptanceEnum.SERVER_FULL, connectionToServer.getJoinAcceptance().getAcceptanceType());
}
}