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