2 * NoseConeHandlerTest.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.NoseCone;
13 import net.sf.openrocket.rocketcomponent.Stage;
14 import net.sf.openrocket.rocketcomponent.Transition;
15 import org.junit.Assert;
16 import org.junit.Test;
18 import java.util.HashMap;
21 * NoseConeHandler Tester.
24 public class NoseConeHandlerTest extends RocksimTestBase {
29 * @throws Exception thrown if something goes awry
32 public void testConstructor() throws Exception {
35 new NoseConeHandler(null, new WarningSet());
36 Assert.fail("Should have thrown IllegalArgumentException");
38 catch (IllegalArgumentException iae) {
42 Stage stage = new Stage();
43 NoseConeHandler handler = new NoseConeHandler(stage, new WarningSet());
44 NoseCone component = (NoseCone) getField(handler, "noseCone");
45 assertContains(component, stage.getChildren());
49 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
51 * @throws Exception thrown if something goes awry
54 public void testOpenElement() throws Exception {
55 Assert.assertEquals(PlainTextHandler.INSTANCE, new NoseConeHandler(new Stage(), new WarningSet()).openElement(null, null, null));
56 Assert.assertNotNull(new NoseConeHandler(new Stage(), new WarningSet()).openElement("AttachedParts", null, null));
61 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
63 * @throws Exception thrown if something goes awry
66 public void testCloseElement() throws Exception {
68 Stage stage = new Stage();
69 HashMap<String, String> attributes = new HashMap<String, String>();
70 WarningSet warnings = new WarningSet();
72 NoseConeHandler handler = new NoseConeHandler(stage, warnings);
73 NoseCone component = (NoseCone) getField(handler, "noseCone");
75 handler.closeElement("ShapeCode", attributes, "0", warnings);
76 Assert.assertEquals(Transition.Shape.CONICAL, component.getType());
77 handler.closeElement("ShapeCode", attributes, "1", warnings);
78 Assert.assertEquals(Transition.Shape.OGIVE, component.getType());
79 handler.closeElement("ShapeCode", attributes, "17", warnings);
80 Assert.assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
81 handler.closeElement("ShapeCode", attributes, "foo", warnings);
82 Assert.assertNotNull(component.getType());
83 Assert.assertEquals(1, warnings.size());
86 handler.closeElement("Len", attributes, "-1", warnings);
87 Assert.assertEquals(0d, component.getLength(), 0.001);
88 handler.closeElement("Len", attributes, "10", warnings);
89 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
90 handler.closeElement("Len", attributes, "10.0", warnings);
91 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
92 handler.closeElement("Len", attributes, "foo", warnings);
93 Assert.assertEquals(1, warnings.size());
96 handler.closeElement("BaseDia", attributes, "-1", warnings);
97 Assert.assertEquals(0d, component.getAftRadius(), 0.001);
98 handler.closeElement("BaseDia", attributes, "100", warnings);
99 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius(), 0.001);
100 handler.closeElement("BaseDia", attributes, "foo", warnings);
101 Assert.assertEquals(1, warnings.size());
105 final double aft = 100d;
106 component.setAftRadius(aft);
108 handler.closeElement("ConstructionType", attributes, "0", warnings);
109 component.setAftShoulderRadius(1.1d);
110 handler.closeElement("WallThickness", attributes, "-1", warnings);
111 handler.endHandler("Transition", attributes, null, warnings);
112 Assert.assertEquals(component.getAftRadius(), component.getThickness(), 0.001);
113 Assert.assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness(), 0.001);
114 handler.closeElement("WallThickness", attributes, "100", warnings);
115 handler.endHandler("Transition", attributes, null, warnings);
116 Assert.assertEquals(aft, component.getThickness(), 0.001);
117 handler.closeElement("WallThickness", attributes, "foo", warnings);
118 handler.endHandler("Transition", attributes, null, warnings);
119 Assert.assertEquals(1, warnings.size());
122 handler.closeElement("ConstructionType", attributes, "1", warnings);
123 component.setAftShoulderRadius(1.1d);
124 handler.closeElement("WallThickness", attributes, "-1", warnings);
125 handler.endHandler("Transition", attributes, null, warnings);
126 Assert.assertEquals(0d, component.getThickness(), 0.001);
127 Assert.assertEquals(0d, component.getAftShoulderThickness(), 0.001);
128 handler.closeElement("WallThickness", attributes, "1.1", warnings);
129 handler.endHandler("Transition", attributes, null, warnings);
130 Assert.assertEquals(1.1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness(), 0.001);
131 Assert.assertEquals(1.1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness(), 0.001);
133 handler.closeElement("ShoulderLen", attributes, "-1", warnings);
134 Assert.assertEquals(0d, component.getAftShoulderLength(), 0.001);
135 handler.closeElement("ShoulderLen", attributes, "10", warnings);
136 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
137 handler.closeElement("ShoulderLen", attributes, "10.0", warnings);
138 Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
139 handler.closeElement("ShoulderLen", attributes, "foo", warnings);
140 Assert.assertEquals(1, warnings.size());
143 handler.closeElement("ShoulderOD", attributes, "-1", warnings);
144 Assert.assertEquals(0d, component.getAftShoulderRadius(), 0.001);
145 handler.closeElement("ShoulderOD", attributes, "100", warnings);
146 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius(), 0.001);
147 handler.closeElement("ShoulderOD", attributes, "foo", warnings);
148 Assert.assertEquals(1, warnings.size());
151 component.setType(Transition.Shape.HAACK);
152 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
153 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
154 handler.closeElement("ShapeParameter", attributes, "100", warnings);
155 Assert.assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter(), 0.001);
156 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
157 Assert.assertEquals(1, warnings.size());
158 Assert.assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
159 warnings.iterator().next().toString());
163 component.setType(Transition.Shape.CONICAL);
164 component.setShapeParameter(0d);
165 handler.closeElement("ShapeParameter", attributes, "100", warnings);
166 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
168 handler.closeElement("FinishCode", attributes, "-1", warnings);
169 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
170 handler.closeElement("FinishCode", attributes, "100", warnings);
171 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
172 handler.closeElement("FinishCode", attributes, "foo", warnings);
173 Assert.assertEquals(1, warnings.size());
176 handler.closeElement("Name", attributes, "Test Name", warnings);
177 Assert.assertEquals("Test Name", component.getName());
179 handler.closeElement("Material", attributes, "Some Material", warnings);
180 handler.endHandler("NoseCone", attributes, null, warnings);
181 Assert.assertTrue(component.getMaterial().getName().contains("Some Material"));
185 * Method: getComponent()
187 * @throws Exception thrown if something goes awry
190 public void testGetComponent() throws Exception {
191 Assert.assertTrue(new NoseConeHandler(new Stage(), new WarningSet()).getComponent() instanceof NoseCone);
195 * Method: getMaterialType()
197 * @throws Exception thrown if something goes awry
200 public void testGetMaterialType() throws Exception {
201 Assert.assertEquals(Material.Type.BULK, new NoseConeHandler(new Stage(), new WarningSet()).getMaterialType());