2 * StreamerHandlerTest.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.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.Streamer;
15 import java.util.HashMap;
18 * StreamerHandler Tester.
20 public class StreamerHandlerTest extends RocksimTestBase {
23 * The class under test.
25 public static final Class classUT = StreamerHandler.class;
28 * The test class (this class).
30 public static final Class testClass = StreamerHandlerTest.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(StreamerHandlerTest.class);
44 * @param name the name of the test to run.
46 public StreamerHandlerTest(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 StreamerHandler(new BodyTube()).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 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();
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());
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());
104 handler.closeElement("Name", attributes, "Test Name", warnings);
105 assertEquals("Test Name", component.getName());
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());
118 * Method: constructor
120 * @throws Exception thrown if something goes awry
122 public void testConstructor() throws Exception {
125 new StreamerHandler(null);
126 fail("Should have thrown IllegalArgumentException");
128 catch (IllegalArgumentException iae) {
132 BodyTube tube = new BodyTube();
133 StreamerHandler handler = new StreamerHandler(tube);
134 Streamer component = (Streamer) getField(handler, "streamer");
135 assertContains(component, tube.getChildren());
139 * Method: setRelativePosition(RocketComponent.Position position)
141 * @throws Exception thrown if something goes awry
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());
152 * Method: getComponent()
154 * @throws Exception thrown if something goes awry
156 public void testGetComponent() throws Exception {
157 assertTrue(new StreamerHandler(new BodyTube()).getComponent() instanceof Streamer);
161 * Method: getMaterialType()
163 * @throws Exception thrown if something goes awry
165 public void testGetMaterialType() throws Exception {
166 assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube()).getMaterialType());
170 * Method: endHandler()
172 * @throws Exception thrown if something goes awry
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();
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);
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);
193 handler.closeElement("Thickness", attributes, "0.02", warnings);
194 assertEquals(0.01848, handler.computeDensity(RocksimDensityType.ROCKSIM_BULK, 924d));
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());
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);
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);
215 assertEquals(1.728190092, component.getMass());