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