2 * NoseConeHandlerTest.java
4 package net.sf.openrocket.file.rocksim;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.simplesax.PlainTextHandler;
8 import net.sf.openrocket.material.Material;
9 import net.sf.openrocket.rocketcomponent.ExternalComponent;
10 import net.sf.openrocket.rocketcomponent.NoseCone;
11 import net.sf.openrocket.rocketcomponent.Stage;
12 import net.sf.openrocket.rocketcomponent.Transition;
13 import org.junit.Assert;
14 import org.junit.Test;
16 import java.util.HashMap;
19 * NoseConeHandler Tester.
22 public class NoseConeHandlerTest extends RocksimTestBase {
27 * @throws Exception thrown if something goes awry
30 public void testConstructor() throws Exception {
33 new NoseConeHandler(null, new WarningSet());
34 Assert.fail("Should have thrown IllegalArgumentException");
36 catch (IllegalArgumentException iae) {
40 Stage stage = new Stage();
41 NoseConeHandler handler = new NoseConeHandler(stage, new WarningSet());
42 NoseCone component = (NoseCone) getField(handler, "noseCone");
43 assertContains(component, stage.getChildren());
47 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
49 * @throws Exception thrown if something goes awry
52 public void testOpenElement() throws Exception {
53 Assert.assertEquals(PlainTextHandler.INSTANCE, new NoseConeHandler(new Stage(), new WarningSet()).openElement(null, null, null));
54 Assert.assertNotNull(new NoseConeHandler(new Stage(), new WarningSet()).openElement("AttachedParts", null, null));
59 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
61 * @throws Exception thrown if something goes awry
64 public void testCloseElement() throws Exception {
66 Stage stage = new Stage();
67 HashMap<String, String> attributes = new HashMap<String, String>();
68 WarningSet warnings = new WarningSet();
70 NoseConeHandler handler = new NoseConeHandler(stage, warnings);
71 NoseCone component = (NoseCone) getField(handler, "noseCone");
73 handler.closeElement("ShapeCode", attributes, "0", warnings);
74 Assert.assertEquals(Transition.Shape.CONICAL, component.getType());
75 handler.closeElement("ShapeCode", attributes, "1", warnings);
76 Assert.assertEquals(Transition.Shape.OGIVE, component.getType());
77 handler.closeElement("ShapeCode", attributes, "17", warnings);
78 Assert.assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
79 handler.closeElement("ShapeCode", attributes, "foo", warnings);
80 Assert.assertNotNull(component.getType());
81 Assert.assertEquals(1, warnings.size());
84 handler.closeElement("Len", attributes, "-1", warnings);
85 Assert.assertEquals(0d, component.getLength(), 0.001);
86 handler.closeElement("Len", attributes, "10", warnings);
87 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
88 handler.closeElement("Len", attributes, "10.0", warnings);
89 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
90 handler.closeElement("Len", attributes, "foo", warnings);
91 Assert.assertEquals(1, warnings.size());
94 handler.closeElement("BaseDia", attributes, "-1", warnings);
95 Assert.assertEquals(0d, component.getAftRadius(), 0.001);
96 handler.closeElement("BaseDia", attributes, "100", warnings);
97 Assert.assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius(), 0.001);
98 handler.closeElement("BaseDia", attributes, "foo", warnings);
99 Assert.assertEquals(1, warnings.size());
103 final double aft = 100d;
104 component.setAftRadius(aft);
106 handler.closeElement("ConstructionType", attributes, "0", warnings);
107 component.setAftShoulderRadius(1.1d);
108 handler.closeElement("WallThickness", attributes, "-1", warnings);
109 handler.endHandler("Transition", attributes, null, warnings);
110 Assert.assertEquals(component.getAftRadius(), component.getThickness(), 0.001);
111 Assert.assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness(), 0.001);
112 handler.closeElement("WallThickness", attributes, "100", warnings);
113 handler.endHandler("Transition", attributes, null, warnings);
114 Assert.assertEquals(aft, component.getThickness(), 0.001);
115 handler.closeElement("WallThickness", attributes, "foo", warnings);
116 handler.endHandler("Transition", attributes, null, warnings);
117 Assert.assertEquals(1, warnings.size());
120 handler.closeElement("ConstructionType", attributes, "1", warnings);
121 component.setAftShoulderRadius(1.1d);
122 handler.closeElement("WallThickness", attributes, "-1", warnings);
123 handler.endHandler("Transition", attributes, null, warnings);
124 Assert.assertEquals(0d, component.getThickness(), 0.001);
125 Assert.assertEquals(0d, component.getAftShoulderThickness(), 0.001);
126 handler.closeElement("WallThickness", attributes, "1.1", warnings);
127 handler.endHandler("Transition", attributes, null, warnings);
128 Assert.assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness(), 0.001);
129 Assert.assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness(), 0.001);
131 handler.closeElement("ShoulderLen", attributes, "-1", warnings);
132 Assert.assertEquals(0d, component.getAftShoulderLength(), 0.001);
133 handler.closeElement("ShoulderLen", attributes, "10", warnings);
134 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
135 handler.closeElement("ShoulderLen", attributes, "10.0", warnings);
136 Assert.assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength(), 0.001);
137 handler.closeElement("ShoulderLen", attributes, "foo", warnings);
138 Assert.assertEquals(1, warnings.size());
141 handler.closeElement("ShoulderOD", attributes, "-1", warnings);
142 Assert.assertEquals(0d, component.getAftShoulderRadius(), 0.001);
143 handler.closeElement("ShoulderOD", attributes, "100", warnings);
144 Assert.assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius(), 0.001);
145 handler.closeElement("ShoulderOD", attributes, "foo", warnings);
146 Assert.assertEquals(1, warnings.size());
149 component.setType(Transition.Shape.HAACK);
150 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
151 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
152 handler.closeElement("ShapeParameter", attributes, "100", warnings);
153 Assert.assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter(), 0.001);
154 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
155 Assert.assertEquals(1, warnings.size());
156 Assert.assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
157 warnings.iterator().next().toString());
161 component.setType(Transition.Shape.CONICAL);
162 component.setShapeParameter(0d);
163 handler.closeElement("ShapeParameter", attributes, "100", warnings);
164 Assert.assertEquals(0d, component.getShapeParameter(), 0.001);
166 handler.closeElement("FinishCode", attributes, "-1", warnings);
167 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
168 handler.closeElement("FinishCode", attributes, "100", warnings);
169 Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
170 handler.closeElement("FinishCode", attributes, "foo", warnings);
171 Assert.assertEquals(1, warnings.size());
174 handler.closeElement("Name", attributes, "Test Name", warnings);
175 Assert.assertEquals("Test Name", component.getName());
177 handler.closeElement("Material", attributes, "Some Material", warnings);
178 handler.endHandler("NoseCone", attributes, null, warnings);
179 Assert.assertTrue(component.getMaterial().getName().contains("Some Material"));
183 * Method: getComponent()
185 * @throws Exception thrown if something goes awry
188 public void testGetComponent() throws Exception {
189 Assert.assertTrue(new NoseConeHandler(new Stage(), new WarningSet()).getComponent() instanceof NoseCone);
193 * Method: getMaterialType()
195 * @throws Exception thrown if something goes awry
198 public void testGetMaterialType() throws Exception {
199 Assert.assertEquals(Material.Type.BULK, new NoseConeHandler(new Stage(), new WarningSet()).getMaterialType());