Add unit tests for file version 1.7 (simulation extensions)

This commit is contained in:
Sampo Niskanen 2015-01-10 18:36:24 +02:00
parent a6c2092379
commit 8c00dbf159
4 changed files with 107 additions and 17 deletions

View File

@ -48,6 +48,7 @@ import net.sf.openrocket.rocketcomponent.TubeCoupler;
import net.sf.openrocket.simulation.SimulationOptions;
import net.sf.openrocket.simulation.customexpression.CustomExpression;
import net.sf.openrocket.simulation.exception.SimulationException;
import net.sf.openrocket.simulation.extension.impl.ScriptingExtension;
import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
import net.sf.openrocket.simulation.listeners.SimulationListener;
import net.sf.openrocket.startup.Application;
@ -241,7 +242,7 @@ public class TestRockets {
}
public Rocket makeSmallFlyable() {
public static Rocket makeSmallFlyable() {
double noseconeLength = 0.10, noseconeRadius = 0.01;
double bodytubeLength = 0.20, bodytubeRadius = 0.01, bodytubeThickness = 0.001;
@ -281,8 +282,12 @@ public class TestRockets {
String id = rocket.newFlightConfigurationID();
bodytube.setMotorMount(true);
Motor m = Application.getMotorSetDatabase().findMotors(null, null, "B4", Double.NaN, Double.NaN).get(0);
bodytube.getMotorConfiguration().get(id).setMotor(m);
MotorConfiguration motorConfig = new MotorConfiguration();
ThrustCurveMotor motor = getTestMotor();
motorConfig.setMotor(motor);
motorConfig.setEjectionDelay(5);
bodytube.getMotorConfiguration().set(id, motorConfig);
bodytube.setMotorOverhang(0.005);
rocket.getDefaultConfiguration().setFlightConfigurationID(id);
@ -643,11 +648,7 @@ public class TestRockets {
// create motor config and add a motor to it
MotorConfiguration motorConfig = new MotorConfiguration();
ThrustCurveMotor motor = new ThrustCurveMotor(
Manufacturer.getManufacturer("A"),
"F12X", "Desc", Motor.Type.UNKNOWN, new double[] {},
0.024, 0.07, new double[] { 0, 1, 2 }, new double[] { 0, 1, 0 },
new Coordinate[] { Coordinate.NUL, Coordinate.NUL, Coordinate.NUL }, "digestA");
ThrustCurveMotor motor = getTestMotor();
motorConfig.setMotor(motor);
motorConfig.setEjectionDelay(5);
@ -685,11 +686,7 @@ public class TestRockets {
// create motor config and add a motor to it
MotorConfiguration motorConfig = new MotorConfiguration();
ThrustCurveMotor motor = new ThrustCurveMotor(
Manufacturer.getManufacturer("A"),
"F12X", "Desc", Motor.Type.UNKNOWN, new double[] {},
0.024, 0.07, new double[] { 0, 1, 2 }, new double[] { 0, 1, 0 },
new Coordinate[] { Coordinate.NUL, Coordinate.NUL, Coordinate.NUL }, "digestA");
ThrustCurveMotor motor = getTestMotor();
motorConfig.setMotor(motor);
motorConfig.setEjectionDelay(5);
@ -918,6 +915,20 @@ public class TestRockets {
return OpenRocketDocumentFactory.createDocumentFromRocket(rocket);
}
public static OpenRocketDocument makeTestRocket_v107_withSimulationExtension(String script) {
Rocket rocket = makeSmallFlyable();
OpenRocketDocument document = OpenRocketDocumentFactory.createDocumentFromRocket(rocket);
Simulation sim = new Simulation(rocket);
ScriptingExtension ext = new ScriptingExtension();
ext.setEnabled(true);
ext.setLanguage("JavaScript");
ext.setScript(script);
sim.getSimulationExtensions().add(ext);
document.addSimulation(sim);
return document;
}
/*
* Create a new test rocket for testing OpenRocketSaver.estimateFileSize()
*/
@ -991,4 +1002,15 @@ public class TestRockets {
}
private static ThrustCurveMotor getTestMotor() {
return new ThrustCurveMotor(
Manufacturer.getManufacturer("A"),
"F12X", "Desc", Motor.Type.UNKNOWN, new double[] {},
0.024, 0.07, new double[] { 0, 1, 2 }, new double[] { 0, 1, 0 },
new Coordinate[] { Coordinate.NUL, Coordinate.NUL, Coordinate.NUL }, "digestA");
}
}

View File

@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.prefs.BackingStoreException;
import net.sf.openrocket.formatting.RocketDescriptor;
import net.sf.openrocket.formatting.RocketDescriptorImpl;
@ -62,6 +63,8 @@ public class ServicesForTesting extends AbstractModule {
public static class PreferencesForTesting extends Preferences {
private static java.util.prefs.Preferences root = null;
@Override
public boolean getBoolean(String key, boolean defaultValue) {
// TODO Auto-generated method stub
@ -153,8 +156,25 @@ public class ServicesForTesting extends AbstractModule {
@Override
public java.util.prefs.Preferences getNode(String nodeName) {
// TODO Auto-generated method stub
return null;
return getBaseNode().node(nodeName);
}
private java.util.prefs.Preferences getBaseNode() {
if (root == null) {
final String name = "OpenRocket-unittest-" + System.currentTimeMillis();
root = java.util.prefs.Preferences.userRoot().node(name);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
root.removeNode();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
});
}
return root;
}
}

View File

@ -28,6 +28,8 @@ import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.plugin.PluginModule;
import net.sf.openrocket.simulation.extension.impl.ScriptingExtension;
import net.sf.openrocket.simulation.extension.impl.ScriptingUtil;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.TestRockets;
@ -47,6 +49,10 @@ public class OpenRocketSaverTest {
private OpenRocketSaver saver = new OpenRocketSaver();
private static final String TMP_DIR = "./tmp/";
public static final String SIMULATION_EXTENSION_SCRIPT = "// Test < &\n// >\n// <![CDATA[";
private static Injector injector;
@BeforeClass
public static void setup() {
Module applicationModule = new ServicesForTesting();
@ -61,7 +67,7 @@ public class OpenRocketSaverTest {
}
};
Injector injector = Guice.createInjector(Modules.override(applicationModule).with(dbOverrides), pluginModule);
injector = Guice.createInjector(Modules.override(applicationModule).with(dbOverrides), pluginModule);
Application.setInjector(injector);
File tmpDir = new File("./tmp");
@ -122,6 +128,7 @@ public class OpenRocketSaverTest {
rocketDocs.add(TestRockets.makeTestRocket_v106_withMotorMountIgnitionConfig());
rocketDocs.add(TestRockets.makeTestRocket_v106_withRecoveryDeviceDeploymentConfig());
rocketDocs.add(TestRockets.makeTestRocket_v106_withStageSeparationConfig());
rocketDocs.add(TestRockets.makeTestRocket_v107_withSimulationExtension(SIMULATION_EXTENSION_SCRIPT));
rocketDocs.add(TestRockets.makeTestRocket_for_estimateFileSize());
StorageOptions options = new StorageOptions();
@ -135,6 +142,35 @@ public class OpenRocketSaverTest {
}
}
@Test
public void testUntrustedScriptDisabledOnLoad() {
OpenRocketDocument rocketDoc = TestRockets.makeTestRocket_v107_withSimulationExtension(SIMULATION_EXTENSION_SCRIPT);
StorageOptions options = new StorageOptions();
File file = saveRocket(rocketDoc, options);
OpenRocketDocument rocketDocLoaded = loadRocket(file.getPath());
assertEquals(1, rocketDocLoaded.getSimulations().size());
assertEquals(1, rocketDocLoaded.getSimulations().get(0).getSimulationExtensions().size());
ScriptingExtension ext = (ScriptingExtension) rocketDocLoaded.getSimulations().get(0).getSimulationExtensions().get(0);
assertEquals(false, ext.isEnabled());
assertEquals(SIMULATION_EXTENSION_SCRIPT, ext.getScript());
}
@Test
public void testTrustedScriptEnabledOnLoad() {
OpenRocketDocument rocketDoc = TestRockets.makeTestRocket_v107_withSimulationExtension("TESTING");
injector.getInstance(ScriptingUtil.class).setTrustedScript("JavaScript", "TESTING", true);
StorageOptions options = new StorageOptions();
File file = saveRocket(rocketDoc, options);
OpenRocketDocument rocketDocLoaded = loadRocket(file.getPath());
assertEquals(1, rocketDocLoaded.getSimulations().size());
assertEquals(1, rocketDocLoaded.getSimulations().get(0).getSimulationExtensions().size());
ScriptingExtension ext = (ScriptingExtension) rocketDocLoaded.getSimulations().get(0).getSimulationExtensions().get(0);
assertEquals(true, ext.isEnabled());
assertEquals("TESTING", ext.getScript());
}
/*
* Test how accurate estimatedFileSize is.
*
@ -258,6 +294,17 @@ public class OpenRocketSaverTest {
assertEquals(106, getCalculatedFileVersion(rocketDoc));
}
////////////////////////////////
// Tests for File Version 1.7 //
////////////////////////////////
@Test
public void testFileVersion107_withSimulationExtension() {
OpenRocketDocument rocketDoc = TestRockets.makeTestRocket_v107_withSimulationExtension(SIMULATION_EXTENSION_SCRIPT);
assertEquals(107, getCalculatedFileVersion(rocketDoc));
}
/*
* Utility Functions
*/
@ -273,6 +320,7 @@ public class OpenRocketSaverTest {
try {
rocketDoc = loader.load();
} catch (RocketLoadException e) {
e.printStackTrace();
fail("RocketLoadException while loading file " + fileName + " : " + e.getMessage());
}
return rocketDoc;

View File

@ -25,7 +25,7 @@ public class DocumentConfigTest extends BaseTestCase {
public void testAllVersionsTested() {
// Update this after creating new unit tests in OpenRocketSaver for a new OR file version
String[] testedVersionsStr = { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6" };
String[] testedVersionsStr = { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7" };
List<String> supportedVersions = Arrays.asList(DocumentConfig.SUPPORTED_VERSIONS);
List<String> testedVersions = Arrays.asList(testedVersionsStr);