update unit tests for simulation aborts instead of exceptions

This commit is contained in:
JoePfeiffer 2024-01-11 09:31:20 -07:00
parent f5755cc4af
commit c803104a16

View File

@ -1,6 +1,7 @@
package net.sf.openrocket.simulation; package net.sf.openrocket.simulation;
import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.logging.SimulationAbort;
import net.sf.openrocket.rocketcomponent.FlightConfiguration; import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId; import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
@ -32,15 +33,12 @@ public class DisableStageTest extends BaseTestCase {
simDisabled.getOptions().setISAAtmosphere(true); simDisabled.getOptions().setISAAtmosphere(true);
simDisabled.getOptions().setTimeStep(0.05); simDisabled.getOptions().setTimeStep(0.05);
// Since there are no stages, the simulation should throw an exception. simDisabled.simulate();
// Needs fixing for SimulationAbort
try { // Since there are no stages, the simulation should abort
simDisabled.simulate(); FlightEvent abort = simDisabled.getSimulatedData().getBranch(0).getLastEvent(FlightEvent.Type.SIM_ABORT);
} catch (SimulationException e) { Assert.assertNotNull("Empty simulation failed to abort", abort);
if (!(e instanceof MotorIgnitionException)) { Assert.assertEquals("Abort cause did not match", SimulationAbort.Cause.NO_ACTIVE_STAGES, ((SimulationAbort)(abort.getData())).getCause());
Assert.fail("Simulation should have thrown a MotorIgnitionException");
}
}
//// Test re-enabling the stage. //// Test re-enabling the stage.
Rocket rocketOriginal = TestRockets.makeEstesAlphaIII(); Rocket rocketOriginal = TestRockets.makeEstesAlphaIII();
@ -210,27 +208,32 @@ public class DisableStageTest extends BaseTestCase {
simRemoved.getOptions().setISAAtmosphere(true); simRemoved.getOptions().setISAAtmosphere(true);
simRemoved.getOptions().setTimeStep(0.05); simRemoved.getOptions().setTimeStep(0.05);
try {
simRemoved.simulate();
} catch(Exception e) {
Assert.fail("unexpected exception " + e);
}
// There should be no motors left at this point, so we should abort on no motors
FlightEvent abort = simRemoved.getSimulatedData().getBranch(0).getLastEvent(FlightEvent.Type.SIM_ABORT);
Assert.assertNotNull("Empty simulation failed to abort", abort);
Assert.assertEquals("Abort cause did not match", SimulationAbort.Cause.NOMOTORSDEFINED, ((SimulationAbort)(abort.getData())).getCause());
Simulation simDisabled = new Simulation(rocketDisabled); Simulation simDisabled = new Simulation(rocketDisabled);
simDisabled.setFlightConfigurationId(fid); simDisabled.setFlightConfigurationId(fid);
simDisabled.getOptions().setISAAtmosphere(true); simDisabled.getOptions().setISAAtmosphere(true);
simDisabled.getOptions().setTimeStep(0.05); simDisabled.getOptions().setTimeStep(0.05);
// There should be no motors left at this point, so a no motors exception should be thrown try {
try { simDisabled.simulate();
simRemoved.simulate(); } catch(Exception e) {
} catch (SimulationException e) { Assert.fail("unexpected exception " + e);
if (!(e instanceof MotorIgnitionException)) { }
Assert.fail("Simulation failed: " + e);
}
}
try { // There should be no motors left at this point, so we should abort on no motors
simDisabled.simulate(); abort = simDisabled.getSimulatedData().getBranch(0).getLastEvent(FlightEvent.Type.SIM_ABORT);
} catch (SimulationException e) { Assert.assertNotNull("Empty simulation failed to abort", abort);
if (!(e instanceof MotorIgnitionException)) { Assert.assertEquals("Abort cause did not match", SimulationAbort.Cause.NOMOTORSDEFINED, ((SimulationAbort)(abort.getData())).getCause());
Assert.fail("Simulation failed: " + e);
}
}
//// Test re-enabling the stage. //// Test re-enabling the stage.
Rocket rocketOriginal = TestRockets.makeFalcon9Heavy(); Rocket rocketOriginal = TestRockets.makeFalcon9Heavy();
@ -265,13 +268,16 @@ public class DisableStageTest extends BaseTestCase {
//// Test that the top stage is the booster stage //// Test that the top stage is the booster stage
Assert.assertEquals(rocketDisabled.getTopmostStage(simDisabled.getActiveConfiguration()), rocketDisabled.getStage(2)); Assert.assertEquals(rocketDisabled.getTopmostStage(simDisabled.getActiveConfiguration()), rocketDisabled.getStage(2));
try { // Just check that the simulation runs without exceptions try {
simDisabled.simulate(); simDisabled.simulate();
} catch (SimulationException e) { } catch(Exception e) {
if (!(e instanceof MotorIgnitionException)) { Assert.fail("unexpected exception " + e);
Assert.fail("Simulation failed: " + e); }
}
} // Sim will tumble under
FlightEvent abort = simDisabled.getSimulatedData().getBranch(0).getLastEvent(FlightEvent.Type.SIM_ABORT);
Assert.assertNotNull("Unstable booster failed to abort", abort);
Assert.assertEquals("Abort cause did not match", SimulationAbort.Cause.TUMBLE_UNDER_THRUST, ((SimulationAbort)(abort.getData())).getCause());
} }
/** /**