Merge remote-tracking branch 'remotes/origin/master' into story66-collision
# Conflicts: # racevisionGame/src/main/java/mock/model/MockBoat.java # racevisionGame/src/main/java/mock/model/RaceLogic.java # racevisionGame/src/main/java/shared/model/CompoundMark.java # racevisionGame/src/main/java/shared/model/Race.java # racevisionGame/src/test/java/mock/model/MockBoatTest.java
This commit is contained in:
@@ -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,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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package mock.model;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import shared.model.Bearing;
|
||||
import shared.model.Wind;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class RandomWindGeneratorTest {
|
||||
|
||||
|
||||
private RandomWindGenerator randomWindGenerator;
|
||||
|
||||
private Bearing windBaselineBearing;
|
||||
private Bearing windBearingLowerBound;
|
||||
private Bearing windBearingUpperBound;
|
||||
private double windBaselineSpeed;
|
||||
private double windSpeedLowerBound;
|
||||
private double windSpeedUpperBound;
|
||||
|
||||
private double speedKnotsEpsilon;
|
||||
private double bearingDegreeEpsilon;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
|
||||
//Bounds.
|
||||
this.windBaselineBearing = Bearing.fromDegrees(88.3);
|
||||
this.windBearingLowerBound = Bearing.fromDegrees(66.5);
|
||||
this.windBearingUpperBound = Bearing.fromDegrees(248.6);
|
||||
this.windBaselineSpeed = 13;
|
||||
this.windSpeedLowerBound = 7;
|
||||
this.windSpeedUpperBound = 20;
|
||||
|
||||
this.randomWindGenerator = new RandomWindGenerator(
|
||||
windBaselineBearing,
|
||||
windBearingLowerBound,
|
||||
windBearingUpperBound,
|
||||
windBaselineSpeed,
|
||||
windSpeedLowerBound,
|
||||
windSpeedUpperBound );
|
||||
|
||||
this.bearingDegreeEpsilon = 0.001;
|
||||
this.speedKnotsEpsilon = 0.001;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if the baseline wind generated it accurate.
|
||||
*/
|
||||
@Test
|
||||
public void generateBaselineWindTest() {
|
||||
|
||||
Wind wind = randomWindGenerator.generateBaselineWind();
|
||||
|
||||
assertEquals(windBaselineSpeed, wind.getWindSpeed(), speedKnotsEpsilon);
|
||||
assertEquals(windBaselineBearing.degrees(), wind.getWindDirection().degrees(), bearingDegreeEpsilon);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the random wind generated is inside the bounds.
|
||||
*/
|
||||
@Test
|
||||
public void generateRandomWindTest() {
|
||||
|
||||
int randomWindCount = 1000;
|
||||
|
||||
for (int i = 0; i < randomWindCount; i++) {
|
||||
|
||||
Wind wind = randomWindGenerator.generateRandomWind();
|
||||
|
||||
assertTrue(wind.getWindSpeed() >= windSpeedLowerBound);
|
||||
assertTrue(wind.getWindSpeed() <= windSpeedUpperBound);
|
||||
|
||||
assertTrue(wind.getWindDirection().degrees() >= windBearingLowerBound.degrees());
|
||||
assertTrue(wind.getWindDirection().degrees() <= windBearingUpperBound.degrees());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if the next wind generated is inside the bounds.
|
||||
*/
|
||||
@Test
|
||||
public void generateNextWindTest() {
|
||||
|
||||
Wind wind = randomWindGenerator.generateBaselineWind();
|
||||
|
||||
int randomWindCount = 1000;
|
||||
|
||||
for (int i = 0; i < randomWindCount; i++) {
|
||||
|
||||
wind = randomWindGenerator.generateNextWind(wind);
|
||||
|
||||
assertTrue(wind.getWindSpeed() >= windSpeedLowerBound);
|
||||
assertTrue(wind.getWindSpeed() <= windSpeedUpperBound);
|
||||
|
||||
assertTrue(wind.getWindDirection().degrees() >= windBearingLowerBound.degrees());
|
||||
assertTrue(wind.getWindDirection().degrees() <= windBearingUpperBound.degrees());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package network;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageDecoders.XMLMessageDecoder;
|
||||
import network.MessageDecoders.XMLMessageDecoderTest;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.Enums.MessageType;
|
||||
@@ -21,29 +22,47 @@ import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
* Tests the binary message decoder and encoder for a variety of messages.
|
||||
*/
|
||||
public class BinaryMessageDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Tests if an XMLMessage can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void decodeTest(){
|
||||
try{
|
||||
String xmlString = XMLReader.readXMLFileToString("network/raceXML/Regatta.xml", StandardCharsets.UTF_8);
|
||||
public void xmlMessageTest() throws Exception {
|
||||
|
||||
try {
|
||||
|
||||
String filePath = "network/raceXML/Regatta.xml";
|
||||
XMLMessageType messageType = XMLMessageType.REGATTA;
|
||||
|
||||
String xmlString = XMLReader.readXMLFileToString(filePath, StandardCharsets.UTF_8);
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
XMLMessage xmlMessagePre = new XMLMessage(
|
||||
XMLMessage xmlMessage = new XMLMessage(
|
||||
(byte)1,
|
||||
1,
|
||||
time,
|
||||
XMLMessageType.REGATTA,
|
||||
messageType,
|
||||
(short)1,
|
||||
xmlString );
|
||||
xmlString );
|
||||
|
||||
byte[] encodedMessage = RaceVisionByteEncoder.xmlMessage(xmlMessagePre);
|
||||
byte[] encodedMessage = RaceVisionByteEncoder.encode(xmlMessage);
|
||||
|
||||
BinaryMessageEncoder testMessage = new BinaryMessageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
|
||||
|
||||
BinaryMessageDecoder decoder = new BinaryMessageDecoder(testMessage.getFullMessage());
|
||||
BinaryMessageEncoder encoder = new BinaryMessageEncoder(
|
||||
xmlMessage.getType(),
|
||||
time,
|
||||
1,
|
||||
(short)encodedMessage.length,
|
||||
encodedMessage );
|
||||
|
||||
BinaryMessageDecoder decoder = new BinaryMessageDecoder(encoder.getFullMessage());
|
||||
|
||||
|
||||
AC35Data message = null;
|
||||
try {
|
||||
@@ -56,7 +75,7 @@ public class BinaryMessageDecoderTest {
|
||||
if (!(message instanceof XMLMessage)){
|
||||
Assert.assertFalse(true);
|
||||
}
|
||||
XMLMessage xmlMessage = (XMLMessage) message;
|
||||
XMLMessage xmlMessageDecoded = (XMLMessage) message;
|
||||
|
||||
|
||||
//message length
|
||||
@@ -66,29 +85,17 @@ public class BinaryMessageDecoderTest {
|
||||
//source ID
|
||||
Assert.assertEquals((short) 1, decoder.getHeaderSourceID());
|
||||
//message type
|
||||
Assert.assertEquals(26, decoder.getHeaderMessageType());
|
||||
Assert.assertEquals(MessageType.XMLMESSAGE.getValue(), decoder.getHeaderMessageType());
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(decoder.getMessageBody());
|
||||
decoderXML.decode();
|
||||
|
||||
//tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals(XMLMessageType.REGATTA, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
XMLMessageDecoderTest.compareXMLMessages(xmlMessage, xmlMessageDecoded);
|
||||
|
||||
// Reader reader = decoderXML.getXmlMessageInputStream().getCharacterStream();
|
||||
// int c;
|
||||
// String contents = "";
|
||||
// while((c = reader.read()) != -1) {
|
||||
// contents += (char)c;
|
||||
// }
|
||||
// Assert.assertEquals(xmlString.toString(), contents);
|
||||
|
||||
} catch (XMLReaderException | TransformerException e){
|
||||
} catch (XMLReaderException e){
|
||||
fail("couldn't read file" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
//TODO add some tests for more messages types.
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.AverageWind;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Test for the AverageWind encoder and decoder
|
||||
*/
|
||||
public class AverageWindDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a AverageWind message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void averageWindEncodeDecodeTest() throws Exception {
|
||||
|
||||
AverageWind averageWind = new AverageWind(
|
||||
AverageWind.currentMessageVersionNumber,
|
||||
System.currentTimeMillis(),
|
||||
3000,
|
||||
12.5,
|
||||
4050,
|
||||
12.6,
|
||||
3055,
|
||||
12.7,
|
||||
6051,
|
||||
13.37
|
||||
);
|
||||
|
||||
byte[] encodedMessage = RaceVisionByteEncoder.encode(averageWind);
|
||||
|
||||
AverageWindDecoder averageWindDecoder = new AverageWindDecoder();
|
||||
averageWindDecoder.decode(encodedMessage);
|
||||
AverageWind averageWindDecoded = averageWindDecoder.getMessage();
|
||||
|
||||
compareAverageWindMessages(averageWind, averageWindDecoded);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two AverageWind messages to check that they are equal.
|
||||
* @param original The original AverageWind message.
|
||||
* @param decoded The decoded AverageWind message.
|
||||
*/
|
||||
public static void compareAverageWindMessages(AverageWind original, AverageWind decoded) {
|
||||
|
||||
|
||||
assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
assertEquals(original.getTime(), decoded.getTime());
|
||||
|
||||
assertEquals(original.getRawPeriod(), decoded.getRawPeriod(), 100);
|
||||
assertEquals(original.getRawSpeedKnots(), decoded.getRawSpeedKnots(), 0.01);
|
||||
|
||||
assertEquals(original.getSampleTwoPeriod(), decoded.getSampleTwoPeriod(), 100);
|
||||
assertEquals(original.getSampleTwoSpeedKnots(), decoded.getSampleTwoSpeedKnots(), 0.01);
|
||||
|
||||
assertEquals(original.getSampleThreePeriod(), decoded.getSampleThreePeriod(), 100);
|
||||
assertEquals(original.getSampleThreeSpeedKnots(), decoded.getSampleThreeSpeedKnots(), 0.01);
|
||||
|
||||
assertEquals(original.getSampleFourPeriod(), decoded.getSampleFourPeriod(), 100);
|
||||
assertEquals(original.getSampleFourSpeedKnots(), decoded.getSampleFourSpeedKnots(), 0.01);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatAction;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import network.Messages.RequestToJoin;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
/**
|
||||
* Test for the BoatAction encoder and decoder
|
||||
*/
|
||||
public class BoatActionDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes and decodes a given message.
|
||||
* @param message Message to encode/decode.
|
||||
* @return The decoded message.
|
||||
* @throws InvalidMessageException If the message cannot be encoded.
|
||||
*/
|
||||
private BoatAction encodeDecodeMessage(BoatAction message) throws InvalidMessageException {
|
||||
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
BoatActionDecoder testDecoder = new BoatActionDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
|
||||
BoatAction decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a specific boat action type message can be encoded and decoded correctly.
|
||||
* @param type The type of boat action.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
private void boatActionTypeTest(BoatActionEnum type) throws Exception {
|
||||
|
||||
//Prepare message.
|
||||
BoatAction beforeMessage = new BoatAction(type);
|
||||
|
||||
|
||||
//Encode/decode it.
|
||||
BoatAction afterMessage = encodeDecodeMessage(beforeMessage);
|
||||
|
||||
|
||||
//Compare.
|
||||
assertEquals(beforeMessage.getBoatAction(), afterMessage.getBoatAction());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if an autopilot message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void autoPilotTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.AUTO_PILOT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a sails in message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void sailsInTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.SAILS_IN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a sails out message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void sailsOutTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.SAILS_OUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a tack/gybe message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void tackGybeTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.TACK_GYBE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if an upwind message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void upwindTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.UPWIND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a downwind message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void downwindTest() throws Exception {
|
||||
boatActionTypeTest(BoatActionEnum.DOWNWIND);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,48 +2,82 @@ package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatLocation;
|
||||
import network.Messages.Enums.BoatLocationDeviceEnum;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import shared.model.Azimuth;
|
||||
import shared.model.Bearing;
|
||||
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
* Test for the BoatLocation encoder and decoder
|
||||
*/
|
||||
public class BoatLocationDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a BoatLocation message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
public void boatLocationEncodeDecodeTest() throws Exception {
|
||||
|
||||
//Create message.
|
||||
long time = System.currentTimeMillis();
|
||||
BoatLocation testMessage = new BoatLocation((byte) 1, time, 2,
|
||||
3, (byte) 1, 180, -180, 4, 5,
|
||||
(short) 6, (short) 7, 8, 9, 10, 11,
|
||||
(short) 12, 13, 14 , (short) 15,
|
||||
16, 17, (short) 18);
|
||||
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.boatLocation(testMessage);
|
||||
BoatLocation testMessage = new BoatLocation(
|
||||
BoatLocation.currentMessageVersionNumber,
|
||||
time,
|
||||
2,
|
||||
3,
|
||||
BoatLocationDeviceEnum.RacingYacht,
|
||||
180,
|
||||
-180,
|
||||
4,
|
||||
Bearing.fromDegrees(45),
|
||||
(short) 6,
|
||||
(short) 7,
|
||||
8,
|
||||
Bearing.fromDegrees(40),
|
||||
10,
|
||||
11,
|
||||
Azimuth.fromDegrees(35),
|
||||
13,
|
||||
Bearing.fromDegrees(80),
|
||||
Azimuth.fromDegrees(80),
|
||||
16,
|
||||
Bearing.fromDegrees(80),
|
||||
Azimuth.fromDegrees(22) );
|
||||
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder(testEncodedMessage);
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(testMessage);
|
||||
|
||||
//Decode.
|
||||
BoatLocationDecoder testDecoder = new BoatLocationDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
BoatLocation decodedTest = testDecoder.getMessage();
|
||||
|
||||
//Check if valid.
|
||||
Assert.assertEquals(testMessage.getMessageVersionNumber(), decodedTest.getMessageVersionNumber());
|
||||
Assert.assertEquals(testMessage.getTime(), decodedTest.getTime());
|
||||
Assert.assertEquals(testMessage.getSequenceNumber(), decodedTest.getSequenceNumber());
|
||||
Assert.assertEquals(testMessage.getDeviceType(), decodedTest.getDeviceType());
|
||||
Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude());
|
||||
Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude());
|
||||
Assert.assertEquals(testMessage.getLatitude(), decodedTest.getLatitude(), 0.01);
|
||||
Assert.assertEquals(testMessage.getLongitude(), decodedTest.getLongitude(), 0.01);
|
||||
Assert.assertEquals(testMessage.getAltitude(), decodedTest.getAltitude());
|
||||
Assert.assertEquals(testMessage.getHeading(), decodedTest.getHeading());
|
||||
Assert.assertEquals(testMessage.getHeading().degrees(), decodedTest.getHeading().degrees(), 0.01);
|
||||
Assert.assertEquals(testMessage.getPitch(), decodedTest.getPitch());
|
||||
Assert.assertEquals(testMessage.getRoll(), decodedTest.getRoll());
|
||||
Assert.assertEquals(testMessage.getBoatSpeed(), decodedTest.getBoatSpeed());
|
||||
Assert.assertEquals(testMessage.getBoatSpeedKnots(), decodedTest.getBoatSpeedKnots(), 0.01);
|
||||
|
||||
Assert.assertEquals(testMessage.getBoatCOG(), decodedTest.getBoatCOG());
|
||||
Assert.assertEquals(testMessage.getBoatSOG(), decodedTest.getBoatSOG());
|
||||
Assert.assertEquals(testMessage.getApparentWindSpeed(), decodedTest.getApparentWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindSpeed(), decodedTest.getTrueWindSpeed());
|
||||
Assert.assertEquals(testMessage.getTrueWindDirection(), decodedTest.getTrueWindDirection());
|
||||
Assert.assertEquals(testMessage.getTrueWindAngle(), decodedTest.getTrueWindAngle());
|
||||
Assert.assertEquals(testMessage.getCurrentDrift(), decodedTest.getCurrentDrift());
|
||||
Assert.assertEquals(testMessage.getCurrentSet(), decodedTest.getCurrentSet());
|
||||
Assert.assertEquals(testMessage.getRudderAngle(), decodedTest.getRudderAngle());
|
||||
Assert.assertEquals(testMessage.getBoatCOG().degrees(), decodedTest.getBoatCOG().degrees(), 0.01);
|
||||
Assert.assertEquals(testMessage.getBoatSOGKnots(), decodedTest.getBoatSOGKnots(), 0.01);
|
||||
Assert.assertEquals(testMessage.getApparentWindSpeedKnots(), decodedTest.getApparentWindSpeedKnots(), 0.01);
|
||||
Assert.assertEquals(testMessage.getTrueWindSpeedKnots(), decodedTest.getTrueWindSpeedKnots(), 0.01);
|
||||
Assert.assertEquals(testMessage.getTrueWindDirection().degrees(), decodedTest.getTrueWindDirection().degrees(), 0.01);
|
||||
Assert.assertEquals(testMessage.getTrueWindAngle().degrees(), decodedTest.getTrueWindAngle().degrees(), 0.01);
|
||||
Assert.assertEquals(testMessage.getCurrentDriftKnots(), decodedTest.getCurrentDriftKnots(), 0.01);
|
||||
Assert.assertEquals(testMessage.getCurrentSet().degrees(), decodedTest.getCurrentSet().degrees(), 0.01);
|
||||
Assert.assertEquals(testMessage.getRudderAngle().degrees(), decodedTest.getRudderAngle().degrees(), 0.01);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.BoatStatusEncoder;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Test for the BoatStatus encoder and decoder
|
||||
*/
|
||||
public class BoatStatusDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a BoatStatus message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void boatStatusEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
//Create data to serialize.
|
||||
int boatSourceID = 5;
|
||||
BoatStatusEnum boatStatusEnum = BoatStatusEnum.RACING;
|
||||
byte boatLegNumber = 5;
|
||||
byte boatPenaltiesAwarded = 4;
|
||||
byte boatPenaltiesServed = 2;
|
||||
long boatTimeAtNextMark = time + (1000 * 3);
|
||||
long boatTimeAtFinish = boatTimeAtNextMark + (1000 * 15);
|
||||
|
||||
BoatStatus boatStatus = new BoatStatus(
|
||||
boatSourceID,
|
||||
boatStatusEnum,
|
||||
boatLegNumber,
|
||||
boatPenaltiesAwarded,
|
||||
boatPenaltiesServed,
|
||||
boatTimeAtNextMark,
|
||||
boatTimeAtFinish );
|
||||
|
||||
|
||||
BoatStatus boatStatusDecoded = encodeDecodeBoatStatus(boatStatus);
|
||||
|
||||
compareBoatStatusMessages(boatStatus, boatStatusDecoded);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes and decodes a BoatStatus, and returns it.
|
||||
* @param boatStatus The BoatStatus to encode and decode.
|
||||
* @return The decoded BoatStatus.
|
||||
* @throws InvalidMessageException Thrown if message cannot be encoded or decoded.
|
||||
*/
|
||||
private static BoatStatus encodeDecodeBoatStatus(BoatStatus boatStatus) throws InvalidMessageException {
|
||||
|
||||
BoatStatusEncoder boatStatusEncoder = new BoatStatusEncoder();
|
||||
byte[] boatStatusEncoded = boatStatusEncoder.encode(boatStatus);
|
||||
|
||||
BoatStatusDecoder boatStatusDecoder = new BoatStatusDecoder();
|
||||
BoatStatus boatStatusDecoded = boatStatusDecoder.decode(boatStatusEncoded);
|
||||
|
||||
return boatStatusDecoded;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two BoatStatus messages to check that they are equal.
|
||||
* @param original The original BoatStatus message.
|
||||
* @param decoded The decoded BoatStatus message.
|
||||
*/
|
||||
public static void compareBoatStatusMessages(BoatStatus original, BoatStatus decoded) {
|
||||
|
||||
Assert.assertEquals(original.getSourceID(), decoded.getSourceID());
|
||||
Assert.assertEquals(original.getBoatStatus(), decoded.getBoatStatus());
|
||||
Assert.assertEquals(original.getLegNumber(), decoded.getLegNumber());
|
||||
Assert.assertEquals(original.getNumPenaltiesAwarded(), decoded.getNumPenaltiesAwarded());
|
||||
Assert.assertEquals(original.getNumPenaltiesServed(), decoded.getNumPenaltiesServed());
|
||||
Assert.assertEquals(original.getEstTimeAtNextMark(), decoded.getEstTimeAtNextMark());
|
||||
Assert.assertEquals(original.getEstTimeAtFinish(), decoded.getEstTimeAtFinish());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +1,79 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.CourseWindEncoder;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.CourseWind;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import shared.model.Bearing;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
* Test for the CourseWind encoder and decoder
|
||||
*/
|
||||
public class CourseWindDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a CourseWind message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
public void courseWindEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
CourseWind testCourseWind1 = new CourseWind(1, time, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind2 = new CourseWind(2, time2, 2,
|
||||
3, 4, 5,
|
||||
7, 6);
|
||||
|
||||
ArrayList<CourseWind> testCourseWinds = new ArrayList<CourseWind>();
|
||||
testCourseWinds.add(testCourseWind1);
|
||||
testCourseWinds.add(testCourseWind2);
|
||||
CourseWind courseWind = new CourseWind(
|
||||
1,
|
||||
time,
|
||||
2,
|
||||
Bearing.fromDegrees(45),
|
||||
4,
|
||||
Bearing.fromDegrees(70),
|
||||
Bearing.fromDegrees(160),
|
||||
(byte) 0x13 );
|
||||
|
||||
|
||||
byte[] testEncodedCourseWind = RaceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
|
||||
CourseWind courseWindDecoded = encodeDecodeCourseWind(courseWind);
|
||||
|
||||
CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind);
|
||||
|
||||
ArrayList<CourseWind> testDecodedCourseWinds = testDecoder.getLoopMessages();
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(0).getID(), testDecodedCourseWinds.get(0).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getTime(), testDecodedCourseWinds.get(0).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getRaceID(), testDecodedCourseWinds.get(0).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindDirection(), testDecodedCourseWinds.get(0).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getWindSpeed(), testDecodedCourseWinds.get(0).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestUpwindAngle(), testDecodedCourseWinds.get(0).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getBestDownwindAngle(), testDecodedCourseWinds.get(0).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(0).getFlags(), testDecodedCourseWinds.get(0).getFlags());
|
||||
|
||||
Assert.assertEquals(testCourseWinds.get(1).getID(), testDecodedCourseWinds.get(1).getID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getTime(), testDecodedCourseWinds.get(1).getTime());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getRaceID(), testDecodedCourseWinds.get(1).getRaceID());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindDirection(), testDecodedCourseWinds.get(1).getWindDirection());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getWindSpeed(), testDecodedCourseWinds.get(1).getWindSpeed());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestUpwindAngle(), testDecodedCourseWinds.get(1).getBestUpwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getBestDownwindAngle(), testDecodedCourseWinds.get(1).getBestDownwindAngle());
|
||||
Assert.assertEquals(testCourseWinds.get(1).getFlags(), testDecodedCourseWinds.get(1).getFlags());
|
||||
compareCourseWindMessages(courseWind, courseWindDecoded);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes and decodes a CourseWind, and returns it.
|
||||
* @param courseWind The CourseWind to encode and decode.
|
||||
* @return The decoded CourseWind.
|
||||
* @throws InvalidMessageException Thrown if message cannot be encoded or decoded.
|
||||
*/
|
||||
private static CourseWind encodeDecodeCourseWind(CourseWind courseWind) throws InvalidMessageException {
|
||||
|
||||
CourseWindEncoder courseWindEncoder = new CourseWindEncoder();
|
||||
byte[] courseWindEncoded = courseWindEncoder.encode(courseWind);
|
||||
|
||||
CourseWindDecoder courseWindDecoder = new CourseWindDecoder();
|
||||
CourseWind courseWindDecoded = courseWindDecoder.decode(courseWindEncoded);
|
||||
|
||||
return courseWindDecoded;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two CourseWind messages to check that they are equal.
|
||||
* @param original The original CourseWind message.
|
||||
* @param decoded The decoded CourseWind message.
|
||||
*/
|
||||
public static void compareCourseWindMessages(CourseWind original, CourseWind decoded) {
|
||||
|
||||
Assert.assertEquals(original.getID(), decoded.getID());
|
||||
Assert.assertEquals(original.getTime(), decoded.getTime());
|
||||
Assert.assertEquals(original.getRaceID(), decoded.getRaceID());
|
||||
Assert.assertEquals(original.getWindDirection().degrees(), decoded.getWindDirection().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getWindSpeedKnots(), decoded.getWindSpeedKnots(), 0.01);
|
||||
Assert.assertEquals(original.getBestUpwindAngle().degrees(), decoded.getBestUpwindAngle().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getBestDownwindAngle().degrees(), decoded.getBestDownwindAngle().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getFlags(), decoded.getFlags());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.CourseWind;
|
||||
import network.Messages.CourseWinds;
|
||||
import org.junit.Test;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for CourseWinds encoder and decoder.
|
||||
*/
|
||||
public class CourseWindsDecoderTest {
|
||||
|
||||
/**
|
||||
* Tests if a CourseWinds message can be encoded and decoded correctly.
|
||||
* @throws Exception Thrown if an error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void courseWindsEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind1 = new CourseWind(
|
||||
1,
|
||||
time1,
|
||||
2,
|
||||
Bearing.fromDegrees(45),
|
||||
4,
|
||||
Bearing.fromDegrees(70),
|
||||
Bearing.fromDegrees(160),
|
||||
(byte) 0xCE );
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
CourseWind testCourseWind2 = new CourseWind(
|
||||
2,
|
||||
time2,
|
||||
2,
|
||||
Bearing.fromDegrees(55),
|
||||
4,
|
||||
Bearing.fromDegrees(80),
|
||||
Bearing.fromDegrees(180),
|
||||
(byte) 0x0D );
|
||||
|
||||
List<CourseWind> testCourseWinds = new ArrayList<>();
|
||||
testCourseWinds.add(testCourseWind1);
|
||||
testCourseWinds.add(testCourseWind2);
|
||||
|
||||
CourseWinds courseWinds = new CourseWinds(CourseWinds.currentMessageVersionNumber, (byte) 2, testCourseWinds);
|
||||
|
||||
|
||||
byte[] testEncodedCourseWind = RaceVisionByteEncoder.encode(courseWinds);
|
||||
|
||||
CourseWindsDecoder courseWindsDecoder = new CourseWindsDecoder();
|
||||
courseWindsDecoder.decode(testEncodedCourseWind);
|
||||
CourseWinds courseWindsDecoded = courseWindsDecoder.getMessage();
|
||||
|
||||
compareCourseWindsMessages(courseWinds, courseWindsDecoded);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two course winds messages to ensure they are the same.
|
||||
* @param original The original message.
|
||||
* @param decoded The decoded message.
|
||||
*/
|
||||
public static void compareCourseWindsMessages(CourseWinds original, CourseWinds decoded) {
|
||||
|
||||
//Compare header.
|
||||
assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
assertEquals(original.getSelectedWindID(), decoded.getSelectedWindID());
|
||||
assertEquals(original.getCourseWinds().size(), decoded.getCourseWinds().size());
|
||||
|
||||
//Compare each CourseWind.
|
||||
List<CourseWind> originalWinds = original.getCourseWinds();
|
||||
List<CourseWind> decodedWinds = decoded.getCourseWinds();
|
||||
|
||||
Iterator<CourseWind> originalIterator = originalWinds.iterator();
|
||||
Iterator<CourseWind> decodedIterator = decodedWinds.iterator();
|
||||
|
||||
while (originalIterator.hasNext() && decodedIterator.hasNext()) {
|
||||
|
||||
CourseWindDecoderTest.compareCourseWindMessages(originalIterator.next(), decodedIterator.next());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatAction;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.HeartBeat;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
/**
|
||||
* Test for the HeartBeat encoder and decoder
|
||||
*/
|
||||
public class HeartBeatDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes and decodes a given message.
|
||||
* @param message Message to encode/decode.
|
||||
* @return The decoded message.
|
||||
* @throws InvalidMessageException if the message cannot be encoded.
|
||||
*/
|
||||
private HeartBeat encodeDecodeMessage(HeartBeat message) throws InvalidMessageException {
|
||||
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
HeartBeatDecoder testDecoder = new HeartBeatDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
HeartBeat decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a given sequence number can be encoded and decoded correctly.
|
||||
* @param sequenceNumber The sequenceNumber to use.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
private void heartBeatTest(long sequenceNumber) throws Exception {
|
||||
|
||||
//Prepare message.
|
||||
HeartBeat beforeMessage = new HeartBeat(sequenceNumber);
|
||||
|
||||
|
||||
//Encode/decode it.
|
||||
HeartBeat afterMessage = encodeDecodeMessage(beforeMessage);
|
||||
|
||||
|
||||
//Compare.
|
||||
assertEquals(beforeMessage.getSequenceNumber(), afterMessage.getSequenceNumber());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a sequence number of zero can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void heartBeatZeroTest() throws Exception {
|
||||
heartBeatTest(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a heartbeat message with a sequence number of 1234512 can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void heartBeatNonZeroTest() throws Exception {
|
||||
heartBeatTest(1234512);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.JoinAcceptanceEnum;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test for the {@link network.Messages.JoinAcceptance} encoder and decoder
|
||||
*/
|
||||
public class JoinAcceptanceDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes and decodes a given message.
|
||||
* @param message Message to encode/decode.
|
||||
* @return The decoded message.
|
||||
* @throws InvalidMessageException If the message cannot be encoded.
|
||||
*/
|
||||
private JoinAcceptance encodeDecodeMessage(JoinAcceptance message) throws InvalidMessageException {
|
||||
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
JoinAcceptanceDecoder testDecoder = new JoinAcceptanceDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
JoinAcceptance decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a specific acceptance type message can be encoded and decoded correctly.
|
||||
* @param type The type of acceptance response.
|
||||
* @param sourceID The source ID to assign.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
private void responseTypeTest(JoinAcceptanceEnum type, int sourceID) throws Exception {
|
||||
|
||||
//Prepare message.
|
||||
JoinAcceptance beforeMessage = new JoinAcceptance(type, sourceID);
|
||||
|
||||
|
||||
//Encode/decode it.
|
||||
JoinAcceptance afterMessage = encodeDecodeMessage(beforeMessage);
|
||||
|
||||
|
||||
//Compare.
|
||||
assertEquals(beforeMessage.getAcceptanceType().getValue(), afterMessage.getAcceptanceType().getValue());
|
||||
assertEquals(beforeMessage.getSourceID(), afterMessage.getSourceID());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a join success message, with a source ID, can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void joinSuccessSourceIDTest() throws Exception {
|
||||
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, 12345);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a join success message, with no source ID, can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void joinSuccessNoSourceIDTest() throws Exception {
|
||||
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a server full message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void serverFullTest() throws Exception {
|
||||
responseTypeTest(JoinAcceptanceEnum.SERVER_FULL, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.MarkRoundingBoatStatusEnum;
|
||||
import network.Messages.Enums.MarkRoundingSideEnum;
|
||||
import network.Messages.Enums.MarkRoundingTypeEnum;
|
||||
import network.Messages.MarkRounding;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for the MarkRounding encoder and decoder
|
||||
*/
|
||||
public class MarkRoundingDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a MarkRounding message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void markRoundingEncodeDecodeTest() throws Exception {
|
||||
|
||||
MarkRounding markRounding = new MarkRounding(
|
||||
MarkRounding.currentMessageVersionNumber,
|
||||
System.currentTimeMillis(),
|
||||
567,
|
||||
42,
|
||||
125,
|
||||
MarkRoundingBoatStatusEnum.RACING,
|
||||
MarkRoundingSideEnum.PORT,
|
||||
MarkRoundingTypeEnum.MARK,
|
||||
(byte)45
|
||||
);
|
||||
|
||||
byte[] encodedMessage = RaceVisionByteEncoder.encode(markRounding);
|
||||
|
||||
MarkRoundingDecoder markRoundingDecoder = new MarkRoundingDecoder();
|
||||
markRoundingDecoder.decode(encodedMessage);
|
||||
MarkRounding markRoundingDecoded = markRoundingDecoder.getMessage();
|
||||
|
||||
compareMarkRoundingMessages(markRounding, markRoundingDecoded);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two MarkRounding messages to check that they are equal.
|
||||
* @param original The original MarkRounding message.
|
||||
* @param decoded The decoded MarkRounding message.
|
||||
*/
|
||||
public static void compareMarkRoundingMessages(MarkRounding original, MarkRounding decoded) {
|
||||
|
||||
|
||||
assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
assertEquals(original.getTime(), decoded.getTime());
|
||||
assertEquals(original.getAckNum(), decoded.getAckNum());
|
||||
assertEquals(original.getRaceID(), decoded.getRaceID());
|
||||
assertEquals(original.getSourceID(), decoded.getSourceID());
|
||||
assertEquals(original.getBoatStatus(), decoded.getBoatStatus());
|
||||
assertEquals(original.getRoundingSide(), decoded.getRoundingSide());
|
||||
assertEquals(original.getMarkType(), decoded.getMarkType());
|
||||
assertEquals(original.getMarkID(), decoded.getMarkID());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+46
-13
@@ -1,29 +1,62 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.RaceStartTypeEnum;
|
||||
import network.Messages.RaceStartStatus;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
* Tests for the RaceStartStatus encoder and decoder.
|
||||
*/
|
||||
public class RaceStartStatusDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a RaceStartStatus message can be encoded and decoded correctly.
|
||||
* @throws Exception Thrown when an error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
long time = System.currentTimeMillis();
|
||||
public void raceStartStatusEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
byte[] encodedRaceStartStatus = RaceVisionByteEncoder.raceStartStatus(time, (short)1,
|
||||
time2, 2, (char)3);
|
||||
long timestamp = System.currentTimeMillis();
|
||||
|
||||
RaceStartStatusDecoder testDecoder = new RaceStartStatusDecoder(encodedRaceStartStatus);
|
||||
long startTime = System.currentTimeMillis() + 10 * 1000;
|
||||
|
||||
Assert.assertEquals(0b1, testDecoder.getMessageVersion());
|
||||
Assert.assertEquals(time, testDecoder.getTime());
|
||||
Assert.assertEquals(1, testDecoder.getAck());
|
||||
Assert.assertEquals(time2, testDecoder.getStartTime());
|
||||
Assert.assertEquals(2, testDecoder.getRaceID());
|
||||
Assert.assertEquals((char)3, testDecoder.getNotification());
|
||||
RaceStartStatus raceStartStatus = new RaceStartStatus(
|
||||
RaceStartStatus.currentMessageVersionNumber,
|
||||
timestamp,
|
||||
55,
|
||||
startTime,
|
||||
35,
|
||||
RaceStartTypeEnum.SET_RACE_START
|
||||
);
|
||||
|
||||
byte[] encodedRaceStartStatus = RaceVisionByteEncoder.encode(raceStartStatus);
|
||||
|
||||
RaceStartStatusDecoder testDecoder = new RaceStartStatusDecoder();
|
||||
testDecoder.decode(encodedRaceStartStatus);
|
||||
RaceStartStatus raceStartStatusDecoded = testDecoder.getMessage();
|
||||
|
||||
compareRaceStartStatusMessages(raceStartStatus, raceStartStatusDecoded);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two RaceStartStatus messages to check that they are the same.
|
||||
* @param original The original message.
|
||||
* @param decoded The decoded message.
|
||||
*/
|
||||
public static void compareRaceStartStatusMessages(RaceStartStatus original, RaceStartStatus decoded) {
|
||||
|
||||
Assert.assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
Assert.assertEquals(original.getTimestamp(), decoded.getTimestamp());
|
||||
Assert.assertEquals(original.getAckNum(), decoded.getAckNum());
|
||||
Assert.assertEquals(original.getRaceStartTime(), decoded.getRaceStartTime());
|
||||
Assert.assertEquals(original.getRaceID(), decoded.getRaceID());
|
||||
Assert.assertEquals(original.getNotificationType(), decoded.getNotificationType());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,27 +1,40 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import network.Messages.RaceStatus;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
* Test for the RaceStatus encoder and decoder
|
||||
*/
|
||||
public class RaceStatusDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a RaceStatus message, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
public void raceStatusEncodeDecodeTest() throws Exception {
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
//Create data to serialize.
|
||||
int boat1SourceID = 5;
|
||||
int boat2SourceID = 8;
|
||||
byte boat1Status = 2;
|
||||
byte boat2Status = 2;
|
||||
BoatStatusEnum boat1Status = BoatStatusEnum.RACING;
|
||||
BoatStatusEnum boat2Status = BoatStatusEnum.RACING;
|
||||
byte boat1LegNumber = 5;
|
||||
byte boat2LegNumber = 3;
|
||||
byte boat1PenaltiesAwarded = 4;
|
||||
@@ -33,44 +46,86 @@ public class RaceStatusDecoderTest {
|
||||
long boat1TimeAtFinish = boat1TimeAtNextMark + (1000 * 15);
|
||||
long boat2TimeAtFinish = boat2TimeAtNextMark + (1000 * 7);
|
||||
|
||||
BoatStatus boatStatus1 = new BoatStatus(boat1SourceID, boat1Status, boat1LegNumber, boat1PenaltiesAwarded, boat1PenaltiesServed, boat1TimeAtNextMark, boat1TimeAtFinish);
|
||||
BoatStatus boatStatus2 = new BoatStatus(boat2SourceID, boat2Status, boat2LegNumber, boat2PenaltiesAwarded, boat2PenaltiesServed, boat2TimeAtNextMark, boat2TimeAtFinish);
|
||||
BoatStatus boatStatus1 = new BoatStatus(
|
||||
boat1SourceID,
|
||||
boat1Status,
|
||||
boat1LegNumber,
|
||||
boat1PenaltiesAwarded,
|
||||
boat1PenaltiesServed,
|
||||
boat1TimeAtNextMark,
|
||||
boat1TimeAtFinish );
|
||||
|
||||
BoatStatus boatStatus2 = new BoatStatus(
|
||||
boat2SourceID,
|
||||
boat2Status,
|
||||
boat2LegNumber,
|
||||
boat2PenaltiesAwarded,
|
||||
boat2PenaltiesServed,
|
||||
boat2TimeAtNextMark,
|
||||
boat2TimeAtFinish );
|
||||
|
||||
|
||||
int raceID = 585;
|
||||
byte raceStatus = 3;
|
||||
RaceStatusEnum raceStatus = RaceStatusEnum.STARTED;
|
||||
long raceStartTime = time - (1000 * 31);
|
||||
int windDirection = 2341;
|
||||
int windSpeed = 10201;
|
||||
int raceType = 1;
|
||||
Bearing windDirection = Bearing.fromDegrees(185.34);
|
||||
double windSpeedKnots = 14.52;
|
||||
RaceTypeEnum raceType = RaceTypeEnum.MATCH_RACE;
|
||||
List<BoatStatus> boatStatuses = new ArrayList<>(2);
|
||||
boatStatuses.add(boatStatus1);
|
||||
boatStatuses.add(boatStatus2);
|
||||
|
||||
RaceStatus raceStatusObject = new RaceStatus(time, raceID, raceStatus, raceStartTime, windDirection, windSpeed, raceType, boatStatuses);
|
||||
RaceStatus raceStatusOriginal = new RaceStatus(
|
||||
RaceStatus.currentMessageVersionNumber,
|
||||
time,
|
||||
raceID,
|
||||
raceStatus,
|
||||
raceStartTime,
|
||||
windDirection,
|
||||
windSpeedKnots,
|
||||
raceType,
|
||||
boatStatuses );
|
||||
|
||||
|
||||
byte[] encodedRaceStatus = RaceVisionByteEncoder.raceStatus(raceStatusObject);
|
||||
byte[] encodedRaceStatus = RaceVisionByteEncoder.encode(raceStatusOriginal);
|
||||
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);
|
||||
RaceStatusDecoder decoderTest = new RaceStatusDecoder();
|
||||
decoderTest.decode(encodedRaceStatus);
|
||||
|
||||
Assert.assertEquals(0b10, decoderTest.getVersionNum());
|
||||
Assert.assertEquals(time, decoderTest.getTime());
|
||||
Assert.assertEquals(raceID, decoderTest.getRace());
|
||||
Assert.assertEquals(raceStatus, decoderTest.getRaceState());
|
||||
Assert.assertEquals(raceStartTime, decoderTest.getStartTime());
|
||||
Assert.assertEquals(windDirection, decoderTest.getRaceWindDir());
|
||||
Assert.assertEquals(windSpeed, decoderTest.getRaceWindSpeed());
|
||||
RaceStatus decodedMessage = decoderTest.getMessage();
|
||||
|
||||
|
||||
BoatStatus boat1 = decoderTest.getBoats().get(0);
|
||||
|
||||
Assert.assertEquals(boat1SourceID, boat1.getSourceID());
|
||||
Assert.assertEquals(boat1Status, boat1.getBoatStatus());
|
||||
Assert.assertEquals(boat1LegNumber, boat1.getLegNumber());
|
||||
Assert.assertEquals(boat1PenaltiesAwarded, boat1.getNumPenaltiesAwarded());
|
||||
Assert.assertEquals(boat1PenaltiesServed, boat1.getNumPenaltiesServed());
|
||||
Assert.assertEquals(boat1TimeAtNextMark, boat1.getEstTimeAtNextMark());
|
||||
Assert.assertEquals(boat1TimeAtFinish, boat1.getEstTimeAtFinish());
|
||||
compareRaceStatusMessages(raceStatusOriginal, decodedMessage);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares two RaceStatus messages to check that they are equal.
|
||||
* @param original The original RaceStatus message.
|
||||
* @param decoded The decoded RaceStatus message.
|
||||
*/
|
||||
public static void compareRaceStatusMessages(RaceStatus original, RaceStatus decoded) {
|
||||
|
||||
//Compare RaceStatus body.
|
||||
Assert.assertEquals(original.getMessageVersionNumber(), decoded.getMessageVersionNumber());
|
||||
Assert.assertEquals(original.getCurrentTime(), decoded.getCurrentTime());
|
||||
Assert.assertEquals(original.getRaceID(), decoded.getRaceID());
|
||||
Assert.assertEquals(original.getRaceStatus(), decoded.getRaceStatus());
|
||||
Assert.assertEquals(original.getExpectedStartTime(), decoded.getExpectedStartTime());
|
||||
Assert.assertEquals(original.getWindDirection().degrees(), decoded.getWindDirection().degrees(), 0.01);
|
||||
Assert.assertEquals(original.getWindSpeed(), decoded.getWindSpeed(), 0.01);
|
||||
|
||||
//Compare all BoatStatuses
|
||||
Iterator<BoatStatus> originalIterator = original.getBoatStatuses().iterator();
|
||||
Iterator<BoatStatus> decodedIterator = decoded.getBoatStatuses().iterator();
|
||||
|
||||
while (originalIterator.hasNext() && decodedIterator.hasNext()) {
|
||||
|
||||
BoatStatusDecoderTest.compareBoatStatusMessages(originalIterator.next(), decodedIterator.next());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import network.Messages.RequestToJoin;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test for the RequestToJoin encoder and decoder
|
||||
*/
|
||||
public class RequestToJoinDecoderTest {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes and decodes a given message.
|
||||
* @param message Message to encode/decode.
|
||||
* @return The decoded message.
|
||||
* @throws InvalidMessageException If the message cannot be encoded.
|
||||
*/
|
||||
private RequestToJoin encodeDecodeMessage(RequestToJoin message) throws InvalidMessageException {
|
||||
|
||||
//Encode.
|
||||
byte [] testEncodedMessage = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
//Decode.
|
||||
RequestToJoinDecoder testDecoder = new RequestToJoinDecoder();
|
||||
testDecoder.decode(testEncodedMessage);
|
||||
RequestToJoin decodedMessage = testDecoder.getMessage();
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a specific request type message can be encoded and decoded correctly.
|
||||
* @param type The type of join request.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
private void requestTypeTest(RequestToJoinEnum type) throws Exception {
|
||||
|
||||
//Prepare message.
|
||||
RequestToJoin beforeMessage = new RequestToJoin(type);
|
||||
|
||||
|
||||
//Encode/decode it.
|
||||
RequestToJoin afterMessage = encodeDecodeMessage(beforeMessage);
|
||||
|
||||
|
||||
//Compare.
|
||||
assertEquals(beforeMessage.getRequestType().getValue(), afterMessage.getRequestType().getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a spectator request message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void spectatorTest() throws Exception {
|
||||
requestTypeTest(RequestToJoinEnum.SPECTATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a participant request message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void participantTest() throws Exception {
|
||||
requestTypeTest(RequestToJoinEnum.PARTICIPANT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a ghost request message can be encoded and decoded correctly.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void ghostTest() throws Exception {
|
||||
requestTypeTest(RequestToJoinEnum.GHOST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package network.MessageDecoders;
|
||||
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
import network.Messages.XMLMessage;
|
||||
@@ -14,13 +15,21 @@ import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
* Test for the XMLMessage encoder and decoder
|
||||
*/
|
||||
public class XMLMessageDecoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest(){
|
||||
try{
|
||||
String xmlString = XMLReader.readXMLFileToString("network/raceXML/Regatta.xml", StandardCharsets.UTF_8);
|
||||
|
||||
|
||||
/**
|
||||
* Creates an XML message of the given type, using the specified filePath, encodes it, decodes it, and checks that the result matches the starting message.
|
||||
* @param filePath The file path for xml file.
|
||||
* @param type The type of xml file.
|
||||
* @throws InvalidMessageException Thrown if message cannot be encoded.
|
||||
*/
|
||||
private void xmlMessageTest(String filePath, XMLMessageType type) throws InvalidMessageException {
|
||||
|
||||
try {
|
||||
String xmlString = XMLReader.readXMLFileToString(filePath, StandardCharsets.UTF_8);
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
@@ -28,27 +37,71 @@ public class XMLMessageDecoderTest {
|
||||
(byte)1,
|
||||
1,
|
||||
time,
|
||||
XMLMessageType.REGATTA,
|
||||
type,
|
||||
(short)1,
|
||||
xmlString );
|
||||
|
||||
byte[] encodedXML = RaceVisionByteEncoder.xmlMessage(message);
|
||||
byte[] encodedXML = RaceVisionByteEncoder.encode(message);
|
||||
|
||||
|
||||
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder(encodedXML);
|
||||
decoderXML.decode();
|
||||
XMLMessageDecoder decoderXML = new XMLMessageDecoder();
|
||||
decoderXML.decode(encodedXML);
|
||||
XMLMessage decodedMessage = decoderXML.getMessage();
|
||||
|
||||
|
||||
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
|
||||
Assert.assertEquals((short)1, decoderXML.getAckNumber());
|
||||
Assert.assertEquals(time, decoderXML.getTimeStamp());
|
||||
Assert.assertEquals(XMLMessageType.REGATTA, decoderXML.getXmlMsgSubType());
|
||||
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
|
||||
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
|
||||
compareXMLMessages(message, decodedMessage);
|
||||
|
||||
} catch (XMLReaderException | TransformerException e){
|
||||
|
||||
} catch (XMLReaderException e){
|
||||
fail("couldn't read file" + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two XML messages to check that they are the same.
|
||||
* @param originalMessage The first message to test.
|
||||
* @param decodedMessage The second message to test.
|
||||
*/
|
||||
public static void compareXMLMessages(XMLMessage originalMessage, XMLMessage decodedMessage) {
|
||||
|
||||
Assert.assertEquals(originalMessage.getVersionNumber(), decodedMessage.getVersionNumber());
|
||||
Assert.assertEquals(originalMessage.getAckNumber(), decodedMessage.getAckNumber());
|
||||
Assert.assertEquals(originalMessage.getTimeStamp(), decodedMessage.getTimeStamp());
|
||||
Assert.assertEquals(originalMessage.getXmlMsgSubType(), decodedMessage.getXmlMsgSubType());
|
||||
Assert.assertEquals(originalMessage.getSequenceNumber(), decodedMessage.getSequenceNumber());
|
||||
Assert.assertEquals(originalMessage.getXmlMsgLength(), decodedMessage.getXmlMsgLength());
|
||||
Assert.assertEquals(originalMessage.getXmlMessage(), decodedMessage.getXmlMessage());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a regatta.xml message can be encoded and decoded.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void regattaXMLMessageTest() throws Exception {
|
||||
xmlMessageTest("network/raceXML/Regatta.xml", XMLMessageType.REGATTA);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a race.xml message can be encoded and decoded.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void raceXMLMessageTest() throws Exception {
|
||||
xmlMessageTest("network/raceXML/Race.xml", XMLMessageType.RACE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if a boat.xml message can be encoded and decoded.
|
||||
* @throws Exception if test fails.
|
||||
*/
|
||||
@Test
|
||||
public void boatXMLMessageTest() throws Exception {
|
||||
xmlMessageTest("network/raceXML/Boats.xml", XMLMessageType.BOAT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,42 +2,92 @@ package network.Utils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static network.Utils.AC35UnitConverter.convertGPS;
|
||||
import static network.Utils.AC35UnitConverter.convertGPSToInt;
|
||||
import static network.Utils.AC35UnitConverter.convertHeading;
|
||||
import static network.Utils.AC35UnitConverter.convertTrueWindAngle;
|
||||
import static network.Utils.AC35UnitConverter.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 4/05/17.
|
||||
* Tests the pack/unpack and conversion functions in {@link AC35UnitConverter}.
|
||||
*/
|
||||
public class AC35UnitConverterTest {
|
||||
|
||||
|
||||
/**
|
||||
* Tests if gps coordinates can be unpacked.
|
||||
*/
|
||||
@Test
|
||||
public void testConvertGPS(){
|
||||
assertTrue(convertGPS(0) == 0);
|
||||
assertTrue(convertGPS(Integer.MAX_VALUE) == (double)Integer.MAX_VALUE * 180.0 / Math.pow(2, 31));
|
||||
public void testUnpackGPS(){
|
||||
assertTrue(unpackGPS(0) == 0);
|
||||
assertTrue(unpackGPS(Integer.MAX_VALUE) == (double)Integer.MAX_VALUE * 180.0 / Math.pow(2, 31));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if gps coodinates can be packed.
|
||||
*/
|
||||
@Test
|
||||
public void testConvertGPSToInt(){
|
||||
assertTrue(convertGPSToInt(0) == 0);
|
||||
assertTrue(convertGPSToInt(180) == (int)2147483648.0);
|
||||
public void testPackGPS(){
|
||||
assertTrue(packGPS(0) == 0);
|
||||
assertTrue(packGPS(180) == (int)2147483648.0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if headings/bearings can be unpacked.
|
||||
*/
|
||||
@Test
|
||||
public void testConvertHeading(){
|
||||
assertTrue(convertHeading(0) == 0);
|
||||
assertTrue(convertHeading(65536) == 360.0);
|
||||
public void testUnpackHeading(){
|
||||
assertTrue(unpackHeading(0) == 0);
|
||||
assertTrue(unpackHeading(65536) == 360.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if headings/bearings can be packed.
|
||||
*/
|
||||
@Test
|
||||
public void testPackHeading(){
|
||||
assertTrue(packHeading(0) == 0);
|
||||
assertTrue(packHeading(360) == 65536);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if true wind angles (azimuths) can be unpacked.
|
||||
*/
|
||||
@Test
|
||||
public void testConvertTrueWindAngle(){
|
||||
assertTrue(convertTrueWindAngle(0) == 0);
|
||||
assertTrue(convertTrueWindAngle(32768) == 180.0);
|
||||
public void testUnpackTrueWindAngle(){
|
||||
assertEquals(unpackTrueWindAngle((short)0), 0, 0.001);
|
||||
assertEquals(unpackTrueWindAngle((short)32767), 180.0, 0.01);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if true wind angles (azimuths) can be packed.
|
||||
*/
|
||||
@Test
|
||||
public void testPackTrueWindAngle(){
|
||||
assertTrue(packTrueWindAngle(0) == (short)0);
|
||||
assertTrue(packTrueWindAngle(180.0) == (short)32768);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if millimeters per second can be unpacked to knots.
|
||||
*/
|
||||
@Test
|
||||
public void testUnpackMMperSecToKnots(){
|
||||
assertEquals(unpackMMperSecToKnots(0), 0d, 0.001);
|
||||
assertEquals(unpackMMperSecToKnots(7331), 14.25, 0.01);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if knots can be packed into millimeters per second.
|
||||
*/
|
||||
@Test
|
||||
public void testPackKnotsToMMperSec(){
|
||||
assertEquals(packKnotsToMMperSec(0), 0, 1);
|
||||
assertEquals(packKnotsToMMperSec(7.44), 3828, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
package network;
|
||||
|
||||
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||
import network.Messages.Enums.XMLMessageType;
|
||||
import network.Messages.XMLMessage;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import shared.dataInput.XMLReader;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 19/04/17.
|
||||
*/
|
||||
public class XMLMessageEncoderTest {
|
||||
@Test
|
||||
public void getByteArrayTest() {
|
||||
try {
|
||||
|
||||
String xmlString = XMLReader.readXMLFileToString("network/raceXML/Regatta.xml", StandardCharsets.UTF_8);
|
||||
|
||||
|
||||
XMLMessage message = new XMLMessage(
|
||||
(byte)1,
|
||||
1,
|
||||
System.currentTimeMillis(),
|
||||
XMLMessageType.REGATTA,
|
||||
(short)1,
|
||||
xmlString );
|
||||
|
||||
int xmlMessageLength = xmlString.getBytes().length;
|
||||
|
||||
byte[] encodedXML = RaceVisionByteEncoder.xmlMessage(message);
|
||||
|
||||
//1 + 2 + 6 + 1 + 2 + 2 + xml.byteLength
|
||||
Assert.assertEquals(14 + xmlMessageLength, encodedXML.length);
|
||||
|
||||
} catch (XMLReaderException | TransformerException 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 {
|
||||
|
||||
+157
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<BoatConfig>
|
||||
<Modified>2017-04-19T15:49:40+1200</Modified>
|
||||
<Version>1</Version>
|
||||
<Settings>
|
||||
<RaceBoatType Type="AC45" />
|
||||
<BoatDimension BoatLength="14.019" HullLength="13.449" />
|
||||
<ZoneSize MarkZoneSize="40.347" CourseZoneSize="40.347" />
|
||||
<ZoneLimits Limit1="200" Limit2="100" Limit3="40.347" Limit4="0" Limit5="-100" />
|
||||
</Settings>
|
||||
<BoatShapes>
|
||||
<BoatShape ShapeID="0">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-2.659" />
|
||||
<Vtx Seq="2" Y="18.359" X="-2.659" />
|
||||
<Vtx Seq="3" Y="18.359" X="2.659" />
|
||||
<Vtx Seq="4" Y="0" X="2.659" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="1">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.278" />
|
||||
<Vtx Seq="2" Y="8.876" X="-1.278" />
|
||||
<Vtx Seq="3" Y="8.876" X="1.278" />
|
||||
<Vtx Seq="4" Y="0" X="1.278" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="2">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-1.1" />
|
||||
<Vtx Seq="2" Y="8.3" X="-1.1" />
|
||||
<Vtx Seq="3" Y="8.3" X="1.1" />
|
||||
<Vtx Seq="4" Y="0" X="1.1" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="3">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-0.75" />
|
||||
<Vtx Seq="2" Y="3" X="-0.75" />
|
||||
<Vtx Seq="3" Y="3" X="0.75" />
|
||||
<Vtx Seq="4" Y="0" X="0.75" />
|
||||
</Vertices>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="4">
|
||||
<Vertices>
|
||||
<Vtx Seq="1" Y="0" X="-3.46" />
|
||||
<Vtx Seq="2" Y="13.449" X="-3.46" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="13.449" X="3.46" />
|
||||
<Vtx Seq="5" Y="0" X="3.46" />
|
||||
</Vertices>
|
||||
<Catamaran>
|
||||
<Vtx Seq="1" Y="1.769" X="-2.752" />
|
||||
<Vtx Seq="2" Y="0" X="-2.813" />
|
||||
<Vtx Seq="3" Y="0" X="-3.34" />
|
||||
<Vtx Seq="4" Y="5.351" X="-3.46" />
|
||||
<Vtx Seq="5" Y="10.544" X="-3.387" />
|
||||
<Vtx Seq="6" Y="13.449" X="-3.075" />
|
||||
<Vtx Seq="7" Y="10.851" X="-2.793" />
|
||||
<Vtx Seq="8" Y="6.669" X="-2.699" />
|
||||
<Vtx Seq="9" Y="6.669" X="2.699" />
|
||||
<Vtx Seq="10" Y="10.851" X="2.793" />
|
||||
<Vtx Seq="11" Y="13.449" X="3.075" />
|
||||
<Vtx Seq="12" Y="10.544" X="3.387" />
|
||||
<Vtx Seq="13" Y="5.351" X="3.46" />
|
||||
<Vtx Seq="14" Y="0" X="3.34" />
|
||||
<Vtx Seq="15" Y="0" X="2.813" />
|
||||
<Vtx Seq="16" Y="1.769" X="2.752" />
|
||||
</Catamaran>
|
||||
<Bowsprit>
|
||||
<Vtx Seq="1" Y="6.669" X="-0.2" />
|
||||
<Vtx Seq="2" Y="11.377" X="-0.2" />
|
||||
<Vtx Seq="3" Y="14.019" X="0" />
|
||||
<Vtx Seq="4" Y="11.377" X="0.2" />
|
||||
<Vtx Seq="5" Y="6.669" X="0.2" />
|
||||
</Bowsprit>
|
||||
<Trampoline>
|
||||
<Vtx Seq="1" Y="2" X="-2.699" />
|
||||
<Vtx Seq="2" Y="6.438" X="-2.699" />
|
||||
<Vtx Seq="3" Y="6.438" X="2.699" />
|
||||
<Vtx Seq="4" Y="2" X="2.699" />
|
||||
</Trampoline>
|
||||
</BoatShape>
|
||||
<BoatShape ShapeID="5" />
|
||||
</BoatShapes>
|
||||
<Boats>>
|
||||
<Boat Type="Yacht" SourceID="101" ShapeID="4" HullNum="AC4501" ShortName="USA"
|
||||
BoatName="ORACLE TEAM USA" Country="USA">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="102" ShapeID="4" HullNum="AC4502" ShortName="GBR"
|
||||
BoatName="Land Rover BAR" Country="United Kingdom">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="103" ShapeID="4" HullNum="AC4503" ShortName="JPN"
|
||||
BoatName="SoftBank Team Japan" Country="Japan">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="104" ShapeID="4" HullNum="AC4504" ShortName="FRA"
|
||||
BoatName="Groupama Team France" Country="France">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="105" ShapeID="4" HullNum="AC4505" ShortName="SWE"
|
||||
BoatName="Artemis Racing" Country="Sweden">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
<Boat Type="Yacht" SourceID="106" ShapeID="4" HullNum="AC4506" ShortName="NZL"
|
||||
BoatName="Emirates Team New Zealand" Country="New Zealand">
|
||||
<GPSposition Z="1.738" Y="0.625" X="0.001" />
|
||||
<MastTop Z="21.496" Y="4.233" X="0.000" />
|
||||
</Boat>
|
||||
</Boats>
|
||||
</BoatConfig>
|
||||
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Race>
|
||||
<RaceID>17041901</RaceID>
|
||||
<RaceType>Fleet</RaceType>
|
||||
<CreationTimeDate>2017-04-19T15:30:00+1200</CreationTimeDate >
|
||||
<RaceStartTime Time="2019-06-01T13:30:00-0400" Postpone="false" />
|
||||
<Participants>
|
||||
<Yacht SourceID="001" Entry="Port" />
|
||||
<Yacht SourceID="002" Entry="Port" />
|
||||
<Yacht SourceID="003" Entry="Port" />
|
||||
<Yacht SourceID="004" Entry="Port" />
|
||||
<Yacht SourceID="005" Entry="Port" />
|
||||
<Yacht SourceID="006" Entry="Port" />
|
||||
</Participants>
|
||||
<Course>
|
||||
<CompoundMark CompoundMarkID="1" Name="StartLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.296577" TargetLng="-64.854304" SourceID="101" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.293771" TargetLng="-64.855242" SourceID="102" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="2" Name="M1">
|
||||
<Mark Name="M1" TargetLat="32.293039" TargetLng="-64.843983" SourceID="103" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="3" Name="WindwardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.284680" TargetLng="-64.850045" SourceID="104" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.280164" TargetLng="-64.847591" SourceID="105" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="4" Name="LeewardGate">
|
||||
<Mark SeqID="1" Name="G1" TargetLat="32.309693" TargetLng="-64.835249" SourceID="106" />
|
||||
<Mark SeqID="2" Name="G2" TargetLat="32.308046" TargetLng="-64.831785" SourceID="107" />
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="5" Name="FinishLine">
|
||||
<Mark SeqID="1" Name="PRO" TargetLat="32.317379" TargetLng="-64.839291" SourceID="108" />
|
||||
<Mark SeqID="2" Name="PIN" TargetLat="32.317257" TargetLng="-64.836260" SourceID="109" />
|
||||
</CompoundMark>
|
||||
</Course>
|
||||
<CompoundMarkSequence>
|
||||
<Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" />
|
||||
<Corner SeqID="2" CompoundMarkID="2" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="4" CompoundMarkID="4" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="3" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
|
||||
<Corner SeqID="5" CompoundMarkID="5" Rounding="SP" ZoneSize="3"/>
|
||||
</CompoundMarkSequence>
|
||||
<CourseLimit>
|
||||
<Limit SeqID="1" Lat="32.313922" Lon="-64.837168"/>
|
||||
<Limit SeqID="2" Lat="32.317379" Lon="-64.839291"/>
|
||||
<Limit SeqID="3" Lat="32.317911" Lon="-64.836996"/>
|
||||
<Limit SeqID="4" Lat="32.317257" Lon="-64.836260"/>
|
||||
<Limit SeqID="5" Lat="32.304273" Lon="-64.822834"/>
|
||||
|
||||
<Limit SeqID="6" Lat="32.279097" Lon="-64.841545"/>
|
||||
<Limit SeqID="7" Lat="32.279604" Lon="-64.849871"/>
|
||||
<Limit SeqID="8" Lat="32.289545" Lon="-64.854162"/>
|
||||
<Limit SeqID="9" Lat="32.290198" Lon="-64.858711"/>
|
||||
<Limit SeqID="10" Lat="32.297164" Lon="-64.856394"/>
|
||||
<Limit SeqID="11" Lat="32.296148" Lon="-64.849184"/>
|
||||
</CourseLimit>
|
||||
</Race>
|
||||
Reference in New Issue
Block a user