2 * ParachuteHandlerTest.java
4 package net.sf.openrocket.file.rocksim;
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import net.sf.openrocket.aerodynamics.WarningSet;
9 import net.sf.openrocket.database.Databases;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.Parachute;
14 import net.sf.openrocket.rocketcomponent.RocketComponent;
16 import java.util.HashMap;
19 * ParachuteHandler Tester.
21 public class ParachuteHandlerTest extends RocksimTestBase {
24 * The class under test.
26 public static final Class classUT = ParachuteHandler.class;
29 * The test class (this class).
31 public static final Class testClass = ParachuteHandlerTest.class;
34 * Create a test suite of all tests within this test class.
36 * @return a suite of tests
38 public static Test suite() {
39 return new TestSuite(ParachuteHandlerTest.class);
45 * @param name the name of the test to run.
47 public ParachuteHandlerTest(String name) {
54 public void setUp() throws Exception {
59 * Teardown the fixture.
61 public void tearDown() throws Exception {
66 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
68 * @throws Exception thrown if something goes awry
70 public void testOpenElement() throws Exception {
71 assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(new BodyTube()).openElement(null, null, null));
75 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
77 * @throws Exception thrown if something goes awry
79 public void testCloseElement() throws Exception {
81 BodyTube tube = new BodyTube();
82 ParachuteHandler handler = new ParachuteHandler(tube);
83 Parachute component = (Parachute) getField(handler, "chute");
84 HashMap<String, String> attributes = new HashMap<String, String>();
85 WarningSet warnings = new WarningSet();
87 handler.closeElement("Name", attributes, "Test Name", warnings);
88 assertEquals("Test Name", component.getName());
90 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
91 assertEquals(0.94d, component.getCD());
92 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
93 assertEquals(-0.94d, component.getCD());
94 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
95 assertEquals(1, warnings.size());
98 handler.closeElement("Dia", attributes, "-1", warnings);
99 assertEquals(-1d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
100 handler.closeElement("Dia", attributes, "10", warnings);
101 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
102 handler.closeElement("Dia", attributes, "foo", warnings);
103 assertEquals(1, warnings.size());
106 handler.closeElement("ShroudLineCount", attributes, "-1", warnings);
107 assertEquals(0, component.getLineCount());
108 handler.closeElement("ShroudLineCount", attributes, "10", warnings);
109 assertEquals(10, component.getLineCount());
110 handler.closeElement("ShroudLineCount", attributes, "foo", warnings);
111 assertEquals(1, warnings.size());
114 handler.closeElement("ShroudLineLen", attributes, "-1", warnings);
115 assertEquals(0d, component.getLineLength());
116 handler.closeElement("ShroudLineLen", attributes, "10", warnings);
117 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLineLength());
118 handler.closeElement("ShroudLineLen", attributes, "foo", warnings);
119 assertEquals(1, warnings.size());
125 * Method: constructor
127 * @throws Exception thrown if something goes awry
129 public void testConstructor() throws Exception {
132 new ParachuteHandler(null);
133 fail("Should have thrown IllegalArgumentException");
135 catch (IllegalArgumentException iae) {
139 BodyTube tube = new BodyTube();
140 ParachuteHandler handler = new ParachuteHandler(tube);
141 Parachute component = (Parachute) getField(handler, "chute");
142 assertContains(component, tube.getChildren());
146 * Method: setRelativePosition(RocketComponent.Position position)
148 * @throws Exception thrown if something goes awry
150 public void testSetRelativePosition() throws Exception {
151 BodyTube tube = new BodyTube();
152 ParachuteHandler handler = new ParachuteHandler(tube);
153 Parachute component = (Parachute) getField(handler, "chute");
154 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
155 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
159 * Method: getComponent()
161 * @throws Exception thrown if something goes awry
163 public void testGetComponent() throws Exception {
164 assertTrue(new ParachuteHandler(new BodyTube()).getComponent() instanceof Parachute);
168 * Method: getMaterialType()
170 * @throws Exception thrown if something goes awry
172 public void testGetMaterialType() throws Exception {
173 assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube()).getMaterialType());
177 * Method: endHandler()
179 * @throws Exception thrown if something goes awry
181 public void testEndHandler() throws Exception {
182 BodyTube tube = new BodyTube();
183 ParachuteHandler handler = new ParachuteHandler(tube);
184 Parachute component = (Parachute) getField(handler, "chute");
185 HashMap<String, String> attributes = new HashMap<String, String>();
186 WarningSet warnings = new WarningSet();
188 handler.closeElement("Xb", attributes, "-10", warnings);
189 handler.closeElement("LocationMode", attributes, "1", warnings);
190 handler.endHandler("Parachute", attributes, null, warnings);
191 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
192 assertEquals(component.getPositionValue(), -10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
194 handler.closeElement("Xb", attributes, "-10", warnings);
195 handler.closeElement("LocationMode", attributes, "2", warnings);
196 handler.endHandler("Parachute", attributes, null, warnings);
197 assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
198 assertEquals(component.getPositionValue(), 10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);