DGP - added DensityType parsing for recovery devices
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / StreamerHandlerTest.java
1 /*
2  * StreamerHandlerTest.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.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.Streamer;
14
15 import java.util.HashMap;
16
17 /**
18  * StreamerHandler Tester.
19  */
20 public class StreamerHandlerTest extends RocksimTestBase {
21
22     /**
23      * The class under test.
24      */
25     public static final Class classUT = StreamerHandler.class;
26
27     /**
28      * The test class (this class).
29      */
30     public static final Class testClass = StreamerHandlerTest.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(StreamerHandlerTest.class);
39     }
40
41     /**
42      * Test constructor.
43      *
44      * @param name the name of the test to run.
45      */
46     public StreamerHandlerTest(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 StreamerHandler(new BodyTube()).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         StreamerHandler handler = new StreamerHandler(tube);
82         Streamer component = (Streamer) getField(handler, "streamer");
83         HashMap<String, String> attributes = new HashMap<String, String>();
84         WarningSet warnings = new WarningSet();
85
86         handler.closeElement("Width", attributes, "0", warnings);
87         assertEquals(0d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
88         handler.closeElement("Width", attributes, "10", warnings);
89         assertEquals(10d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
90         handler.closeElement("Width", attributes, "foo", warnings);
91         assertEquals(1, warnings.size());
92         warnings.clear();
93
94         handler.closeElement("Len", attributes, "-1", warnings);
95         assertEquals(0d, component.getStripLength());
96         handler.closeElement("Len", attributes, "10", warnings);
97         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
98         handler.closeElement("Len", attributes, "10.0", warnings);
99         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
100         handler.closeElement("Len", attributes, "foo", warnings);
101         assertEquals(1, warnings.size());
102         warnings.clear();
103
104         handler.closeElement("Name", attributes, "Test Name", warnings);
105         assertEquals("Test Name", component.getName());
106
107         handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
108         assertEquals(0.94d, component.getCD());
109         handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
110         assertEquals(-0.94d, component.getCD());
111         handler.closeElement("DragCoefficient", attributes, "foo", warnings);
112         assertEquals(1, warnings.size());
113         warnings.clear();
114
115     }
116
117     /**
118      * Method: constructor
119      *
120      * @throws Exception thrown if something goes awry
121      */
122     public void testConstructor() throws Exception {
123
124         try {
125             new StreamerHandler(null);
126             fail("Should have thrown IllegalArgumentException");
127         }
128         catch (IllegalArgumentException iae) {
129             //success
130         }
131
132         BodyTube tube = new BodyTube();
133         StreamerHandler handler = new StreamerHandler(tube);
134         Streamer component = (Streamer) getField(handler, "streamer");
135         assertContains(component, tube.getChildren());
136     }
137
138     /**
139      * Method: setRelativePosition(RocketComponent.Position position)
140      *
141      * @throws Exception thrown if something goes awry
142      */
143     public void testSetRelativePosition() throws Exception {
144         BodyTube tube = new BodyTube();
145         StreamerHandler handler = new StreamerHandler(tube);
146         Streamer component = (Streamer) getField(handler, "streamer");
147         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
148         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
149     }
150
151     /**
152      * Method: getComponent()
153      *
154      * @throws Exception thrown if something goes awry
155      */
156     public void testGetComponent() throws Exception {
157         assertTrue(new StreamerHandler(new BodyTube()).getComponent() instanceof Streamer);
158     }
159
160     /**
161      * Method: getMaterialType()
162      *
163      * @throws Exception thrown if something goes awry
164      */
165     public void testGetMaterialType() throws Exception {
166         assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube()).getMaterialType());
167     }
168
169     /**
170      * Method: endHandler()
171      *
172      * @throws Exception thrown if something goes awry
173      */
174     public void testEndHandler() throws Exception {
175         BodyTube tube = new BodyTube();
176         StreamerHandler handler = new StreamerHandler(tube);
177         Streamer component = (Streamer) getField(handler, "streamer");
178         HashMap<String, String> attributes = new HashMap<String, String>();
179         WarningSet warnings = new WarningSet();
180
181         handler.closeElement("Xb", attributes, "-10", warnings);
182         handler.closeElement("LocationMode", attributes, "1", warnings);
183         handler.endHandler("Streamer", attributes, null, warnings);
184         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
185         assertEquals(component.getPositionValue(), -10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
186
187         handler.closeElement("Xb", attributes, "-10", warnings);
188         handler.closeElement("LocationMode", attributes, "2", warnings);
189         handler.endHandler("Streamer", attributes, null, warnings);
190         assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
191         assertEquals(component.getPositionValue(), 10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
192
193         handler.closeElement("Thickness", attributes, "0.02", warnings);
194         assertEquals(0.01848, handler.computeDensity(RocksimDensityType.ROCKSIM_BULK, 924d));
195
196         //Test Density Type 0 (Bulk)
197         handler.closeElement("Density", attributes, "924.0", warnings);
198         handler.closeElement("DensityType", attributes, "0", warnings);
199         handler.endHandler("Streamer", attributes, null, warnings);
200         assertEquals(0.01848d, component.getMaterial().getDensity());
201
202         //Test Density Type 1 (Surface)
203         handler.closeElement("Density", attributes, "0.006685", warnings);
204         handler.closeElement("DensityType", attributes, "1", warnings);
205         handler.endHandler("Streamer", attributes, null, warnings);
206         assertTrue(Math.abs(0.06685d - component.getMaterial().getDensity()) < 0.00001);
207
208         //Test Density Type 2 (Line)
209         handler.closeElement("Density", attributes, "0.223225", warnings);
210         handler.closeElement("DensityType", attributes, "2", warnings);
211         handler.closeElement("Len", attributes, "3810.", warnings);
212         handler.closeElement("Width", attributes, "203.2", warnings);
213         handler.endHandler("Streamer", attributes, null, warnings);
214
215         assertEquals(1.728190092, component.getMass());
216
217     }
218
219 }