Merge branch 'master' into CommandFactory

# Conflicts:
#	racevisionGame/src/main/java/mock/app/ConnectionAcceptor.java
#	racevisionGame/src/main/java/mock/app/Event.java
#	racevisionGame/src/main/java/mock/model/commandFactory/TackGybeCommand.java
#	racevisionGame/src/main/java/mock/model/commandFactory/VMGCommand.java
#	racevisionGame/src/main/java/visualiser/gameController/ControllerServer.java
This commit is contained in:
zwu18
2017-08-12 03:18:24 +12:00
103 changed files with 6241 additions and 1880 deletions
@@ -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 WindGeneratorTest {
private WindGenerator windGenerator;
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.windGenerator = new WindGenerator(
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 = windGenerator.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 = windGenerator.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 = windGenerator.generateBaselineWind();
int randomWindCount = 1000;
for (int i = 0; i < randomWindCount; i++) {
wind = windGenerator.generateNextWind(wind);
assertTrue(wind.getWindSpeed() >= windSpeedLowerBound);
assertTrue(wind.getWindSpeed() <= windSpeedUpperBound);
assertTrue(wind.getWindDirection().degrees() >= windBearingLowerBound.degrees());
assertTrue(wind.getWindDirection().degrees() <= windBearingUpperBound.degrees());
}
}
}
@@ -1,31 +1,58 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import network.Messages.Enums.BoatActionEnum;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
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;
/**
* Created by connortaylorbrown on 4/08/17.
*/
public class WindCommandTest {
private Race race;
private Boat boat;
private MockRace race;
private MockBoat boat;
private Command upwind;
private Command downwind;
private double initial;
private double offset = 3.0;
@Before
public void setUp() {
boat = new Boat(0, "Bob", "NZ");
race = mock(MockRace.class);
boat = new MockBoat(0, "Bob", "NZ", null);
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);
initial = boat.getBearing().degrees();
}
/**
* Ensure the difference between initial and final angle is 3 degrees
*/
@Test
public void upwindCommandDecreasesAngle() {
upwind.execute();
assertEquals(initial - boat.getBearing().degrees(), -offset, 1e-5);
}
@Test
public void upwindCommandDecreasesAngle() {
public void downwindCommandIncreasesAngle() {
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){
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,108 @@
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, 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, 0);
}
/**
* Tests if a participants full message can be encoded and decoded correctly.
* @throws Exception if test fails.
*/
@Test
public void participantFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.RACE_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a ghosts full message can be encoded and decoded correctly.
* @throws Exception if test fails.
*/
@Test
public void ghostFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.GHOST_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a server full message can be encoded and decoded correctly.
* @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());
}
}
@@ -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){
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());
}
}
}
@@ -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>