2 * ParachuteHandlerTest.java
4 package net.sf.openrocket.file.rocksim.importt;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
8 import net.sf.openrocket.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.Parachute;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import org.junit.Assert;
15 import java.util.HashMap;
18 * ParachuteHandler Tester.
20 public class ParachuteHandlerTest extends RocksimTestBase {
23 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
25 * @throws Exception thrown if something goes awry
28 public void testOpenElement() throws Exception {
29 Assert.assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
33 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
35 * @throws Exception thrown if something goes awry
38 public void testCloseElement() throws Exception {
40 BodyTube tube = new BodyTube();
41 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
42 Parachute component = (Parachute) getField(handler, "chute");
43 HashMap<String, String> attributes = new HashMap<String, String>();
44 WarningSet warnings = new WarningSet();
46 handler.closeElement("Name", attributes, "Test Name", warnings);
47 Assert.assertEquals("Test Name", component.getName());
49 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
50 Assert.assertEquals(0.94d, component.getCD(), 0.001);
51 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
52 Assert.assertEquals(-0.94d, component.getCD(), 0.001);
53 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
54 Assert.assertEquals(1, warnings.size());
57 handler.closeElement("Dia", attributes, "-1", warnings);
58 Assert.assertEquals(-1d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter(), 0.001);
59 handler.closeElement("Dia", attributes, "10", warnings);
60 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter(), 0.001);
61 handler.closeElement("Dia", attributes, "foo", warnings);
62 Assert.assertEquals(1, warnings.size());
65 handler.closeElement("ShroudLineCount", attributes, "-1", warnings);
66 Assert.assertEquals(0, component.getLineCount());
67 handler.closeElement("ShroudLineCount", attributes, "10", warnings);
68 Assert.assertEquals(10, component.getLineCount());
69 handler.closeElement("ShroudLineCount", attributes, "foo", warnings);
70 Assert.assertEquals(1, warnings.size());
73 handler.closeElement("ShroudLineLen", attributes, "-1", warnings);
74 Assert.assertEquals(0d, component.getLineLength(), 0.001);
75 handler.closeElement("ShroudLineLen", attributes, "10", warnings);
76 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLineLength(), 0.001);
77 handler.closeElement("ShroudLineLen", attributes, "foo", warnings);
78 Assert.assertEquals(1, warnings.size());
86 * @throws Exception thrown if something goes awry
89 public void testConstructor() throws Exception {
92 new ParachuteHandler(null, new WarningSet());
93 Assert.fail("Should have thrown IllegalArgumentException");
95 catch (IllegalArgumentException iae) {
99 BodyTube tube = new BodyTube();
100 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
101 Parachute component = (Parachute) getField(handler, "chute");
102 assertContains(component, tube.getChildren());
106 * Method: setRelativePosition(RocketComponent.Position position)
108 * @throws Exception thrown if something goes awry
111 public void testSetRelativePosition() throws Exception {
112 BodyTube tube = new BodyTube();
113 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
114 Parachute component = (Parachute) getField(handler, "chute");
115 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
116 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
120 * Method: getComponent()
122 * @throws Exception thrown if something goes awry
125 public void testGetComponent() throws Exception {
126 Assert.assertTrue(new ParachuteHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Parachute);
130 * Method: getMaterialType()
132 * @throws Exception thrown if something goes awry
135 public void testGetMaterialType() throws Exception {
136 Assert.assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube(), new WarningSet()).getMaterialType());
140 * Method: endHandler()
142 * @throws Exception thrown if something goes awry
145 public void testEndHandler() throws Exception {
146 BodyTube tube = new BodyTube();
147 ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
148 Parachute component = (Parachute) getField(handler, "chute");
149 HashMap<String, String> attributes = new HashMap<String, String>();
150 WarningSet warnings = new WarningSet();
152 handler.closeElement("Xb", attributes, "-10", warnings);
153 handler.closeElement("LocationMode", attributes, "1", warnings);
154 handler.endHandler("Parachute", attributes, null, warnings);
155 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
156 Assert.assertEquals(component.getPositionValue(), -10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
158 handler.closeElement("Xb", attributes, "-10", warnings);
159 handler.closeElement("LocationMode", attributes, "2", warnings);
160 handler.endHandler("Parachute", attributes, null, warnings);
161 Assert.assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
162 Assert.assertEquals(component.getPositionValue(), 10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);