2 * TransitionHandlerTest.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.ExternalComponent;
12 import net.sf.openrocket.rocketcomponent.Stage;
13 import net.sf.openrocket.rocketcomponent.Transition;
15 import java.util.HashMap;
18 * TransitionHandler Tester.
20 public class TransitionHandlerTest extends RocksimTestBase {
23 * The class under test.
25 public static final Class classUT = TransitionHandler.class;
28 * The test class (this class).
30 public static final Class testClass = TransitionHandlerTest.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(TransitionHandlerTest.class);
44 * @param name the name of the test to run.
46 public TransitionHandlerTest(String name) {
53 public void setUp() throws Exception {
58 * Teardown the fixture.
60 public void tearDown() throws Exception {
67 * @throws Exception thrown if something goes awry
69 public void testConstructor() throws Exception {
72 new TransitionHandler(null, new WarningSet());
73 fail("Should have thrown IllegalArgumentException");
75 catch (IllegalArgumentException iae) {
79 Stage stage = new Stage();
80 TransitionHandler handler = new TransitionHandler(stage, new WarningSet());
81 Transition component = (Transition) getField(handler, "transition");
82 assertContains(component, stage.getChildren());
86 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
88 * @throws Exception thrown if something goes awry
90 public void testOpenElement() throws Exception {
91 assertEquals(PlainTextHandler.INSTANCE, new TransitionHandler(new Stage(), new WarningSet()).openElement(null, null, null));
95 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
97 * @throws Exception thrown if something goes awry
99 public void testCloseElement() throws Exception {
101 Stage stage = new Stage();
102 HashMap<String, String> attributes = new HashMap<String, String>();
103 WarningSet warnings = new WarningSet();
105 TransitionHandler handler = new TransitionHandler(stage, new WarningSet());
106 Transition component = (Transition) getField(handler, "transition");
108 handler.closeElement("ShapeCode", attributes, "0", warnings);
109 assertEquals(Transition.Shape.CONICAL, component.getType());
110 handler.closeElement("ShapeCode", attributes, "1", warnings);
111 assertEquals(Transition.Shape.OGIVE, component.getType());
112 handler.closeElement("ShapeCode", attributes, "17", warnings);
113 assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
114 handler.closeElement("ShapeCode", attributes, "foo", warnings);
115 assertNotNull(component.getType());
116 assertEquals(1, warnings.size());
119 handler.closeElement("Len", attributes, "-1", warnings);
120 assertEquals(0d, component.getLength());
121 handler.closeElement("Len", attributes, "10", warnings);
122 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
123 handler.closeElement("Len", attributes, "10.0", warnings);
124 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
125 handler.closeElement("Len", attributes, "foo", warnings);
126 assertEquals(1, warnings.size());
129 handler.closeElement("FrontDia", attributes, "-1", warnings);
130 assertEquals(0d, component.getForeRadius());
131 handler.closeElement("FrontDia", attributes, "100", warnings);
132 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeRadius());
133 handler.closeElement("FrontDia", attributes, "foo", warnings);
134 assertEquals(1, warnings.size());
137 handler.closeElement("RearDia", attributes, "-1", warnings);
138 assertEquals(0d, component.getAftRadius());
139 handler.closeElement("RearDia", attributes, "100", warnings);
140 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius());
141 handler.closeElement("RearDia", attributes, "foo", warnings);
142 assertEquals(1, warnings.size());
145 final double aft = 100d;
146 component.setAftRadius(aft);
148 handler.closeElement("ConstructionType", attributes, "0", warnings);
149 component.setAftShoulderRadius(1.1d);
150 component.setForeShoulderRadius(1.1d);
151 handler.closeElement("WallThickness", attributes, "-1", warnings);
152 handler.endHandler("Transition", attributes, null, warnings);
153 assertEquals(component.getAftRadius(), component.getThickness());
154 assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness());
155 assertEquals(component.getForeShoulderThickness(), component.getForeShoulderThickness());
156 handler.closeElement("WallThickness", attributes, "100", warnings);
157 handler.endHandler("Transition", attributes, null, warnings);
158 assertEquals(aft, component.getThickness());
159 handler.closeElement("WallThickness", attributes, "foo", warnings);
160 handler.endHandler("Transition", attributes, null, warnings);
161 assertEquals(1, warnings.size());
164 handler.closeElement("ConstructionType", attributes, "1", warnings);
165 component.setAftShoulderRadius(1.1d);
166 component.setForeShoulderRadius(1.1d);
167 handler.closeElement("WallThickness", attributes, "-1", warnings);
168 handler.endHandler("Transition", attributes, null, warnings);
169 assertEquals(0d, component.getThickness());
170 assertEquals(0d, component.getAftShoulderThickness());
171 assertEquals(0d, component.getForeShoulderThickness());
172 handler.closeElement("WallThickness", attributes, "1.1", warnings);
173 handler.endHandler("Transition", attributes, null, warnings);
174 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness());
175 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness());
176 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderThickness());
179 handler.closeElement("FrontShoulderLen", attributes, "-1", warnings);
180 assertEquals(0d, component.getForeShoulderLength());
181 handler.closeElement("FrontShoulderLen", attributes, "10", warnings);
182 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
183 handler.closeElement("FrontShoulderLen", attributes, "10.0", warnings);
184 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
185 handler.closeElement("FrontShoulderLen", attributes, "foo", warnings);
186 assertEquals(1, warnings.size());
189 handler.closeElement("RearShoulderLen", attributes, "-1", warnings);
190 assertEquals(0d, component.getAftShoulderLength());
191 handler.closeElement("RearShoulderLen", attributes, "10", warnings);
192 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
193 handler.closeElement("RearShoulderLen", attributes, "10.0", warnings);
194 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
195 handler.closeElement("RearShoulderLen", attributes, "foo", warnings);
196 assertEquals(1, warnings.size());
199 handler.closeElement("FrontShoulderDia", attributes, "-1", warnings);
200 assertEquals(0d, component.getForeShoulderRadius());
201 handler.closeElement("FrontShoulderDia", attributes, "100", warnings);
202 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeShoulderRadius());
203 handler.closeElement("FrontShoulderDia", attributes, "foo", warnings);
204 assertEquals(1, warnings.size());
207 handler.closeElement("RearShoulderDia", attributes, "-1", warnings);
208 assertEquals(0d, component.getAftShoulderRadius());
209 handler.closeElement("RearShoulderDia", attributes, "100", warnings);
210 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius());
211 handler.closeElement("RearShoulderDia", attributes, "foo", warnings);
212 assertEquals(1, warnings.size());
215 component.setType(Transition.Shape.HAACK);
216 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
217 assertEquals(0d, component.getShapeParameter());
218 handler.closeElement("ShapeParameter", attributes, "100", warnings);
219 assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter());
220 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
221 assertEquals(1, warnings.size());
222 assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
223 warnings.iterator().next().toString());
227 component.setType(Transition.Shape.CONICAL);
228 component.setShapeParameter(0d);
229 handler.closeElement("ShapeParameter", attributes, "100", warnings);
230 assertEquals(0d, component.getShapeParameter());
232 handler.closeElement("FinishCode", attributes, "-1", warnings);
233 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
234 handler.closeElement("FinishCode", attributes, "100", warnings);
235 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
236 handler.closeElement("FinishCode", attributes, "foo", warnings);
237 assertEquals(1, warnings.size());
240 handler.closeElement("Name", attributes, "Test Name", warnings);
241 assertEquals("Test Name", component.getName());
243 handler.closeElement("Material", attributes, "Some Material", warnings);
244 handler.endHandler("Transition", attributes, null, warnings);
245 assertTrue(component.getMaterial().getName().contains("Some Material"));
249 * Method: getComponent()
251 * @throws Exception thrown if something goes awry
253 public void testGetComponent() throws Exception {
254 assertTrue(new TransitionHandler(new Stage(), new WarningSet()).getComponent() instanceof Transition);
258 * Method: getMaterialType()
260 * @throws Exception thrown if something goes awry
262 public void testGetMaterialType() throws Exception {
263 assertEquals(Material.Type.BULK, new TransitionHandler(new Stage(), new WarningSet()).getMaterialType());