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