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