logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / NoseConeHandlerTest.java
1 /*
2  * NoseConeHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
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;
16
17 import java.util.HashMap;
18
19 /**
20  * NoseConeHandler Tester.
21  *
22  */
23 public class NoseConeHandlerTest extends RocksimTestBase {
24
25     /**
26      * The class under test.
27      */
28     public static final Class classUT = NoseConeHandler.class;
29
30     /**
31      * The test class (this class).
32      */
33     public static final Class testClass = NoseConeHandlerTest.class;
34
35     /**
36      * Create a test suite of all tests within this test class.
37      *
38      * @return a suite of tests
39      */
40     public static Test suite() {
41         return new TestSuite(NoseConeHandlerTest.class);
42     }
43
44     /**
45      * Test constructor.
46      *
47      * @param name the name of the test to run.
48      */
49     public NoseConeHandlerTest(String name) {
50         super(name);
51     }
52
53     /**
54      * Setup the fixture.
55      */
56     public void setUp() throws Exception {
57         super.setUp();
58     }
59
60     /**
61      * Teardown the fixture.
62      */
63     public void tearDown() throws Exception {
64         super.tearDown();
65     }
66
67     /**
68      * Method: constructor
69      *
70      * @throws Exception thrown if something goes awry
71      */
72     public void testConstructor() throws Exception {
73
74         try {
75             new NoseConeHandler(null);
76             fail("Should have thrown IllegalArgumentException");
77         }
78         catch (IllegalArgumentException iae) {
79             //success
80         }
81
82         Stage stage = new Stage();
83         NoseConeHandler handler = new NoseConeHandler(stage);
84         NoseCone component = (NoseCone) getField(handler, "noseCone");
85         assertContains(component, stage.getChildren());
86     }
87
88     /**
89      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
90      *
91      * @throws Exception thrown if something goes awry
92      */
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));
96     }
97
98     /**
99      *
100      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
101      *
102      * @throws Exception  thrown if something goes awry
103      */
104     public void testCloseElement() throws Exception {
105
106         Stage stage = new Stage();
107         HashMap<String, String> attributes = new HashMap<String, String>();
108         WarningSet warnings = new WarningSet();
109
110         NoseConeHandler handler = new NoseConeHandler(stage);
111         NoseCone component = (NoseCone) getField(handler, "noseCone");
112
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());
122         warnings.clear();
123
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());
132         warnings.clear();
133
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());
140         warnings.clear();
141
142         
143         final double aft = 100d;
144         component.setAftRadius(aft);
145         
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());
158         warnings.clear();
159         
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());
170         
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());
179         warnings.clear();
180
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());
187         warnings.clear();
188
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());
198
199         warnings.clear();
200
201         component.setType(Transition.Shape.CONICAL);
202         component.setShapeParameter(0d);
203         handler.closeElement("ShapeParameter", attributes, "100", warnings);
204         assertEquals(0d, component.getShapeParameter());
205
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());
212         warnings.clear();
213
214         handler.closeElement("Name", attributes, "Test Name", warnings);
215         assertEquals("Test Name", component.getName());
216         
217         handler.closeElement("Material", attributes, "Some Material", warnings);
218         handler.endHandler("NoseCone", attributes, null, warnings);
219         assertTrue(component.getMaterial().getName().contains("Some Material"));
220      }
221
222     /**
223      * Method: getComponent()
224      *
225      * @throws Exception thrown if something goes awry
226      */
227     public void testGetComponent() throws Exception {
228         assertTrue(new NoseConeHandler(new Stage()).getComponent() instanceof NoseCone);
229     }
230
231     /**
232      * Method: getMaterialType()
233      *
234      * @throws Exception thrown if something goes awry
235      */
236     public void testGetMaterialType() throws Exception {
237         assertEquals(Material.Type.BULK, new NoseConeHandler(new Stage()).getMaterialType());
238     }
239 }