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.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.NoseCone;
11 import net.sf.openrocket.rocketcomponent.Stage;
12 import net.sf.openrocket.rocketcomponent.Transition;
13 import net.sf.openrocket.rocketcomponent.ExternalComponent;
14 import net.sf.openrocket.aerodynamics.WarningSet;
15 import net.sf.openrocket.database.Databases;
17 import java.util.HashMap;
20 * NoseConeHandler Tester.
23 public class NoseConeHandlerTest extends RocksimTestBase {
26 * The class under test.
28 public static final Class classUT = NoseConeHandler.class;
31 * The test class (this class).
33 public static final Class testClass = NoseConeHandlerTest.class;
36 * Create a test suite of all tests within this test class.
38 * @return a suite of tests
40 public static Test suite() {
41 return new TestSuite(NoseConeHandlerTest.class);
47 * @param name the name of the test to run.
49 public NoseConeHandlerTest(String name) {
56 public void setUp() throws Exception {
61 * Teardown the fixture.
63 public void tearDown() throws Exception {
70 * @throws Exception thrown if something goes awry
72 public void testConstructor() throws Exception {
75 new NoseConeHandler(null);
76 fail("Should have thrown IllegalArgumentException");
78 catch (IllegalArgumentException iae) {
82 Stage stage = new Stage();
83 NoseConeHandler handler = new NoseConeHandler(stage);
84 NoseCone component = (NoseCone) getField(handler, "noseCone");
85 assertContains(component, stage.getChildren());
89 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
91 * @throws Exception thrown if something goes awry
93 public void testOpenElement() throws Exception {
94 assertEquals(PlainTextHandler.INSTANCE, new NoseConeHandler(new Stage()).openElement(null, null, null));
95 assertNotNull(new NoseConeHandler(new Stage()).openElement("AttachedParts", null, null));
100 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
102 * @throws Exception thrown if something goes awry
104 public void testCloseElement() throws Exception {
106 Stage stage = new Stage();
107 HashMap<String, String> attributes = new HashMap<String, String>();
108 WarningSet warnings = new WarningSet();
110 NoseConeHandler handler = new NoseConeHandler(stage);
111 NoseCone component = (NoseCone) getField(handler, "noseCone");
113 handler.closeElement("ShapeCode", attributes, "0", warnings);
114 assertEquals(Transition.Shape.CONICAL, component.getType());
115 handler.closeElement("ShapeCode", attributes, "1", warnings);
116 assertEquals(Transition.Shape.OGIVE, component.getType());
117 handler.closeElement("ShapeCode", attributes, "17", warnings);
118 assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType()); //test of default
119 handler.closeElement("ShapeCode", attributes, "foo", warnings);
120 assertNotNull(component.getType());
121 assertEquals(1, warnings.size());
124 handler.closeElement("Len", attributes, "-1", warnings);
125 assertEquals(0d, component.getLength());
126 handler.closeElement("Len", attributes, "10", warnings);
127 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
128 handler.closeElement("Len", attributes, "10.0", warnings);
129 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
130 handler.closeElement("Len", attributes, "foo", warnings);
131 assertEquals(1, warnings.size());
134 handler.closeElement("BaseDia", attributes, "-1", warnings);
135 assertEquals(0d, component.getAftRadius());
136 handler.closeElement("BaseDia", attributes, "100", warnings);
137 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius());
138 handler.closeElement("BaseDia", attributes, "foo", warnings);
139 assertEquals(1, warnings.size());
143 final double aft = 100d;
144 component.setAftRadius(aft);
146 handler.closeElement("ConstructionType", attributes, "0", warnings);
147 component.setAftShoulderRadius(1.1d);
148 handler.closeElement("WallThickness", attributes, "-1", warnings);
149 handler.endHandler("Transition", attributes, null, warnings);
150 assertEquals(component.getAftRadius(), component.getThickness());
151 assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness());
152 handler.closeElement("WallThickness", attributes, "100", warnings);
153 handler.endHandler("Transition", attributes, null, warnings);
154 assertEquals(aft, component.getThickness());
155 handler.closeElement("WallThickness", attributes, "foo", warnings);
156 handler.endHandler("Transition", attributes, null, warnings);
157 assertEquals(1, warnings.size());
160 handler.closeElement("ConstructionType", attributes, "1", warnings);
161 component.setAftShoulderRadius(1.1d);
162 handler.closeElement("WallThickness", attributes, "-1", warnings);
163 handler.endHandler("Transition", attributes, null, warnings);
164 assertEquals(0d, component.getThickness());
165 assertEquals(0d, component.getAftShoulderThickness());
166 handler.closeElement("WallThickness", attributes, "1.1", warnings);
167 handler.endHandler("Transition", attributes, null, warnings);
168 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness());
169 assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness());
171 handler.closeElement("ShoulderLen", attributes, "-1", warnings);
172 assertEquals(0d, component.getAftShoulderLength());
173 handler.closeElement("ShoulderLen", attributes, "10", warnings);
174 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
175 handler.closeElement("ShoulderLen", attributes, "10.0", warnings);
176 assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
177 handler.closeElement("ShoulderLen", attributes, "foo", warnings);
178 assertEquals(1, warnings.size());
181 handler.closeElement("ShoulderOD", attributes, "-1", warnings);
182 assertEquals(0d, component.getAftShoulderRadius());
183 handler.closeElement("ShoulderOD", attributes, "100", warnings);
184 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius());
185 handler.closeElement("ShoulderOD", attributes, "foo", warnings);
186 assertEquals(1, warnings.size());
189 component.setType(Transition.Shape.HAACK);
190 handler.closeElement("ShapeParameter", attributes, "-1", warnings);
191 assertEquals(0d, component.getShapeParameter());
192 handler.closeElement("ShapeParameter", attributes, "100", warnings);
193 assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter());
194 handler.closeElement("ShapeParameter", attributes, "foo", warnings);
195 assertEquals(1, warnings.size());
196 assertEquals("Could not convert ShapeParameter value of foo. It is expected to be a number.",
197 warnings.iterator().next().toString());
201 component.setType(Transition.Shape.CONICAL);
202 component.setShapeParameter(0d);
203 handler.closeElement("ShapeParameter", attributes, "100", warnings);
204 assertEquals(0d, component.getShapeParameter());
206 handler.closeElement("FinishCode", attributes, "-1", warnings);
207 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
208 handler.closeElement("FinishCode", attributes, "100", warnings);
209 assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
210 handler.closeElement("FinishCode", attributes, "foo", warnings);
211 assertEquals(1, warnings.size());
214 handler.closeElement("Name", attributes, "Test Name", warnings);
215 assertEquals("Test Name", component.getName());
217 handler.closeElement("Material", attributes, "Some Material", warnings);
218 handler.endHandler("NoseCone", attributes, null, warnings);
219 assertTrue(component.getMaterial().getName().contains("Some Material"));
223 * Method: getComponent()
225 * @throws Exception thrown if something goes awry
227 public void testGetComponent() throws Exception {
228 assertTrue(new NoseConeHandler(new Stage()).getComponent() instanceof NoseCone);
232 * Method: getMaterialType()
234 * @throws Exception thrown if something goes awry
236 public void testGetMaterialType() throws Exception {
237 assertEquals(Material.Type.BULK, new NoseConeHandler(new Stage()).getMaterialType());