ae9ca2aa74a9f57c58e4b1e029d906d8c392b68a
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / ParachuteHandlerTest.java
1 /*
2  * ParachuteHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
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;
14
15 import java.util.HashMap;
16
17 /**
18  * ParachuteHandler Tester.
19  */
20 public class ParachuteHandlerTest extends RocksimTestBase {
21
22     /**
23      * The class under test.
24      */
25     public static final Class classUT = ParachuteHandler.class;
26
27     /**
28      * The test class (this class).
29      */
30     public static final Class testClass = ParachuteHandlerTest.class;
31
32     /**
33      * Create a test suite of all tests within this test class.
34      *
35      * @return a suite of tests
36      */
37     public static Test suite() {
38         return new TestSuite(ParachuteHandlerTest.class);
39     }
40
41     /**
42      * Test constructor.
43      *
44      * @param name the name of the test to run.
45      */
46     public ParachuteHandlerTest(String name) {
47         super(name);
48     }
49
50     /**
51      * Setup the fixture.
52      */
53     public void setUp() throws Exception {
54         super.setUp();
55     }
56
57     /**
58      * Teardown the fixture.
59      */
60     public void tearDown() throws Exception {
61         super.tearDown();
62     }
63
64     /**
65      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
66      *
67      * @throws Exception thrown if something goes awry
68      */
69     public void testOpenElement() throws Exception {
70         assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
71     }
72
73     /**
74      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
75      *
76      * @throws Exception thrown if something goes awry
77      */
78     public void testCloseElement() throws Exception {
79
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();
85
86         handler.closeElement("Name", attributes, "Test Name", warnings);
87         assertEquals("Test Name", component.getName());
88
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());
95         warnings.clear();
96
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());
103         warnings.clear();
104
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());
111         warnings.clear();
112
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());
119         warnings.clear();
120
121     }
122
123     /**
124      * Method: constructor
125      *
126      * @throws Exception thrown if something goes awry
127      */
128     public void testConstructor() throws Exception {
129
130         try {
131             new ParachuteHandler(null, new WarningSet());
132             fail("Should have thrown IllegalArgumentException");
133         }
134         catch (IllegalArgumentException iae) {
135             //success
136         }
137
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());
142     }
143
144     /**
145      * Method: setRelativePosition(RocketComponent.Position position)
146      *
147      * @throws Exception thrown if something goes awry
148      */
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());
155     }
156
157     /**
158      * Method: getComponent()
159      *
160      * @throws Exception thrown if something goes awry
161      */
162     public void testGetComponent() throws Exception {
163         assertTrue(new ParachuteHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Parachute);
164     }
165
166     /**
167      * Method: getMaterialType()
168      *
169      * @throws Exception thrown if something goes awry
170      */
171     public void testGetMaterialType() throws Exception {
172         assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube(), new WarningSet()).getMaterialType());
173     }
174
175     /**
176      * Method: endHandler()
177      *
178      * @throws Exception thrown if something goes awry
179      */
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();
186
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);
192
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);
198     }
199
200
201 }