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.database.Databases;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.ExternalComponent;
13 import net.sf.openrocket.rocketcomponent.Stage;
14 import net.sf.openrocket.rocketcomponent.Transition;
16 import java.util.HashMap;
19 * TransitionHandler Tester.
21 public class TransitionHandlerTest extends RocksimTestBase {
24 * The class under test.
26 public static final Class classUT = TransitionHandler.class;
29 * The test class (this class).
31 public static final Class testClass = TransitionHandlerTest.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(TransitionHandlerTest.class);
45 * @param name the name of the test to run.
47 public TransitionHandlerTest(String name) {
54 public void setUp() throws Exception {
59 * Teardown the fixture.
61 public void tearDown() throws Exception {
68 * @throws Exception thrown if something goes awry
70 public void testConstructor() throws Exception {
73 new TransitionHandler(null);
74 fail("Should have thrown IllegalArgumentException");
76 catch (IllegalArgumentException iae) {
80 Stage stage = new Stage();
81 TransitionHandler handler = new TransitionHandler(stage);
82 Transition component = (Transition) getField(handler, "transition");
83 assertContains(component, stage.getChildren());
87 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
89 * @throws Exception thrown if something goes awry
91 public void testOpenElement() throws Exception {
92 assertEquals(PlainTextHandler.INSTANCE, new TransitionHandler(new Stage()).openElement(null, null, null));
96 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
98 * @throws Exception thrown if something goes awry
100 public void testCloseElement() throws Exception {
102 Stage stage = new Stage();
103 HashMap<String, String> attributes = new HashMap<String, String>();
104 WarningSet warnings = new WarningSet();
106 TransitionHandler handler = new TransitionHandler(stage);
107 Transition component = (Transition) getField(handler, "transition");
109 handler.closeElement("ShapeCode", attributes, "0", warnings);
110 assertEquals(Transition.Shape.CONICAL, component.getType());
111 handler.closeElement("ShapeCode", attributes, "1", warnings);
112 assertEquals(Transition.Shape.OGIVE, component.getType());
113 handler.closeElement("ShapeCode", attributes, "17", warnings);
114 assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
115 handler.closeElement("ShapeCode", attributes, "foo", warnings);
116 assertNotNull(component.getType());
117 assertEquals(1, warnings.size());
120 handler.closeElement("Len", attributes, "-1", warnings);
121 assertEquals(0d, component.getLength());
122 handler.closeElement("Len", attributes, "10", warnings);
123 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
124 handler.closeElement("Len", attributes, "10.0", warnings);
125 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
126 handler.closeElement("Len", attributes, "foo", warnings);
127 assertEquals(1, warnings.size());
130 handler.closeElement("FrontDia", attributes, "-1", warnings);
131 assertEquals(0d, component.getForeRadius());
132 handler.closeElement("FrontDia", attributes, "100", warnings);
133 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeRadius());
134 handler.closeElement("FrontDia", attributes, "foo", warnings);
135 assertEquals(1, warnings.size());
138 handler.closeElement("RearDia", attributes, "-1", warnings);
139 assertEquals(0d, component.getAftRadius());
140 handler.closeElement("RearDia", attributes, "100", warnings);
141 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius());
142 handler.closeElement("RearDia", attributes, "foo", warnings);
143 assertEquals(1, warnings.size());
146 final double aft = 100d;
147 component.setAftRadius(aft);
149 handler.closeElement("ConstructionType", attributes, "0", warnings);
150 component.setAftShoulderRadius(1.1d);
151 component.setForeShoulderRadius(1.1d);
152 handler.closeElement("WallThickness", attributes, "-1", warnings);
153 handler.endHandler("Transition", attributes, null, warnings);
154 assertEquals(component.getAftRadius(), component.getThickness());
155 assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness());
156 assertEquals(component.getForeShoulderThickness(), component.getForeShoulderThickness());
157 handler.closeElement("WallThickness", attributes, "100", warnings);
158 handler.endHandler("Transition", attributes, null, warnings);
159 assertEquals(aft, component.getThickness());
160 handler.closeElement("WallThickness", attributes, "foo", warnings);
161 handler.endHandler("Transition", attributes, null, warnings);
162 assertEquals(1, warnings.size());
165 handler.closeElement("ConstructionType", attributes, "1", warnings);
166 component.setAftShoulderRadius(1.1d);
167 component.setForeShoulderRadius(1.1d);
168 handler.closeElement("WallThickness", attributes, "-1", warnings);
169 handler.endHandler("Transition", attributes, null, warnings);
170 assertEquals(0d, component.getThickness());
171 assertEquals(0d, component.getAftShoulderThickness());
172 assertEquals(0d, component.getForeShoulderThickness());
173 handler.closeElement("WallThickness", attributes, "1.1", warnings);
174 handler.endHandler("Transition", attributes, null, warnings);
175 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness());
176 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness());
177 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderThickness());
180 handler.closeElement("FrontShoulderLen", attributes, "-1", warnings);
181 assertEquals(0d, component.getForeShoulderLength());
182 handler.closeElement("FrontShoulderLen", attributes, "10", warnings);
183 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
184 handler.closeElement("FrontShoulderLen", attributes, "10.0", warnings);
185 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
186 handler.closeElement("FrontShoulderLen", attributes, "foo", warnings);
187 assertEquals(1, warnings.size());
190 handler.closeElement("RearShoulderLen", attributes, "-1", warnings);
191 assertEquals(0d, component.getAftShoulderLength());
192 handler.closeElement("RearShoulderLen", attributes, "10", warnings);
193 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
194 handler.closeElement("RearShoulderLen", attributes, "10.0", warnings);
195 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
196 handler.closeElement("RearShoulderLen", attributes, "foo", warnings);
197 assertEquals(1, warnings.size());
200 handler.closeElement("FrontShoulderDia", attributes, "-1", warnings);
201 assertEquals(0d, component.getForeShoulderRadius());
202 handler.closeElement("FrontShoulderDia", attributes, "100", warnings);
203 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeShoulderRadius());
204 handler.closeElement("FrontShoulderDia", attributes, "foo", warnings);
205 assertEquals(1, warnings.size());
208 handler.closeElement("RearShoulderDia", attributes, "-1", warnings);
209 assertEquals(0d, component.getAftShoulderRadius());
210 handler.closeElement("RearShoulderDia", attributes, "100", warnings);
211 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius());
212 handler.closeElement("RearShoulderDia", attributes, "foo", warnings);
213 assertEquals(1, warnings.size());
216 component.setType(Transition.Shape.HAACK);
217 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
218 assertEquals(0d, component.getShapeParameter());
219 handler.closeElement("ShapeParameter", attributes, "100", warnings);
220 assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter());
221 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
222 assertEquals(1, warnings.size());
223 assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
224 warnings.iterator().next().toString());
228 component.setType(Transition.Shape.CONICAL);
229 component.setShapeParameter(0d);
230 handler.closeElement("ShapeParameter", attributes, "100", warnings);
231 assertEquals(0d, component.getShapeParameter());
233 handler.closeElement("FinishCode", attributes, "-1", warnings);
234 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
235 handler.closeElement("FinishCode", attributes, "100", warnings);
236 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
237 handler.closeElement("FinishCode", attributes, "foo", warnings);
238 assertEquals(1, warnings.size());
241 handler.closeElement("Name", attributes, "Test Name", warnings);
242 assertEquals("Test Name", component.getName());
244 handler.closeElement("Material", attributes, "Some Material", warnings);
245 handler.endHandler("Transition", attributes, null, warnings);
246 assertTrue(component.getMaterial().getName().contains("Some Material"));
250 * Method: getComponent()
252 * @throws Exception thrown if something goes awry
254 public void testGetComponent() throws Exception {
255 assertTrue(new TransitionHandler(new Stage()).getComponent() instanceof Transition);
259 * Method: getMaterialType()
261 * @throws Exception thrown if something goes awry
263 public void testGetMaterialType() throws Exception {
264 assertEquals(Material.Type.BULK, new TransitionHandler(new Stage()).getMaterialType());