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