From 8c00dbf159424d148b1613a544201394091db1a5 Mon Sep 17 00:00:00 2001 From: Sampo Niskanen Date: Sat, 10 Jan 2015 18:36:24 +0200 Subject: [PATCH] Add unit tests for file version 1.7 (simulation extensions) --- .../net/sf/openrocket/util/TestRockets.java | 48 +++++++++++++----- .../net/sf/openrocket/ServicesForTesting.java | 24 ++++++++- .../file/openrocket/OpenRocketSaverTest.java | 50 ++++++++++++++++++- .../importt/DocumentConfigTest.java | 2 +- 4 files changed, 107 insertions(+), 17 deletions(-) diff --git a/core/src/net/sf/openrocket/util/TestRockets.java b/core/src/net/sf/openrocket/util/TestRockets.java index fedb59f4a..f4fff0762 100644 --- a/core/src/net/sf/openrocket/util/TestRockets.java +++ b/core/src/net/sf/openrocket/util/TestRockets.java @@ -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"); + } + + } diff --git a/core/test/net/sf/openrocket/ServicesForTesting.java b/core/test/net/sf/openrocket/ServicesForTesting.java index f80300cd5..8f729ffce 100644 --- a/core/test/net/sf/openrocket/ServicesForTesting.java +++ b/core/test/net/sf/openrocket/ServicesForTesting.java @@ -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; } } diff --git a/core/test/net/sf/openrocket/file/openrocket/OpenRocketSaverTest.java b/core/test/net/sf/openrocket/file/openrocket/OpenRocketSaverTest.java index 96f826c08..ca18850a1 100644 --- a/core/test/net/sf/openrocket/file/openrocket/OpenRocketSaverTest.java +++ b/core/test/net/sf/openrocket/file/openrocket/OpenRocketSaverTest.java @@ -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// supportedVersions = Arrays.asList(DocumentConfig.SUPPORTED_VERSIONS); List testedVersions = Arrays.asList(testedVersionsStr);