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.database.Databases;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import net.sf.openrocket.rocketcomponent.Streamer;
16 import java.util.HashMap;
19 * StreamerHandler Tester.
21 public class StreamerHandlerTest extends BaseRocksimTest {
24 * The class under test.
26 public static final Class classUT = StreamerHandler.class;
29 * The test class (this class).
31 public static final Class testClass = StreamerHandlerTest.class;
34 * Create a test suite of all tests within this test class.
36 * @return a suite of tests
38 public static Test suite() {
39 return new TestSuite(StreamerHandlerTest.class);
45 * @param name the name of the test to run.
47 public StreamerHandlerTest(String name) {
54 public void setUp() throws Exception {
59 * Teardown the fixture.
61 public void tearDown() throws Exception {
66 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
68 * @throws Exception thrown if something goes awry
70 public void testOpenElement() throws Exception {
71 assertEquals(PlainTextHandler.INSTANCE, new StreamerHandler(new BodyTube()).openElement(null, null, null));
75 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
77 * @throws Exception thrown if something goes awry
79 public void testCloseElement() throws Exception {
81 BodyTube tube = new BodyTube();
82 StreamerHandler handler = new StreamerHandler(tube);
83 Streamer component = (Streamer) getField(handler, "streamer");
84 HashMap<String, String> attributes = new HashMap<String, String>();
85 WarningSet warnings = new WarningSet();
87 handler.closeElement("Width", attributes, "0", warnings);
88 assertEquals(0d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
89 handler.closeElement("Width", attributes, "10", warnings);
90 assertEquals(10d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
91 handler.closeElement("Width", attributes, "foo", warnings);
92 assertEquals(1, warnings.size());
95 handler.closeElement("Len", attributes, "-1", warnings);
96 assertEquals(0d, component.getStripLength());
97 handler.closeElement("Len", attributes, "10", warnings);
98 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
99 handler.closeElement("Len", attributes, "10.0", warnings);
100 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
101 handler.closeElement("Len", attributes, "foo", warnings);
102 assertEquals(1, warnings.size());
105 handler.closeElement("Name", attributes, "Test Name", warnings);
106 assertEquals("Test Name", component.getName());
108 handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
109 assertEquals(0.94d, component.getCD());
110 handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
111 assertEquals(-0.94d, component.getCD());
112 handler.closeElement("DragCoefficient", attributes, "foo", warnings);
113 assertEquals(1, warnings.size());
119 * Method: constructor
121 * @throws Exception thrown if something goes awry
123 public void testConstructor() throws Exception {
126 new StreamerHandler(null);
127 fail("Should have thrown IllegalArgumentException");
129 catch (IllegalArgumentException iae) {
133 BodyTube tube = new BodyTube();
134 StreamerHandler handler = new StreamerHandler(tube);
135 Streamer component = (Streamer) getField(handler, "streamer");
136 assertContains(component, tube.getChildren());
140 * Method: setRelativePosition(RocketComponent.Position position)
142 * @throws Exception thrown if something goes awry
144 public void testSetRelativePosition() throws Exception {
145 BodyTube tube = new BodyTube();
146 StreamerHandler handler = new StreamerHandler(tube);
147 Streamer component = (Streamer) getField(handler, "streamer");
148 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
149 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
153 * Method: getComponent()
155 * @throws Exception thrown if something goes awry
157 public void testGetComponent() throws Exception {
158 assertTrue(new StreamerHandler(new BodyTube()).getComponent() instanceof Streamer);
162 * Method: getMaterialType()
164 * @throws Exception thrown if something goes awry
166 public void testGetMaterialType() throws Exception {
167 assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube()).getMaterialType());
171 * Method: endHandler()
173 * @throws Exception thrown if something goes awry
175 public void testEndHandler() throws Exception {
176 BodyTube tube = new BodyTube();
177 StreamerHandler handler = new StreamerHandler(tube);
178 Streamer component = (Streamer) getField(handler, "streamer");
179 HashMap<String, String> attributes = new HashMap<String, String>();
180 WarningSet warnings = new WarningSet();
182 handler.closeElement("Xb", attributes, "-10", warnings);
183 handler.closeElement("LocationMode", attributes, "1", warnings);
184 handler.endHandler("Streamer", attributes, null, warnings);
185 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
186 assertEquals(component.getPositionValue(), -10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
188 handler.closeElement("Xb", attributes, "-10", warnings);
189 handler.closeElement("LocationMode", attributes, "2", warnings);
190 handler.endHandler("Streamer", attributes, null, warnings);
191 assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
192 assertEquals(component.getPositionValue(), 10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);