logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / TransitionHandlerTest.java
1 /*
2  * TransitionHandlerTest.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.aerodynamics.WarningSet;
9 import net.sf.openrocket.database.Databases;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.ExternalComponent;
13 import net.sf.openrocket.rocketcomponent.Stage;
14 import net.sf.openrocket.rocketcomponent.Transition;
15
16 import java.util.HashMap;
17
18 /**
19  * TransitionHandler Tester.
20  */
21 public class TransitionHandlerTest extends RocksimTestBase {
22
23     /**
24      * The class under test.
25      */
26     public static final Class classUT = TransitionHandler.class;
27
28     /**
29      * The test class (this class).
30      */
31     public static final Class testClass = TransitionHandlerTest.class;
32
33     /**
34      * Create a test suite of all tests within this test class.
35      *
36      * @return a suite of tests
37      */
38     public static Test suite() {
39         return new TestSuite(TransitionHandlerTest.class);
40     }
41
42     /**
43      * Test constructor.
44      *
45      * @param name the name of the test to run.
46      */
47     public TransitionHandlerTest(String name) {
48         super(name);
49     }
50
51     /**
52      * Setup the fixture.
53      */
54     public void setUp() throws Exception {
55         super.setUp();
56     }
57
58     /**
59      * Teardown the fixture.
60      */
61     public void tearDown() throws Exception {
62         super.tearDown();
63     }
64
65     /**
66      * Method: constructor
67      *
68      * @throws Exception thrown if something goes awry
69      */
70     public void testConstructor() throws Exception {
71
72         try {
73             new TransitionHandler(null);
74             fail("Should have thrown IllegalArgumentException");
75         }
76         catch (IllegalArgumentException iae) {
77             //success
78         }
79
80         Stage stage = new Stage();
81         TransitionHandler handler = new TransitionHandler(stage);
82         Transition component = (Transition) getField(handler, "transition");
83         assertContains(component, stage.getChildren());
84     }
85
86     /**
87      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
88      *
89      * @throws Exception thrown if something goes awry
90      */
91     public void testOpenElement() throws Exception {
92         assertEquals(PlainTextHandler.INSTANCE, new TransitionHandler(new Stage()).openElement(null, null, null));
93     }
94
95     /**
96      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
97      *
98      * @throws Exception thrown if something goes awry
99      */
100     public void testCloseElement() throws Exception {
101
102         Stage stage = new Stage();
103         HashMap<String, String> attributes = new HashMap<String, String>();
104         WarningSet warnings = new WarningSet();
105
106         TransitionHandler handler = new TransitionHandler(stage);
107         Transition component = (Transition) getField(handler, "transition");
108
109         handler.closeElement("ShapeCode", attributes, "0", warnings);
110         assertEquals(Transition.Shape.CONICAL, component.getType());
111         handler.closeElement("ShapeCode", attributes, "1", warnings);
112         assertEquals(Transition.Shape.OGIVE, component.getType());
113         handler.closeElement("ShapeCode", attributes, "17", warnings);
114         assertEquals(RocksimNoseConeCode.PARABOLIC.asOpenRocket(), component.getType());  //test of default
115         handler.closeElement("ShapeCode", attributes, "foo", warnings);
116         assertNotNull(component.getType());
117         assertEquals(1, warnings.size());
118         warnings.clear();
119
120         handler.closeElement("Len", attributes, "-1", warnings);
121         assertEquals(0d, component.getLength());
122         handler.closeElement("Len", attributes, "10", warnings);
123         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
124         handler.closeElement("Len", attributes, "10.0", warnings);
125         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
126         handler.closeElement("Len", attributes, "foo", warnings);
127         assertEquals(1, warnings.size());
128         warnings.clear();
129
130         handler.closeElement("FrontDia", attributes, "-1", warnings);
131         assertEquals(0d, component.getForeRadius());
132         handler.closeElement("FrontDia", attributes, "100", warnings);
133         assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeRadius());
134         handler.closeElement("FrontDia", attributes, "foo", warnings);
135         assertEquals(1, warnings.size());
136         warnings.clear();
137
138         handler.closeElement("RearDia", attributes, "-1", warnings);
139         assertEquals(0d, component.getAftRadius());
140         handler.closeElement("RearDia", attributes, "100", warnings);
141         assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftRadius());
142         handler.closeElement("RearDia", attributes, "foo", warnings);
143         assertEquals(1, warnings.size());
144         warnings.clear();
145
146         final double aft = 100d;
147         component.setAftRadius(aft);
148         
149         handler.closeElement("ConstructionType", attributes, "0", warnings);
150         component.setAftShoulderRadius(1.1d);
151         component.setForeShoulderRadius(1.1d);
152         handler.closeElement("WallThickness", attributes, "-1", warnings);
153         handler.endHandler("Transition", attributes, null, warnings);
154         assertEquals(component.getAftRadius(), component.getThickness());
155         assertEquals(component.getAftShoulderThickness(), component.getAftShoulderThickness());
156         assertEquals(component.getForeShoulderThickness(), component.getForeShoulderThickness());
157         handler.closeElement("WallThickness", attributes, "100", warnings);
158         handler.endHandler("Transition", attributes, null, warnings);
159         assertEquals(aft, component.getThickness());
160         handler.closeElement("WallThickness", attributes, "foo", warnings);
161         handler.endHandler("Transition", attributes, null, warnings);
162         assertEquals(1, warnings.size());
163         warnings.clear();
164         
165         handler.closeElement("ConstructionType", attributes, "1", warnings);
166         component.setAftShoulderRadius(1.1d);
167         component.setForeShoulderRadius(1.1d);
168         handler.closeElement("WallThickness", attributes, "-1", warnings);
169         handler.endHandler("Transition", attributes, null, warnings);
170         assertEquals(0d, component.getThickness());
171         assertEquals(0d, component.getAftShoulderThickness());
172         assertEquals(0d, component.getForeShoulderThickness());
173         handler.closeElement("WallThickness", attributes, "1.1", warnings);
174         handler.endHandler("Transition", attributes, null, warnings);
175         assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getThickness());
176         assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderThickness());
177         assertEquals(1.1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderThickness());
178         
179
180         handler.closeElement("FrontShoulderLen", attributes, "-1", warnings);
181         assertEquals(0d, component.getForeShoulderLength());
182         handler.closeElement("FrontShoulderLen", attributes, "10", warnings);
183         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
184         handler.closeElement("FrontShoulderLen", attributes, "10.0", warnings);
185         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getForeShoulderLength());
186         handler.closeElement("FrontShoulderLen", attributes, "foo", warnings);
187         assertEquals(1, warnings.size());
188         warnings.clear();
189
190         handler.closeElement("RearShoulderLen", attributes, "-1", warnings);
191         assertEquals(0d, component.getAftShoulderLength());
192         handler.closeElement("RearShoulderLen", attributes, "10", warnings);
193         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
194         handler.closeElement("RearShoulderLen", attributes, "10.0", warnings);
195         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getAftShoulderLength());
196         handler.closeElement("RearShoulderLen", attributes, "foo", warnings);
197         assertEquals(1, warnings.size());
198         warnings.clear();
199
200         handler.closeElement("FrontShoulderDia", attributes, "-1", warnings);
201         assertEquals(0d, component.getForeShoulderRadius());
202         handler.closeElement("FrontShoulderDia", attributes, "100", warnings);
203         assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getForeShoulderRadius());
204         handler.closeElement("FrontShoulderDia", attributes, "foo", warnings);
205         assertEquals(1, warnings.size());
206         warnings.clear();
207
208         handler.closeElement("RearShoulderDia", attributes, "-1", warnings);
209         assertEquals(0d, component.getAftShoulderRadius());
210         handler.closeElement("RearShoulderDia", attributes, "100", warnings);
211         assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getAftShoulderRadius());
212         handler.closeElement("RearShoulderDia", attributes, "foo", warnings);
213         assertEquals(1, warnings.size());
214         warnings.clear();
215
216         component.setType(Transition.Shape.HAACK);
217         handler.closeElement("ShapeParameter", attributes, "-1", warnings);
218         assertEquals(0d, component.getShapeParameter());
219         handler.closeElement("ShapeParameter", attributes, "100", warnings);
220         assertEquals(Transition.Shape.HAACK.maxParameter(), component.getShapeParameter());
221         handler.closeElement("ShapeParameter", attributes, "foo", warnings);
222         assertEquals(1, warnings.size());
223         assertEquals("Could not convert ShapeParameter value of foo.  It is expected to be a number.", 
224                      warnings.iterator().next().toString());
225
226         warnings.clear();
227
228         component.setType(Transition.Shape.CONICAL);
229         component.setShapeParameter(0d);
230         handler.closeElement("ShapeParameter", attributes, "100", warnings);
231         assertEquals(0d, component.getShapeParameter());
232
233         handler.closeElement("FinishCode", attributes, "-1", warnings);
234         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
235         handler.closeElement("FinishCode", attributes, "100", warnings);
236         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
237         handler.closeElement("FinishCode", attributes, "foo", warnings);
238         assertEquals(1, warnings.size());
239         warnings.clear();
240
241         handler.closeElement("Name", attributes, "Test Name", warnings);
242         assertEquals("Test Name", component.getName());
243         
244         handler.closeElement("Material", attributes, "Some Material", warnings);
245         handler.endHandler("Transition", attributes, null, warnings);
246         assertTrue(component.getMaterial().getName().contains("Some Material"));
247     }
248
249     /**
250      * Method: getComponent()
251      *
252      * @throws Exception thrown if something goes awry
253      */
254     public void testGetComponent() throws Exception {
255         assertTrue(new TransitionHandler(new Stage()).getComponent() instanceof Transition);
256     }
257
258     /**
259      * Method: getMaterialType()
260      *
261      * @throws Exception thrown if something goes awry
262      */
263     public void testGetMaterialType() throws Exception {
264         assertEquals(Material.Type.BULK, new TransitionHandler(new Stage()).getMaterialType());
265     }
266
267
268 }