2 * NoseConeHandlerTest.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.NoseCone;
13 import net.sf.openrocket.rocketcomponent.Stage;
14 import net.sf.openrocket.rocketcomponent.Transition;
16 import java.util.HashMap;
19 * NoseConeHandler Tester.
22 public class NoseConeHandlerTest extends RocksimTestBase {
25 * The class under test.
27 public static final Class classUT = NoseConeHandler.class;
30 * The test class (this class).
32 public static final Class testClass = NoseConeHandlerTest.class;
35 * Create a test suite of all tests within this test class.
37 * @return a suite of tests
39 public static Test suite() {
40 return new TestSuite(NoseConeHandlerTest.class);
46 * @param name the name of the test to run.
48 public NoseConeHandlerTest(String name) {
55 public void setUp() throws Exception {
60 * Teardown the fixture.
62 public void tearDown() throws Exception {
69 * @throws Exception thrown if something goes awry
71 public void testConstructor() throws Exception {
74 new NoseConeHandler(null, new WarningSet());
75 fail("Should have thrown IllegalArgumentException");
77 catch (IllegalArgumentException iae) {
81 Stage stage = new Stage();
82 NoseConeHandler handler = new NoseConeHandler(stage, new WarningSet());
83 NoseCone component = (NoseCone) getField(handler, "noseCone");
84 assertContains(component, stage.getChildren());
88 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
90 * @throws Exception thrown if something goes awry
92 public void testOpenElement() throws Exception {
93 assertEquals(PlainTextHandler.INSTANCE, new NoseConeHandler(new Stage(), new WarningSet()).openElement(null, null, null));
94 assertNotNull(new NoseConeHandler(new Stage(), new WarningSet()).openElement("AttachedParts", null, null));
99 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
101 * @throws Exception thrown if something goes awry
103 public void testCloseElement() throws Exception {
105 Stage stage = new Stage();
106 HashMap<String, String> attributes = new HashMap<String, String>();
107 WarningSet warnings = new WarningSet();
109 NoseConeHandler handler = new NoseConeHandler(stage, warnings);
110 NoseCone component = (NoseCone) getField(handler, "noseCone");
112 handler.closeElement("ShapeCode", attributes, "0", warnings);
113 assertEquals(Transition.Shape.CONICAL, component.getType());
114 handler.closeElement("ShapeCode", attributes, "1", warnings);
115 assertEquals(Transition.Shape.OGIVE, component.getType());
116 handler.closeElement("ShapeCode", attributes, "17", warnings);
117 assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
118 handler.closeElement("ShapeCode", attributes, "foo", warnings);
119 assertNotNull(component.getType());
120 assertEquals(1, warnings.size());
123 handler.closeElement("Len", attributes, "-1", warnings);
124 assertEquals(0d, component.getLength());
125 handler.closeElement("Len", attributes, "10", warnings);
126 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
127 handler.closeElement("Len", attributes, "10.0", warnings);
128 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
129 handler.closeElement("Len", attributes, "foo", warnings);
130 assertEquals(1, warnings.size());
133 handler.closeElement("BaseDia", attributes, "-1", warnings);
134 assertEquals(0d, component.getAftRadius());
135 handler.closeElement("BaseDia", attributes, "100", warnings);
136 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius());
137 handler.closeElement("BaseDia", attributes, "foo", warnings);
138 assertEquals(1, warnings.size());
142 final double aft = 100d;
143 component.setAftRadius(aft);
145 handler.closeElement("ConstructionType", attributes, "0", warnings);
146 component.setAftShoulderRadius(1.1d);
147 handler.closeElement("WallThickness", attributes, "-1", warnings);
148 handler.endHandler("Transition", attributes, null, warnings);
149 assertEquals(component.getAftRadius(), component.getThickness());
150 assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness());
151 handler.closeElement("WallThickness", attributes, "100", warnings);
152 handler.endHandler("Transition", attributes, null, warnings);
153 assertEquals(aft, component.getThickness());
154 handler.closeElement("WallThickness", attributes, "foo", warnings);
155 handler.endHandler("Transition", attributes, null, warnings);
156 assertEquals(1, warnings.size());
159 handler.closeElement("ConstructionType", attributes, "1", warnings);
160 component.setAftShoulderRadius(1.1d);
161 handler.closeElement("WallThickness", attributes, "-1", warnings);
162 handler.endHandler("Transition", attributes, null, warnings);
163 assertEquals(0d, component.getThickness());
164 assertEquals(0d, component.getAftShoulderThickness());
165 handler.closeElement("WallThickness", attributes, "1.1", warnings);
166 handler.endHandler("Transition", attributes, null, warnings);
167 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness());
168 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness());
170 handler.closeElement("ShoulderLen", attributes, "-1", warnings);
171 assertEquals(0d, component.getAftShoulderLength());
172 handler.closeElement("ShoulderLen", attributes, "10", warnings);
173 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
174 handler.closeElement("ShoulderLen", attributes, "10.0", warnings);
175 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
176 handler.closeElement("ShoulderLen", attributes, "foo", warnings);
177 assertEquals(1, warnings.size());
180 handler.closeElement("ShoulderOD", attributes, "-1", warnings);
181 assertEquals(0d, component.getAftShoulderRadius());
182 handler.closeElement("ShoulderOD", attributes, "100", warnings);
183 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius());
184 handler.closeElement("ShoulderOD", attributes, "foo", warnings);
185 assertEquals(1, warnings.size());
188 component.setType(Transition.Shape.HAACK);
189 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
190 assertEquals(0d, component.getShapeParameter());
191 handler.closeElement("ShapeParameter", attributes, "100", warnings);
192 assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter());
193 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
194 assertEquals(1, warnings.size());
195 assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
196 warnings.iterator().next().toString());
200 component.setType(Transition.Shape.CONICAL);
201 component.setShapeParameter(0d);
202 handler.closeElement("ShapeParameter", attributes, "100", warnings);
203 assertEquals(0d, component.getShapeParameter());
205 handler.closeElement("FinishCode", attributes, "-1", warnings);
206 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
207 handler.closeElement("FinishCode", attributes, "100", warnings);
208 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
209 handler.closeElement("FinishCode", attributes, "foo", warnings);
210 assertEquals(1, warnings.size());
213 handler.closeElement("Name", attributes, "Test Name", warnings);
214 assertEquals("Test Name", component.getName());
216 handler.closeElement("Material", attributes, "Some Material", warnings);
217 handler.endHandler("NoseCone", attributes, null, warnings);
218 assertTrue(component.getMaterial().getName().contains("Some Material"));
222 * Method: getComponent()
224 * @throws Exception thrown if something goes awry
226 public void testGetComponent() throws Exception {
227 assertTrue(new NoseConeHandler(new Stage(), new WarningSet()).getComponent() instanceof NoseCone);
231 * Method: getMaterialType()
233 * @throws Exception thrown if something goes awry
235 public void testGetMaterialType() throws Exception {
236 assertEquals(Material.Type.BULK, new NoseConeHandler(new Stage(), new WarningSet()).getMaterialType());