Skip to content
Snippets Groups Projects
Commit 2937783c authored by Brutzman, Don's avatar Brutzman, Don
Browse files

multiple refactoring improvements

parent 2960e052
No related branches found
No related tags found
No related merge requests found
...@@ -4,17 +4,16 @@ ...@@ -4,17 +4,16 @@
*/ */
package edu.nps.moves.dis7; package edu.nps.moves.dis7;
import edu.nps.moves.dis7.entities.usa.munition.other.M1A2;
import edu.nps.moves.dis7.enumerations.Country; import edu.nps.moves.dis7.enumerations.Country;
import edu.nps.moves.dis7.enumerations.EntityKind; import edu.nps.moves.dis7.enumerations.EntityKind;
import edu.nps.moves.dis7.utilities.PduFactory; import edu.nps.moves.dis7.utilities.PduFactory;
import edu.nps.moves.dis7.enumerations.PlatformDomain; import edu.nps.moves.dis7.enumerations.PlatformDomain;
import edu.nps.moves.dis7.pdus.Domain; import edu.nps.moves.dis7.pdus.Domain;
import edu.nps.moves.dis7.pdus.EntityID; import edu.nps.moves.dis7.pdus.EntityID;
import edu.nps.moves.dis7.pdus.EntityMarking;
import edu.nps.moves.dis7.pdus.EntityStatePdu; import edu.nps.moves.dis7.pdus.EntityStatePdu;
import edu.nps.moves.dis7.pdus.EntityType; import edu.nps.moves.dis7.pdus.EntityType;
import edu.nps.moves.dis7.pdus.Pdu; import edu.nps.moves.dis7.pdus.Pdu;
import edu.nps.moves.dis7.pdus.PduBase;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
...@@ -29,8 +28,12 @@ public class EntityStatePduTest extends PduTest ...@@ -29,8 +28,12 @@ public class EntityStatePduTest extends PduTest
PduFactory pduFactory = new PduFactory(); PduFactory pduFactory = new PduFactory();
EntityStatePdu espdu = pduFactory.makeEntityStatePdu(); EntityStatePdu espdu = pduFactory.makeEntityStatePdu();
// TODO alternate constructors and utility methods
EntityID entityID = new EntityID().setSiteID((short)1).setApplicationID((short)2).setEntityID((short)3); EntityID entityID = new EntityID().setSiteID((short)1).setApplicationID((short)2).setEntityID((short)3);
espdu.setEntityID(entityID);
// TODO test various alternate constructors and utility methods
EntityType entityType = new EntityType() EntityType entityType = new EntityType()
.setEntityKind (EntityKind.PLATFORM).setEntityKind(EntityKind.PLATFORM) //(short) 1); // Platform (vs lifeform, munition, sensor, etc.); //(short) 1); // Platform (vs lifeform, munition, sensor, etc.) .setEntityKind (EntityKind.PLATFORM).setEntityKind(EntityKind.PLATFORM) //(short) 1); // Platform (vs lifeform, munition, sensor, etc.); //(short) 1); // Platform (vs lifeform, munition, sensor, etc.)
.setCountry (Country.UNITED_STATES_OF_AMERICA_USA) // 225 USA .setCountry (Country.UNITED_STATES_OF_AMERICA_USA) // 225 USA
...@@ -38,68 +41,53 @@ public class EntityStatePduTest extends PduTest ...@@ -38,68 +41,53 @@ public class EntityStatePduTest extends PduTest
.setCategory ((byte) 1) // Tank .setCategory ((byte) 1) // Tank
.setSubCategory((byte) 1) // M1 Abrams .setSubCategory((byte) 1) // M1 Abrams
.setSpecific ((byte) 3); // M1A2 Abrams .setSpecific ((byte) 3); // M1A2 Abrams
espdu.setEntityType(entityType); espdu.setEntityType(entityType);
// Alternate way using entity jar(s) // TODO this is screaming for utility methods...
espdu.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.land.M1A2()); EntityMarking entityMarking = new EntityMarking().setCharacters("Espdu Test1".getBytes());
// or simply use an enumeration by name, with accompanying import statement above espdu.setMarking (entityMarking);
espdu.setEntityType(new M1A2());
// TODO causes failure, need to debug setter:
// // Alternate way using entity jar(s)
// espdu.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.land.M1A2());
// // or simply use an enumeration by name, with accompanying import statement above
// espdu.setEntityType(new M1A2());
testOnePdu(espdu); testOnePdu(espdu);
testOnePdu(espdu.setEntityID(entityID).setEntityType(entityType)); testOnePdu(espdu.setEntityID(entityID).setEntityType(entityType)
.setMarking(entityMarking.setCharacters("Espdu Test2".getBytes()))); // pipelining
} }
/** /**
* Test single PDU for correctness according to all contained fields in this PDU type * Test single PDU for correctness according to all contained fields in this PDU type
* See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a> * See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a>
* @param newPdu separate PDU for comparison * @param createdPdu separate PDU for comparison
*/ */
@Override @Override
protected void testOnePdu(Pdu newPdu) protected void testOnePdu(Pdu createdPdu)
{ {
sendPdu(newPdu); // will wait a while testPduSendReceiveHeaderMatch (createdPdu); // shared tests in superclass
assertTrue(receivedPdu != null, "No response from network receive after " + getThreadSleepInterval() + " msec");
testPduHeaderMatch(newPdu);
// can cast PDUs at this point since PduType matched // can cast PDUs at this point since PduType matched
EntityStatePdu newEspdu = (EntityStatePdu) newPdu; EntityStatePdu createdEspdu = (EntityStatePdu) createdPdu;
EntityStatePdu receivedEspdu = (EntityStatePdu) newPdu; EntityStatePdu receivedEspdu = (EntityStatePdu) receivedPdu;
assertEquals (newEspdu.getEntityID(), receivedEspdu.getEntityID(), "mismatched EntityID");
assertEquals (createdEspdu.getEntityID(), receivedEspdu.getEntityID(), "mismatched EntityID");
// TODO Sequence number // TODO Sequence number
assertEquals (newEspdu.getEntityType(), receivedEspdu.getEntityType(), "mismatched EntityType"); assertEquals (createdEspdu.getEntityType(), receivedEspdu.getEntityType(), "mismatched EntityType");
// padding? // padding?
assertEquals (newEspdu.getEntityAppearance(), receivedEspdu.getEntityAppearance(), "mismatched EntityAppearance"); assertEquals (createdEspdu.getEntityAppearance(), receivedEspdu.getEntityAppearance(), "mismatched EntityAppearance");
assertEquals (newEspdu.getCapabilities(), receivedEspdu.getCapabilities(), "mismatched EntityCapabilities"); assertEquals (createdEspdu.getCapabilities(), receivedEspdu.getCapabilities(), "mismatched EntityCapabilities");
assertEquals (newEspdu.getEntityLocation(), receivedEspdu.getEntityLocation(), "mismatched EntityLocation"); assertEquals (createdEspdu.getEntityLocation(), receivedEspdu.getEntityLocation(), "mismatched EntityLocation");
assertEquals (newEspdu.getEntityOrientation(), receivedEspdu.getEntityOrientation(), "mismatched EntityOrientation"); assertEquals (createdEspdu.getEntityOrientation(), receivedEspdu.getEntityOrientation(), "mismatched EntityOrientation");
assertEquals (newEspdu.getForceId(), receivedEspdu.getForceId(), "mismatched EntityForceId"); assertEquals (createdEspdu.getForceId(), receivedEspdu.getForceId(), "mismatched EntityForceId");
assertEquals (newEspdu.getMarking(), receivedEspdu.getMarking(), "mismatched Marking"); assertEquals (createdEspdu.getMarking(), receivedEspdu.getMarking(), "mismatched Marking");
assertEquals (newEspdu.getDeadReckoningParameters(), receivedEspdu.getDeadReckoningParameters(), "mismatched DeadReckoningParameters"); assertEquals (createdEspdu.getDeadReckoningParameters(), receivedEspdu.getDeadReckoningParameters(), "mismatched DeadReckoningParameters");
// attached parts, utility method for NumberVariableParameters // attached parts, utility method for NumberVariableParameters
assertEquals (newEspdu.getVariableParameters(), receivedEspdu.getVariableParameters(), "mismatched VariableParameters"); assertEquals (createdEspdu.getVariableParameters(), receivedEspdu.getVariableParameters(), "mismatched VariableParameters");
assertEquals (newEspdu.getEntityLinearVelocity(), receivedEspdu.getEntityLinearVelocity(), "mismatched EntityLinearVelocity"); assertEquals (createdEspdu.getEntityLinearVelocity(), receivedEspdu.getEntityLinearVelocity(), "mismatched EntityLinearVelocity");
// trace option to compare strings, JSON or XML testPduFinishingChecks(createdPdu); // shared tests in superclass
if (false) // true || !newEspdu.toString().equals(receivedEspdu.toString()))
{
System.err.println(" newEspdu=" + newEspdu.toString());
System.err.println("receivedEspdu=" + receivedEspdu.toString());
}
assertEquals (newEspdu.toString(), receivedEspdu.toString(), "mismatched toString()");
// built-in object comparison
assertTrue (newEspdu.equalsImpl(receivedEspdu), "EntityStatePdu.equalsImpl() built-in object comparison");
// final recheck that everything adds up
assertEquals(newPdu.getMarshalledSize(),receivedPdu.getMarshalledSize(),
"Marshalled size mismatch," +
"sent (" + newPdu.getMarshalledSize() + " bytes) and " +
"recieved (" + receivedPdu.getMarshalledSize() + " bytes)");
assertEquals (newPdu.getLength(), receivedPdu.getLength(), "mismatched length"); // from Pdu superclass
// comparison of class Pdu is questionable
// assertTrue(compare(newPdu,receivedPdu), "compare() method failed for original and received PDUs");
receivedPdu = null; // ensure cleared prior to next test
} }
public static void main(String[] args) public static void main(String[] args)
......
...@@ -30,58 +30,34 @@ public class FirePduTest extends PduTest ...@@ -30,58 +30,34 @@ public class FirePduTest extends PduTest
/** Test single PDU for correctness according to all contained fields in this PDU type /** Test single PDU for correctness according to all contained fields in this PDU type
* See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a> * See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a>
* @param newPdu separate PDU for comparison * @param createdPdu separate PDU for comparison
*/ */
@Override @Override
protected void testOnePdu(Pdu newPdu) protected void testOnePdu(Pdu createdPdu)
{ {
String TODO_WARNING = " (test works standalone but mysteriously fails as part of project test suite)"; testPduSendReceiveHeaderMatch (createdPdu); // shared tests in superclass
sendPdu(newPdu); // will wait a while
assertTrue(receivedPdu != null, "No response from network receive after " + getThreadSleepInterval() + " msec"
+ TODO_WARNING);
testPduHeaderMatch(newPdu);
// can cast PDUs at this point since PduType matched // can cast PDUs at this point since PduType matched
FirePdu newEspdu = (FirePdu) newPdu; FirePdu createdFirePdu = (FirePdu) createdPdu;
FirePdu receivedEspdu = (FirePdu) newPdu; FirePdu receivedFirePdu = (FirePdu) receivedPdu;
assertEquals (newEspdu.getFiringEntityID(), receivedEspdu.getFiringEntityID(), "mismatched FiringEntityID"); assertEquals (createdFirePdu.getFiringEntityID(), receivedFirePdu.getFiringEntityID(), "mismatched FiringEntityID");
assertEquals (newEspdu.getTargetEntityID(), receivedEspdu.getTargetEntityID(), "mismatched TargetEntityID"); assertEquals (createdFirePdu.getTargetEntityID(), receivedFirePdu.getTargetEntityID(), "mismatched TargetEntityID");
// TODO Target Entity ID (App, Entity) // TODO Target Entity ID (App, Entity)
// TODO Munition/Expendable Entity ID (Site, App) // TODO Munition/Expendable Entity ID (Site, App)
// TODO Munition/Expendable Entity ID (Entity) // TODO Munition/Expendable Entity ID (Entity)
assertEquals (newEspdu.getEventID(), receivedEspdu.getEventID(), "mismatched EventID"); assertEquals (createdFirePdu.getEventID(), receivedFirePdu.getEventID(), "mismatched EventID");
assertEquals (newEspdu.getLocationInWorldCoordinates(),receivedEspdu.getLocationInWorldCoordinates(),"mismatched LocationInWorldCoordinates"); assertEquals (createdFirePdu.getLocationInWorldCoordinates(),receivedFirePdu.getLocationInWorldCoordinates(),"mismatched LocationInWorldCoordinates");
assertEquals (newEspdu.getVelocity(), receivedEspdu.getVelocity(), "mismatched Velocity"); assertEquals (createdFirePdu.getVelocity(), receivedFirePdu.getVelocity(), "mismatched Velocity");
assertEquals (newEspdu.getFireMissionIndex(), receivedEspdu.getFireMissionIndex(), "mismatched FireMissionIndex"); assertEquals (createdFirePdu.getFireMissionIndex(), receivedFirePdu.getFireMissionIndex(), "mismatched FireMissionIndex");
assertEquals (newEspdu.getMunitionExpendibleID(), receivedEspdu.getMunitionExpendibleID(), "mismatched MunitionExpendibleID"); assertEquals (createdFirePdu.getMunitionExpendibleID(), receivedFirePdu.getMunitionExpendibleID(), "mismatched MunitionExpendibleID");
// TODO Munition Descriptor // TODO Munition Descriptor
assertEquals (newEspdu.getPadding(), receivedEspdu.getPadding(), "mismatched Padding"); assertEquals (createdFirePdu.getPadding(), receivedFirePdu.getPadding(), "mismatched Padding");
assertEquals (newEspdu.getRange(), receivedEspdu.getRange(), "mismatched Range"); assertEquals (createdFirePdu.getRange(), receivedFirePdu.getRange(), "mismatched Range");
// TODO Fire Type, Padding2, Num Variable Records // TODO Fire Type, Padding2, Num Variable Records
// TODO Variable Records // TODO Variable Records
// trace option to compare strings, JSON or XML testPduFinishingChecks(createdPdu); // shared tests in superclass
if (false) // true || !newEspdu.toString().equals(receivedEspdu.toString()))
{
System.err.println(" newEspdu=" + newEspdu.toString());
System.err.println("receivedEspdu=" + receivedEspdu.toString());
}
assertEquals (newEspdu.toString(), receivedEspdu.toString(), "mismatched toString()");
// built-in object comparison
assertTrue (newEspdu.equalsImpl(receivedEspdu), "FirePdu.equalsImpl() built-in object comparison");
// final recheck that everything adds up
assertEquals(newPdu.getMarshalledSize(),receivedPdu.getMarshalledSize(),
"Marshalled size mismatch," +
"sent (" + newPdu.getMarshalledSize() + " bytes) and " +
"recieved (" + receivedPdu.getMarshalledSize() + " bytes)");
assertEquals (newPdu.getLength(), receivedPdu.getLength(), "mismatched length"); // from Pdu superclass
// comparison of class Pdu is questionable
// assertTrue(compare(newPdu,receivedPdu), "compare() method failed for original and received PDUs");
receivedPdu = null; // ensure cleared prior to next test
} }
public static void main(String[] args) public static void main(String[] args)
......
...@@ -39,6 +39,7 @@ import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; ...@@ -39,6 +39,7 @@ import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
...@@ -49,7 +50,7 @@ import org.junit.jupiter.api.BeforeEach; ...@@ -49,7 +50,7 @@ import org.junit.jupiter.api.BeforeEach;
abstract public class PduTest abstract public class PduTest
{ {
protected final long THREAD_SLEEP_INTERVAL_MSEC_DEFAULT = 100l; protected final long THREAD_SLEEP_INTERVAL_MSEC_DEFAULT = 100l; // i.e. 100, type long
private long threadSleepInterval = THREAD_SLEEP_INTERVAL_MSEC_DEFAULT; private long threadSleepInterval = THREAD_SLEEP_INTERVAL_MSEC_DEFAULT;
@BeforeAll @BeforeAll
...@@ -75,9 +76,9 @@ abstract public class PduTest ...@@ -75,9 +76,9 @@ abstract public class PduTest
pduListener = new DisThreadedNetworkInterface.PduListener() pduListener = new DisThreadedNetworkInterface.PduListener()
{ {
@Override @Override
public void incomingPdu(Pdu newPdu) public void incomingPdu(Pdu createdPdu)
{ {
setUpReceiver(newPdu); setUpReceiver(createdPdu);
} }
}; };
disNetworkInterface.addListener(pduListener); disNetworkInterface.addListener(pduListener);
...@@ -94,18 +95,18 @@ abstract public class PduTest ...@@ -94,18 +95,18 @@ abstract public class PduTest
/** /**
* Handler * Handler
* @param newPdu new PDU of interest * @param createdPdu new PDU of interest
*/ */
protected void sendPdu(Pdu newPdu) protected void sendPdu(Pdu createdPdu)
{ {
try try
{ {
disNetworkInterface.send(newPdu); disNetworkInterface.send(createdPdu);
Thread.sleep(getThreadSleepInterval()); // TODO better way to wait? Thread.sleep(getThreadSleepInterval()); // TODO better way to wait?
} }
catch (InterruptedException ex) catch (InterruptedException ex)
{ {
System.err.println(this.getClass().getName() + ".sendPdu(Pdu newPdu), error sending Multicast: " + ex.getLocalizedMessage()); System.err.println(this.getClass().getName() + ".sendPdu(Pdu createdPdu), error sending Multicast: " + ex.getLocalizedMessage());
System.exit(1); System.exit(1);
} }
} }
...@@ -131,21 +132,63 @@ abstract public class PduTest ...@@ -131,21 +132,63 @@ abstract public class PduTest
receivedPdu = newPdu; receivedPdu = newPdu;
} }
/** /**
* Common tests for fields in PDU header * Initial common tests for fields in PDU header.
* See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a> * See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a>
* @param newPdu separate PDU for comparison * @param createdPdu separate PDU for comparison
*/ */
protected void testPduHeaderMatch (Pdu newPdu) protected void testPduSendReceiveHeaderMatch (Pdu createdPdu)
{ {
assertEquals ( newPdu.getProtocolVersion(), receivedPdu.getProtocolVersion(), "mismatched ProtocolVersion"); String TEST_SUITE_WARNING = " (TODO note that test works standalone but mysteriously fails as part of project test suite)";
sendPdu(createdPdu); // will wait a while, then return receivedPdu
assertTrue(receivedPdu != null, "No response from network receive after " + getThreadSleepInterval() + " msec"
+ TEST_SUITE_WARNING);
assertTrue((createdPdu != receivedPdu), "Exact match found between createdPdu and receivedPdu object references indicates improper test configuration");
assertEquals ( createdPdu.getProtocolVersion(), receivedPdu.getProtocolVersion(), "mismatched ProtocolVersion");
// TODO compatibility version // TODO compatibility version
assertEquals ( newPdu.getExerciseID(), receivedPdu.getExerciseID(), "mismatched ExerciseID"); assertEquals ( createdPdu.getExerciseID(), receivedPdu.getExerciseID(), "mismatched ExerciseID");
assertEquals ( newPdu.getPduType(), receivedPdu.getPduType(), "mismatched PduType"); assertEquals ( createdPdu.getPduType(), receivedPdu.getPduType(), "mismatched PduType");
assertEquals ( newPdu.getProtocolFamily(), receivedPdu.getProtocolFamily(), "mismatched ProtocolFamily"); // derived from PduType assertEquals ( createdPdu.getProtocolFamily(), receivedPdu.getProtocolFamily(), "mismatched ProtocolFamily"); // derived from PduType
assertEquals(((PduBase)newPdu).getPduStatus(), ((PduBase)receivedPdu).getPduStatus(), "mismatched PduStatus"); assertEquals(((PduBase)createdPdu).getPduStatus(), ((PduBase)receivedPdu).getPduStatus(), "mismatched PduStatus");
assertEquals(((PduBase)newPdu).getPadding(), ((PduBase)receivedPdu).getPadding(), "mismatched header padding"); assertEquals(((PduBase)createdPdu).getPadding(), ((PduBase)receivedPdu).getPadding(), "mismatched header padding");
// TODO HDR length // TODO HDR length
assertEquals (newPdu.getTimestamp(), receivedPdu.getTimestamp(), "mismatched Timestamp"); assertEquals (createdPdu.getTimestamp(), receivedPdu.getTimestamp(), "mismatched Timestamp");
// trace option to show strings if mismatched (prior to assertion error)
if (!createdPdu.toString().equals(receivedPdu.toString())) // false true TODO JSON or XML
{
System.out.println("String mismatch:");
System.out.println("================================================================");
System.out.println(" newEspdu=" + createdPdu.toString());
System.out.println("================================================================");
System.out.println("receivedEspdu=" + receivedPdu.toString());
System.out.println("================================================================");
}
}
/**
* Final common tests of PDU characteristics after individual fields are checked.
* See <a href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)" target="_blank">https://en.wikipedia.org/wiki/Marshalling_(computer_science)</a>
* @param createdPdu separate PDU for comparison
*/
protected void testPduFinishingChecks (Pdu createdPdu)
{
assertEquals (createdPdu.toString(), receivedPdu.toString(), "mismatched toString()");
// built-in object comparison
assertTrue (createdPdu.equalsImpl(receivedPdu), "createdPdu.equalsImpl(receivedPdu) built-in object comparison");
// final recheck that everything adds up
assertEquals(createdPdu.getMarshalledSize(), receivedPdu.getMarshalledSize(),
"Marshalled size mismatch," +
"sent (" + createdPdu.getMarshalledSize() + " bytes) and " +
"recieved (" + receivedPdu.getMarshalledSize() + " bytes)");
assertEquals (createdPdu.getLength(), receivedPdu.getLength(), "mismatched length"); // from Pdu superclass
// comparison of class Pdu is questionable
// assertTrue(compare(createdPdu,receivedPdu), "compare() method failed for original and received PDUs");
receivedPdu = null; // ensure cleared prior to next test
} }
/** /**
...@@ -154,9 +197,9 @@ abstract public class PduTest ...@@ -154,9 +197,9 @@ abstract public class PduTest
public abstract void testRoundTrip(); public abstract void testRoundTrip();
/** Test single PDU for correctness according to all contained fields in this PDU type /** Test single PDU for correctness according to all contained fields in this PDU type
* @param newPdu separate PDU for comparison * @param createdPdu separate PDU for comparison
*/ */
protected abstract void testOnePdu(Pdu newPdu); protected abstract void testOnePdu(Pdu createdPdu);
/** /**
* Threaded sleep may be necessary to ensure completion of sending/receiving PDU * Threaded sleep may be necessary to ensure completion of sending/receiving PDU
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment