2 * StreamerHandlerTest.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.rocksim.RocksimDensityType;
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 import org.junit.Assert;
15 import org.junit.Test;
17 import java.util.HashMap;
20 * StreamerHandler Tester.
22 public class StreamerHandlerTest extends RocksimTestBase {
25 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
27 * @throws Exception thrown if something goes awry
30 public void testOpenElement() throws Exception {
31 Assert.assertEquals(PlainTextHandler.INSTANCE, new StreamerHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
35 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
37 * @throws Exception thrown if something goes awry
40 public void testCloseElement() throws Exception {
42 BodyTube tube = new BodyTube();
43 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
44 Streamer component = (Streamer) getField(handler, "streamer");
45 HashMap<String, String> attributes = new HashMap<String, String>();
46 WarningSet warnings = new WarningSet();
48 handler.closeElement("Width", attributes, "0", warnings);
49 Assert.assertEquals(0d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
50 handler.closeElement("Width", attributes, "10", warnings);
51 Assert.assertEquals(10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
52 handler.closeElement("Width", attributes, "foo", warnings);
53 Assert.assertEquals(1, warnings.size());
56 handler.closeElement("Len", attributes, "-1", warnings);
57 Assert.assertEquals(0d, component.getStripLength(), 0.001);
58 handler.closeElement("Len", attributes, "10", warnings);
59 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
60 handler.closeElement("Len", attributes, "10.0", warnings);
61 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
62 handler.closeElement("Len", attributes, "foo", warnings);
63 Assert.assertEquals(1, warnings.size());
66 handler.closeElement("Name", attributes, "Test Name", warnings);
67 Assert.assertEquals("Test Name", component.getName());
69 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
70 Assert.assertEquals(0.94d, component.getCD(), 0.001);
71 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
72 Assert.assertEquals(-0.94d, component.getCD(), 0.001);
73 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
74 Assert.assertEquals(1, warnings.size());
82 * @throws Exception thrown if something goes awry
85 public void testConstructor() throws Exception {
88 new StreamerHandler(null, new WarningSet());
89 Assert.fail("Should have thrown IllegalArgumentException");
91 catch (IllegalArgumentException iae) {
95 BodyTube tube = new BodyTube();
96 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
97 Streamer component = (Streamer) getField(handler, "streamer");
98 assertContains(component, tube.getChildren());
102 * Method: setRelativePosition(RocketComponent.Position position)
104 * @throws Exception thrown if something goes awry
107 public void testSetRelativePosition() throws Exception {
108 BodyTube tube = new BodyTube();
109 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
110 Streamer component = (Streamer) getField(handler, "streamer");
111 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
112 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
116 * Method: getComponent()
118 * @throws Exception thrown if something goes awry
121 public void testGetComponent() throws Exception {
122 Assert.assertTrue(new StreamerHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Streamer);
126 * Method: getMaterialType()
128 * @throws Exception thrown if something goes awry
131 public void testGetMaterialType() throws Exception {
132 Assert.assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube(), new WarningSet()).getMaterialType());
136 * Method: endHandler()
138 * @throws Exception thrown if something goes awry
141 public void testEndHandler() throws Exception {
142 BodyTube tube = new BodyTube();
143 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
144 Streamer component = (Streamer) getField(handler, "streamer");
145 HashMap<String, String> attributes = new HashMap<String, String>();
146 WarningSet warnings = new WarningSet();
148 handler.closeElement("Xb", attributes, "-10", warnings);
149 handler.closeElement("LocationMode", attributes, "1", warnings);
150 handler.endHandler("Streamer", attributes, null, warnings);
151 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
152 Assert.assertEquals(component.getPositionValue(), -10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
154 handler.closeElement("Xb", attributes, "-10", warnings);
155 handler.closeElement("LocationMode", attributes, "2", warnings);
156 handler.endHandler("Streamer", attributes, null, warnings);
157 Assert.assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
158 Assert.assertEquals(component.getPositionValue(), 10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
160 handler.closeElement("Thickness", attributes, "0.02", warnings);
161 Assert.assertEquals(0.01848, handler.computeDensity(RocksimDensityType.ROCKSIM_BULK, 924d), 0.001);
163 //Test Density Type 0 (Bulk)
164 handler.closeElement("Density", attributes, "924.0", warnings);
165 handler.closeElement("DensityType", attributes, "0", warnings);
166 handler.endHandler("Streamer", attributes, null, warnings);
167 Assert.assertEquals(0.01848d, component.getMaterial().getDensity(), 0.001);
169 //Test Density Type 1 (Surface)
170 handler.closeElement("Density", attributes, "0.006685", warnings);
171 handler.closeElement("DensityType", attributes, "1", warnings);
172 handler.endHandler("Streamer", attributes, null, warnings);
173 Assert.assertTrue(Math.abs(0.06685d - component.getMaterial().getDensity()) < 0.00001);
175 //Test Density Type 2 (Line)
176 handler.closeElement("Density", attributes, "0.223225", warnings);
177 handler.closeElement("DensityType", attributes, "2", warnings);
178 handler.closeElement("Len", attributes, "3810.", warnings);
179 handler.closeElement("Width", attributes, "203.2", warnings);
180 handler.endHandler("Streamer", attributes, null, warnings);
182 Assert.assertEquals(1.728190092, component.getMass(), 0.001);