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