Skip to content

Commit

Permalink
avoid null pointer exception, update information layers when layer 2 …
Browse files Browse the repository at this point in the history
…is set, and added IFFPdu unit test
  • Loading branch information
fo-ifad committed Mar 4, 2024
1 parent f37994f commit c045295
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/edu/nps/moves/dis7/IFFPdu.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public int getMarshalledSize() {
int marshalSize = 0;
marshalSize = super.getMarshalledSize();
marshalSize = marshalSize + layer1.getMarshalledSize();//emittingEntityId
if (layer2 != null) {
if (isLayerPresent(2)) {
marshalSize = marshalSize + layer2.getMarshalledSize();
}

return marshalSize;
}

Expand All @@ -59,6 +60,7 @@ public IFFLayer2 getLayer2() {

public void setLayer2(IFFLayer2 layer2) {
this.layer2 = layer2;
updateInformationLayersPresent();
}

private void updateInformationLayersPresent() {
Expand Down Expand Up @@ -109,7 +111,8 @@ public void marshal(java.nio.ByteBuffer buff) {
public void unmarshal(java.nio.ByteBuffer buff) {
super.unmarshal(buff);
layer1.unmarshal(buff);
if (isLayerPresent(2)) {
if (isLayerPresent(2) ) {
layer2 = new IFFLayer2();
layer2.unmarshal(buff);
}
} // end of unmarshal method
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/nps/moves/dis7/PduFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ public Pdu createPdu(ByteBuffer buff) {
case ENTITY_STATE_UPDATE:
aPdu = new EntityStateUpdatePdu();
break;
case IFF_ATC_NAVAIDS:
aPdu = new IFFPdu();
break;
default:
this.logger.log(Level.INFO, "PDU not implemented. Type = " + pduType + "\n");
if (pduTypeEnum != null) {
Expand Down
172 changes: 172 additions & 0 deletions src/test/java/edu/nps/moves/dis7/IFFPduTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package edu.nps.moves.dis7;

import java.io.IOException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author fo
*/
public class IFFPduTest {

public IFFPduTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
public void marshal() {
IFFPdu ipdu = new IFFPdu();

byte[] buffer = ipdu.marshal();

assertEquals(buffer.length, ipdu.getLength());
}

@Test
public void unmarshal() throws IOException {
PduFactory factory = new PduFactory();
Pdu pdu = factory.createPdu(edu.nps.moves.dis7.PduFileLoader.load("IFFPdu.raw"));

// Header
assertEquals(7, pdu.getProtocolVersion());
assertEquals(1, pdu.getExerciseID());
assertEquals(28, pdu.getPduType());
assertEquals(6, pdu.getProtocolFamily());
assertEquals(136, pdu.getLength());
assertEquals(13, pdu.getPduStatus());
assertEquals(0, pdu.getPadding());

IFFPdu ipdu =(IFFPdu) pdu;



//Layer1
final IFFLayer1 layer1 = ipdu.getLayer1();

//EmittingEntityId
assertEquals(1, layer1.getEmittingEntityId().getSiteID());
assertEquals(206, layer1.getEmittingEntityId().getApplicationID());
assertEquals(55, layer1.getEmittingEntityId().getEntityID());

//EventId
assertEquals(1, layer1.getEventId().getSimulationAddress().getSite());
assertEquals(206, layer1.getEventId().getSimulationAddress().getApplication());
assertEquals(1, layer1.getEventId().getEventNumber());

//RelativeAntennaLocation
assertEquals(1, layer1.getRelativeAntennaLocation().getX(),0.001);
assertEquals(1, layer1.getRelativeAntennaLocation().getY(),0.001);
assertEquals(1, layer1.getRelativeAntennaLocation().getZ(),0.001);

//SystemId
assertEquals(1, layer1.getSystemId().getSystemType());
assertEquals(1, layer1.getSystemId().getSystemName());
assertEquals(3, layer1.getSystemId().getSystemMode());
assertEquals(2, layer1.getSystemId().getChangeOptions().getValue());

//SystemDesignator
assertEquals(55, layer1.getSystemDesignator());

//SystemSpecificData
assertEquals(55, layer1.getSystemSpecificData());

//FundamentalOperationalData

//SystemStatus
assertEquals(1, layer1.getFundamentalOperationalData().getSystemStatus());

//DataField1
assertEquals(0, layer1.getFundamentalOperationalData().getDataField1());

//InformationLayers
assertEquals(3, layer1.getFundamentalOperationalData().getInformationLayers());

//DataField2
assertEquals(14, layer1.getFundamentalOperationalData().getDataField2());

//Parameter1
assertEquals(57403, layer1.getFundamentalOperationalData().getParameter1());

//Parameter1
assertEquals(61439, layer1.getFundamentalOperationalData().getParameter2());

//Parameter1
assertEquals(61439, layer1.getFundamentalOperationalData().getParameter3());

//Parameter1
assertEquals(57389, layer1.getFundamentalOperationalData().getParameter4());

//Parameter1
assertEquals(57364, layer1.getFundamentalOperationalData().getParameter5());

//Parameter1
assertEquals(57344, layer1.getFundamentalOperationalData().getParameter6());



//Layer2
final IFFLayer2 layer2 = ipdu.getLayer2();

//LayerHeader
assertEquals(2, layer2.getLayerHeader().getLayerNumber());
assertEquals(76, layer2.getLayerHeader().getLength());
assertEquals(0, layer2.getLayerHeader().getLayerSpecificInformation());

//BeamData
assertEquals(56, layer2.getBeamData().getBeamAzimuthCenter(), 0.001);
assertEquals(57, layer2.getBeamData().getBeamAzimuthSweep(), 0.001);
assertEquals(58, layer2.getBeamData().getBeamElevationCenter(), 0.001);
assertEquals(60, layer2.getBeamData().getBeamElevationSweep(), 0.001);
assertEquals(59, layer2.getBeamData().getBeamSweepSync(), 0.001);

//IffFundamentalParameterData
assertEquals(2, layer2.getIffFundamentalParameterData().size());

assertEquals(0, layer2.getIffFundamentalParameterData().get(0).getApplicableModes());
assertEquals(5, layer2.getIffFundamentalParameterData().get(0).getBurstLength());
assertEquals(5, layer2.getIffFundamentalParameterData().get(0).getErp(), 0.001);
assertEquals(5, layer2.getIffFundamentalParameterData().get(0).getFrequency(), 0.001);
assertEquals(5, layer2.getIffFundamentalParameterData().get(0).getPgrf(), 0.001);
assertEquals(5, layer2.getIffFundamentalParameterData().get(0).getPulseWidth(), 0.001);
assertEquals(3, layer2.getIffFundamentalParameterData().get(0).getSystemSpecificData().length);
assertEquals(0, layer2.getIffFundamentalParameterData().get(0).getSystemSpecificData()[0]);
assertEquals(0, layer2.getIffFundamentalParameterData().get(0).getSystemSpecificData()[1]);
assertEquals(0, layer2.getIffFundamentalParameterData().get(0).getSystemSpecificData()[2]);

assertEquals(0, layer2.getIffFundamentalParameterData().get(1).getApplicableModes());
assertEquals(6, layer2.getIffFundamentalParameterData().get(1).getBurstLength());
assertEquals(6, layer2.getIffFundamentalParameterData().get(1).getErp(), 0.001);
assertEquals(6, layer2.getIffFundamentalParameterData().get(1).getFrequency(), 0.001);
assertEquals(6, layer2.getIffFundamentalParameterData().get(1).getPgrf(), 0.001);
assertEquals(6, layer2.getIffFundamentalParameterData().get(1).getPulseWidth(), 0.001);
assertEquals(3, layer2.getIffFundamentalParameterData().get(1).getSystemSpecificData().length);
assertEquals(0, layer2.getIffFundamentalParameterData().get(1).getSystemSpecificData()[0]);
assertEquals(0, layer2.getIffFundamentalParameterData().get(1).getSystemSpecificData()[1]);
assertEquals(0, layer2.getIffFundamentalParameterData().get(1).getSystemSpecificData()[2]);

//SecondaryOperationalData
assertEquals(2, layer2.getSecondaryOperationalData().getNumberOfIFFFundamentalParameterRecords());
assertEquals(0, layer2.getSecondaryOperationalData().getOperationalData1());
assertEquals(0, layer2.getSecondaryOperationalData().getOperationalData2());
}
}
Binary file added src/test/resources/edu/nps/moves/dis7/IFFPdu.raw
Binary file not shown.

0 comments on commit c045295

Please sign in to comment.