2 * StreamerHandlerTest.java
4 package net.sf.openrocket.file.rocksim;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.simplesax.PlainTextHandler;
8 import net.sf.openrocket.material.Material;
9 import net.sf.openrocket.rocketcomponent.BodyTube;
10 import net.sf.openrocket.rocketcomponent.RocketComponent;
11 import net.sf.openrocket.rocketcomponent.Streamer;
12 import org.junit.Assert;
13 import org.junit.Test;
15 import java.util.HashMap;
18 * StreamerHandler Tester.
20 public class StreamerHandlerTest 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 StreamerHandler(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 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
42 Streamer component = (Streamer) getField(handler, "streamer");
43 HashMap<String, String> attributes = new HashMap<String, String>();
44 WarningSet warnings = new WarningSet();
46 handler.closeElement("Width", attributes, "0", warnings);
47 Assert.assertEquals(0d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
48 handler.closeElement("Width", attributes, "10", warnings);
49 Assert.assertEquals(10d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
50 handler.closeElement("Width", attributes, "foo", warnings);
51 Assert.assertEquals(1, warnings.size());
54 handler.closeElement("Len", attributes, "-1", warnings);
55 Assert.assertEquals(0d, component.getStripLength(), 0.001);
56 handler.closeElement("Len", attributes, "10", warnings);
57 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
58 handler.closeElement("Len", attributes, "10.0", warnings);
59 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
60 handler.closeElement("Len", attributes, "foo", warnings);
61 Assert.assertEquals(1, warnings.size());
64 handler.closeElement("Name", attributes, "Test Name", warnings);
65 Assert.assertEquals("Test Name", component.getName());
67 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
68 Assert.assertEquals(0.94d, component.getCD(), 0.001);
69 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
70 Assert.assertEquals(-0.94d, component.getCD(), 0.001);
71 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
72 Assert.assertEquals(1, warnings.size());
80 * @throws Exception thrown if something goes awry
83 public void testConstructor() throws Exception {
86 new StreamerHandler(null, new WarningSet());
87 Assert.fail("Should have thrown IllegalArgumentException");
89 catch (IllegalArgumentException iae) {
93 BodyTube tube = new BodyTube();
94 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
95 Streamer component = (Streamer) getField(handler, "streamer");
96 assertContains(component, tube.getChildren());
100 * Method: setRelativePosition(RocketComponent.Position position)
102 * @throws Exception thrown if something goes awry
105 public void testSetRelativePosition() throws Exception {
106 BodyTube tube = new BodyTube();
107 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
108 Streamer component = (Streamer) getField(handler, "streamer");
109 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
110 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
114 * Method: getComponent()
116 * @throws Exception thrown if something goes awry
119 public void testGetComponent() throws Exception {
120 Assert.assertTrue(new StreamerHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Streamer);
124 * Method: getMaterialType()
126 * @throws Exception thrown if something goes awry
129 public void testGetMaterialType() throws Exception {
130 Assert.assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube(), new WarningSet()).getMaterialType());
134 * Method: endHandler()
136 * @throws Exception thrown if something goes awry
139 public void testEndHandler() throws Exception {
140 BodyTube tube = new BodyTube();
141 StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
142 Streamer component = (Streamer) getField(handler, "streamer");
143 HashMap<String, String> attributes = new HashMap<String, String>();
144 WarningSet warnings = new WarningSet();
146 handler.closeElement("Xb", attributes, "-10", warnings);
147 handler.closeElement("LocationMode", attributes, "1", warnings);
148 handler.endHandler("Streamer", attributes, null, warnings);
149 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
150 Assert.assertEquals(component.getPositionValue(), -10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
152 handler.closeElement("Xb", attributes, "-10", warnings);
153 handler.closeElement("LocationMode", attributes, "2", warnings);
154 handler.endHandler("Streamer", attributes, null, warnings);
155 Assert.assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
156 Assert.assertEquals(component.getPositionValue(), 10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
158 handler.closeElement("Thickness", attributes, "0.02", warnings);
159 Assert.assertEquals(0.01848, handler.computeDensity(RocksimDensityType.ROCKSIM_BULK, 924d), 0.001);
161 //Test Density Type 0 (Bulk)
162 handler.closeElement("Density", attributes, "924.0", warnings);
163 handler.closeElement("DensityType", attributes, "0", warnings);
164 handler.endHandler("Streamer", attributes, null, warnings);
165 Assert.assertEquals(0.01848d, component.getMaterial().getDensity(), 0.001);
167 //Test Density Type 1 (Surface)
168 handler.closeElement("Density", attributes, "0.006685", warnings);
169 handler.closeElement("DensityType", attributes, "1", warnings);
170 handler.endHandler("Streamer", attributes, null, warnings);
171 Assert.assertTrue(Math.abs(0.06685d - component.getMaterial().getDensity()) < 0.00001);
173 //Test Density Type 2 (Line)
174 handler.closeElement("Density", attributes, "0.223225", warnings);
175 handler.closeElement("DensityType", attributes, "2", warnings);
176 handler.closeElement("Len", attributes, "3810.", warnings);
177 handler.closeElement("Width", attributes, "203.2", warnings);
178 handler.endHandler("Streamer", attributes, null, warnings);
180 Assert.assertEquals(1.728190092, component.getMass(), 0.001);