2 * TransitionHandlerTest.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.RocksimNoseConeCode;
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;
14 import org.junit.Assert;
16 import java.util.HashMap;
19 * TransitionHandler Tester.
21 public class TransitionHandlerTest extends RocksimTestBase {
26 * @throws Exception thrown if something goes awry
29 public void testConstructor() throws Exception {
32 new TransitionHandler(null, new WarningSet());
33 Assert.fail("Should have thrown IllegalArgumentException");
35 catch (IllegalArgumentException iae) {
39 Stage stage = new Stage();
40 TransitionHandler handler = new TransitionHandler(stage, new WarningSet());
41 Transition component = (Transition) getField(handler, "transition");
42 assertContains(component, stage.getChildren());
46 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
48 * @throws Exception thrown if something goes awry
51 public void testOpenElement() throws Exception {
52 Assert.assertEquals(PlainTextHandler.INSTANCE, new TransitionHandler(new Stage(), new WarningSet()).openElement(null, null, null));
56 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
58 * @throws Exception thrown if something goes awry
61 public void testCloseElement() throws Exception {
63 Stage stage = new Stage();
64 HashMap<String, String> attributes = new HashMap<String, String>();
65 WarningSet warnings = new WarningSet();
67 TransitionHandler handler = new TransitionHandler(stage, new WarningSet());
68 Transition component = (Transition) getField(handler, "transition");
70 handler.closeElement("ShapeCode", attributes, "0", warnings);
71 Assert.assertEquals(Transition.Shape.CONICAL, component.getType());
72 handler.closeElement("ShapeCode", attributes, "1", warnings);
73 Assert.assertEquals(Transition.Shape.OGIVE, component.getType());
74 handler.closeElement("ShapeCode", attributes, "17", warnings);
75 Assert.assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
76 handler.closeElement("ShapeCode", attributes, "foo", warnings);
77 Assert.assertNotNull(component.getType());
78 Assert.assertEquals(1, warnings.size());
81 handler.closeElement("Len", attributes, "-1", warnings);
82 Assert.assertEquals(0d, component.getLength(), 0.001);
83 handler.closeElement("Len", attributes, "10", warnings);
84 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
85 handler.closeElement("Len", attributes, "10.0", warnings);
86 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
87 handler.closeElement("Len", attributes, "foo", warnings);
88 Assert.assertEquals(1, warnings.size());
91 handler.closeElement("FrontDia", attributes, "-1", warnings);
92 Assert.assertEquals(0d, component.getForeRadius(), 0.001);
93 handler.closeElement("FrontDia", attributes, "100", warnings);
94 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeRadius(), 0.001);
95 handler.closeElement("FrontDia", attributes, "foo", warnings);
96 Assert.assertEquals(1, warnings.size());
99 handler.closeElement("RearDia", attributes, "-1", warnings);
100 Assert.assertEquals(0d, component.getAftRadius(), 0.001);
101 handler.closeElement("RearDia", attributes, "100", warnings);
102 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius(), 0.001);
103 handler.closeElement("RearDia", attributes, "foo", warnings);
104 Assert.assertEquals(1, warnings.size());
107 final double aft = 100d;
108 component.setAftRadius(aft);
110 handler.closeElement("ConstructionType", attributes, "0", warnings);
111 component.setAftShoulderRadius(1.1d);
112 component.setForeShoulderRadius(1.1d);
113 handler.closeElement("WallThickness", attributes, "-1", warnings);
114 handler.endHandler("Transition", attributes, null, warnings);
115 Assert.assertEquals(component.getAftRadius(), component.getThickness(), 0.001);
116 Assert.assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness(), 0.001);
117 Assert.assertEquals(component.getForeShoulderThickness(), component.getForeShoulderThickness(), 0.001);
118 handler.closeElement("WallThickness", attributes, "100", warnings);
119 handler.endHandler("Transition", attributes, null, warnings);
120 Assert.assertEquals(aft, component.getThickness(), 0.001);
121 handler.closeElement("WallThickness", attributes, "foo", warnings);
122 handler.endHandler("Transition", attributes, null, warnings);
123 Assert.assertEquals(1, warnings.size());
126 handler.closeElement("ConstructionType", attributes, "1", warnings);
127 component.setAftShoulderRadius(1.1d);
128 component.setForeShoulderRadius(1.1d);
129 handler.closeElement("WallThickness", attributes, "-1", warnings);
130 handler.endHandler("Transition", attributes, null, warnings);
131 Assert.assertEquals(0d, component.getThickness(), 0.001);
132 Assert.assertEquals(0d, component.getAftShoulderThickness(), 0.001);
133 Assert.assertEquals(0d, component.getForeShoulderThickness(), 0.001);
134 handler.closeElement("WallThickness", attributes, "1.1", warnings);
135 handler.endHandler("Transition", attributes, null, warnings);
136 Assert.assertEquals(1.1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness(), 0.001);
137 Assert.assertEquals(1.1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness(), 0.001);
138 Assert.assertEquals(1.1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderThickness(), 0.001);
141 handler.closeElement("FrontShoulderLen", attributes, "-1", warnings);
142 Assert.assertEquals(0d, component.getForeShoulderLength(), 0.001);
143 handler.closeElement("FrontShoulderLen", attributes, "10", warnings);
144 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength(), 0.001);
145 handler.closeElement("FrontShoulderLen", attributes, "10.0", warnings);
146 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength(), 0.001);
147 handler.closeElement("FrontShoulderLen", attributes, "foo", warnings);
148 Assert.assertEquals(1, warnings.size());
151 handler.closeElement("RearShoulderLen", attributes, "-1", warnings);
152 Assert.assertEquals(0d, component.getAftShoulderLength(), 0.001);
153 handler.closeElement("RearShoulderLen", attributes, "10", warnings);
154 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
155 handler.closeElement("RearShoulderLen", attributes, "10.0", warnings);
156 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
157 handler.closeElement("RearShoulderLen", attributes, "foo", warnings);
158 Assert.assertEquals(1, warnings.size());
161 handler.closeElement("FrontShoulderDia", attributes, "-1", warnings);
162 Assert.assertEquals(0d, component.getForeShoulderRadius(), 0.001);
163 handler.closeElement("FrontShoulderDia", attributes, "100", warnings);
164 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeShoulderRadius(), 0.001);
165 handler.closeElement("FrontShoulderDia", attributes, "foo", warnings);
166 Assert.assertEquals(1, warnings.size());
169 handler.closeElement("RearShoulderDia", attributes, "-1", warnings);
170 Assert.assertEquals(0d, component.getAftShoulderRadius(), 0.001);
171 handler.closeElement("RearShoulderDia", attributes, "100", warnings);
172 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius(), 0.001);
173 handler.closeElement("RearShoulderDia", attributes, "foo", warnings);
174 Assert.assertEquals(1, warnings.size());
177 component.setType(Transition.Shape.HAACK);
178 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
179 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
180 handler.closeElement("ShapeParameter", attributes, "100", warnings);
181 Assert.assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter(), 0.001);
182 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
183 Assert.assertEquals(1, warnings.size());
184 Assert.assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
185 warnings.iterator().next().toString());
189 component.setType(Transition.Shape.CONICAL);
190 component.setShapeParameter(0d);
191 handler.closeElement("ShapeParameter", attributes, "100", warnings);
192 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
194 handler.closeElement("FinishCode", attributes, "-1", warnings);
195 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
196 handler.closeElement("FinishCode", attributes, "100", warnings);
197 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
198 handler.closeElement("FinishCode", attributes, "foo", warnings);
199 Assert.assertEquals(1, warnings.size());
202 handler.closeElement("Name", attributes, "Test Name", warnings);
203 Assert.assertEquals("Test Name", component.getName());
205 handler.closeElement("Material", attributes, "Some Material", warnings);
206 handler.endHandler("Transition", attributes, null, warnings);
207 Assert.assertTrue(component.getMaterial().getName().contains("Some Material"));
211 * Method: getComponent()
213 * @throws Exception thrown if something goes awry
216 public void testGetComponent() throws Exception {
217 Assert.assertTrue(new TransitionHandler(new Stage(), new WarningSet()).getComponent() instanceof Transition);
221 * Method: getMaterialType()
223 * @throws Exception thrown if something goes awry
226 public void testGetMaterialType() throws Exception {
227 Assert.assertEquals(Material.Type.BULK, new TransitionHandler(new Stage(), new WarningSet()).getMaterialType());