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.file.simplesax.PlainTextHandler;
10 import net.sf.openrocket.material.Material;
11 import net.sf.openrocket.rocketcomponent.BodyTube;
12 import net.sf.openrocket.rocketcomponent.Parachute;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
15 import java.util.HashMap;
18 * ParachuteHandler Tester.
20 public class ParachuteHandlerTest extends RocksimTestBase {
23 * The class under test.
25 public static final Class classUT = ParachuteHandler.class;
28 * The test class (this class).
30 public static final Class testClass = ParachuteHandlerTest.class;
33 * Create a test suite of all tests within this test class.
35 * @return a suite of tests
37 public static Test suite() {
38 return new TestSuite(ParachuteHandlerTest.class);
44 * @param name the name of the test to run.
46 public ParachuteHandlerTest(String name) {
53 public void setUp() throws Exception {
58 * Teardown the fixture.
60 public void tearDown() throws Exception {
65 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
67 * @throws Exception thrown if something goes awry
69 public void testOpenElement() throws Exception {
70 assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
74 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
76 * @throws Exception thrown if something goes awry
78 public void testCloseElement() throws Exception {
80 BodyTube tube = new BodyTube();
81 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
82 Parachute component = (Parachute) getField(handler, "chute");
83 HashMap<String, String> attributes = new HashMap<String, String>();
84 WarningSet warnings = new WarningSet();
86 handler.closeElement("Name", attributes, "Test Name", warnings);
87 assertEquals("Test Name", component.getName());
89 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
90 assertEquals(0.94d, component.getCD());
91 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
92 assertEquals(-0.94d, component.getCD());
93 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
94 assertEquals(1, warnings.size());
97 handler.closeElement("Dia", attributes, "-1", warnings);
98 assertEquals(-1d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
99 handler.closeElement("Dia", attributes, "10", warnings);
100 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
101 handler.closeElement("Dia", attributes, "foo", warnings);
102 assertEquals(1, warnings.size());
105 handler.closeElement("ShroudLineCount", attributes, "-1", warnings);
106 assertEquals(0, component.getLineCount());
107 handler.closeElement("ShroudLineCount", attributes, "10", warnings);
108 assertEquals(10, component.getLineCount());
109 handler.closeElement("ShroudLineCount", attributes, "foo", warnings);
110 assertEquals(1, warnings.size());
113 handler.closeElement("ShroudLineLen", attributes, "-1", warnings);
114 assertEquals(0d, component.getLineLength());
115 handler.closeElement("ShroudLineLen", attributes, "10", warnings);
116 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLineLength());
117 handler.closeElement("ShroudLineLen", attributes, "foo", warnings);
118 assertEquals(1, warnings.size());
124 * Method: constructor
126 * @throws Exception thrown if something goes awry
128 public void testConstructor() throws Exception {
131 new ParachuteHandler(null, new WarningSet());
132 fail("Should have thrown IllegalArgumentException");
134 catch (IllegalArgumentException iae) {
138 BodyTube tube = new BodyTube();
139 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
140 Parachute component = (Parachute) getField(handler, "chute");
141 assertContains(component, tube.getChildren());
145 * Method: setRelativePosition(RocketComponent.Position position)
147 * @throws Exception thrown if something goes awry
149 public void testSetRelativePosition() throws Exception {
150 BodyTube tube = new BodyTube();
151 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
152 Parachute component = (Parachute) getField(handler, "chute");
153 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
154 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
158 * Method: getComponent()
160 * @throws Exception thrown if something goes awry
162 public void testGetComponent() throws Exception {
163 assertTrue(new ParachuteHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Parachute);
167 * Method: getMaterialType()
169 * @throws Exception thrown if something goes awry
171 public void testGetMaterialType() throws Exception {
172 assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube(), new WarningSet()).getMaterialType());
176 * Method: endHandler()
178 * @throws Exception thrown if something goes awry
180 public void testEndHandler() throws Exception {
181 BodyTube tube = new BodyTube();
182 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
183 Parachute component = (Parachute) getField(handler, "chute");
184 HashMap<String, String> attributes = new HashMap<String, String>();
185 WarningSet warnings = new WarningSet();
187 handler.closeElement("Xb", attributes, "-10", warnings);
188 handler.closeElement("LocationMode", attributes, "1", warnings);
189 handler.endHandler("Parachute", attributes, null, warnings);
190 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
191 assertEquals(component.getPositionValue(), -10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
193 handler.closeElement("Xb", attributes, "-10", warnings);
194 handler.closeElement("LocationMode", attributes, "2", warnings);
195 handler.endHandler("Parachute", attributes, null, warnings);
196 assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
197 assertEquals(component.getPositionValue(), 10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);