create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / file / rocksim / importt / RingHandlerTest.java
1 /*
2  * RingHandlerTest.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.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.Bulkhead;
12 import net.sf.openrocket.rocketcomponent.CenteringRing;
13 import net.sf.openrocket.rocketcomponent.EngineBlock;
14 import net.sf.openrocket.rocketcomponent.RingComponent;
15 import net.sf.openrocket.rocketcomponent.RocketComponent;
16 import net.sf.openrocket.rocketcomponent.TubeCoupler;
17 import org.junit.Assert;
18 import org.junit.Test;
19
20 import java.util.HashMap;
21
22 /**
23  * RingHandler Tester.
24  */
25 public class RingHandlerTest extends RocksimTestBase {
26
27     /**
28      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
29      *
30      * @throws Exception thrown if something goes awry
31      */
32     @org.junit.Test
33     public void testOpenElement() throws Exception {
34         Assert.assertEquals(PlainTextHandler.INSTANCE, new RingHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
35     }
36
37     /**
38      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
39      *
40      * @throws Exception thrown if something goes awry
41      */
42     @org.junit.Test
43     public void testCloseElement() throws Exception {
44
45         BodyTube tube = new BodyTube();
46         RingHandler handler = new RingHandler(tube, new WarningSet());
47         CenteringRing component = (CenteringRing) getField(handler, "ring");
48         HashMap<String, String> attributes = new HashMap<String, String>();
49         WarningSet warnings = new WarningSet();
50
51         handler.closeElement("OD", attributes, "0", warnings);
52         Assert.assertEquals(0d, component.getOuterRadius(), 0.001);
53         handler.closeElement("OD", attributes, "75", warnings);
54         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getOuterRadius(), 0.001);
55         handler.closeElement("OD", attributes, "foo", warnings);
56         Assert.assertEquals(1, warnings.size());
57         warnings.clear();
58
59         handler.closeElement("ID", attributes, "0", warnings);
60         Assert.assertEquals(0d, component.getInnerRadius(), 0.001);
61         handler.closeElement("ID", attributes, "75", warnings);
62         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius(), 0.001);
63         handler.closeElement("ID", attributes, "foo", warnings);
64         Assert.assertEquals(1, warnings.size());
65         warnings.clear();
66
67         handler.closeElement("Len", attributes, "-1", warnings);
68         Assert.assertEquals(0d, component.getLength(), 0.001);
69         handler.closeElement("Len", attributes, "10", warnings);
70         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
71         handler.closeElement("Len", attributes, "10.0", warnings);
72         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
73         handler.closeElement("Len", attributes, "foo", warnings);
74         Assert.assertEquals(1, warnings.size());
75         warnings.clear();
76
77         handler.closeElement("Name", attributes, "Test Name", warnings);
78         Assert.assertEquals("Test Name", component.getName());
79     }
80
81     /**
82      * Test a bulkhead.
83      *
84      * @throws Exception thrown if something goes awry
85      */
86     @Test
87     public void testBulkhead() throws Exception {
88         BodyTube tube = new BodyTube();
89         RingHandler handler = new RingHandler(tube, new WarningSet());
90         CenteringRing component = (CenteringRing) getField(handler, "ring");
91         HashMap<String, String> attributes = new HashMap<String, String>();
92         WarningSet warnings = new WarningSet();
93
94         handler.closeElement("OD", attributes, "75", warnings);
95         handler.closeElement("ID", attributes, "0", warnings);
96         handler.closeElement("Len", attributes, "10", warnings);
97         handler.closeElement("Name", attributes, "Test Name", warnings);
98         handler.closeElement("KnownMass", attributes, "109.9", warnings);
99         handler.closeElement("UsageCode", attributes, "1", warnings);
100         handler.closeElement("UseKnownCG", attributes, "1", warnings);
101         handler.endHandler("", attributes, "", warnings);
102         
103         Assert.assertEquals(1, tube.getChildren().size());
104         RingComponent child = (RingComponent)tube.getChild(0);
105
106         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getOuterRadius(), 0.001);
107         Assert.assertEquals(0d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getInnerRadius(), 0.001);
108         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, child.getLength(), 0.001);
109         Assert.assertEquals("Test Name", child.getName());
110         Assert.assertEquals(109.9/1000, child.getMass(), 0.001);
111         Assert.assertEquals(0, child.getPositionValue(), 0.0);
112         Assert.assertEquals(RocketComponent.Position.TOP, child.getRelativePosition());
113         Assert.assertTrue(child instanceof Bulkhead);
114
115     }
116     
117     /**
118      * Test a tube coupler.
119      *
120      * @throws Exception thrown if something goes awry
121      */
122     @Test
123     public void testTubeCoupler() throws Exception {
124         BodyTube tube = new BodyTube();
125         RingHandler handler = new RingHandler(tube, new WarningSet());
126         HashMap<String, String> attributes = new HashMap<String, String>();
127         WarningSet warnings = new WarningSet();
128
129         handler.closeElement("OD", attributes, "75", warnings);
130         handler.closeElement("ID", attributes, "70", warnings);
131         handler.closeElement("Len", attributes, "10", warnings);
132         handler.closeElement("Name", attributes, "Test Name", warnings);
133         handler.closeElement("KnownMass", attributes, "109.9", warnings);
134         handler.closeElement("UsageCode", attributes, "4", warnings);
135         handler.closeElement("UseKnownCG", attributes, "1", warnings);
136         handler.endHandler("", attributes, "", warnings);
137
138         Assert.assertEquals(1, tube.getChildren().size());
139         RingComponent child = (RingComponent)tube.getChild(0);
140         Assert.assertTrue(child instanceof TubeCoupler);
141
142         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getOuterRadius(), 0.001);
143         Assert.assertEquals(70d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getInnerRadius(), 0.001);
144         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, child.getLength(), 0.001);
145         Assert.assertEquals("Test Name", child.getName());
146         Assert.assertEquals(109.9/1000, child.getMass(), 0.001);
147         Assert.assertEquals(0, child.getPositionValue(), 0.0);
148         Assert.assertEquals(RocketComponent.Position.TOP, child.getRelativePosition());
149     }
150
151     /**
152      * Test a engine block.
153      *
154      * @throws Exception thrown if something goes awry
155      */
156     @Test
157     public void testEngineBlock() throws Exception {
158         BodyTube tube = new BodyTube();
159         RingHandler handler = new RingHandler(tube, new WarningSet());
160         HashMap<String, String> attributes = new HashMap<String, String>();
161         WarningSet warnings = new WarningSet();
162
163         handler.closeElement("OD", attributes, "75", warnings);
164         handler.closeElement("ID", attributes, "70", warnings);
165         handler.closeElement("Len", attributes, "10", warnings);
166         handler.closeElement("Name", attributes, "Test Name", warnings);
167         handler.closeElement("KnownMass", attributes, "109.9", warnings);
168         handler.closeElement("UsageCode", attributes, "2", warnings);
169         handler.closeElement("KnownCG", attributes, "4", warnings);
170         handler.closeElement("UseKnownCG", attributes, "1", warnings);
171         handler.endHandler("", attributes, "", warnings);
172
173         Assert.assertEquals(1, tube.getChildren().size());
174         RingComponent child = (RingComponent)tube.getChild(0);
175         Assert.assertTrue(child instanceof EngineBlock);
176
177         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getOuterRadius(), 0.001);
178         Assert.assertEquals(70d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getInnerRadius(), 0.001);
179         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, child.getLength(), 0.001);
180         Assert.assertEquals("Test Name", child.getName());
181         Assert.assertEquals(109.9/1000, child.getMass(), 0.001);
182         Assert.assertEquals(0, child.getPositionValue(), 0.0);
183         Assert.assertEquals(RocketComponent.Position.TOP, child.getRelativePosition());
184         Assert.assertEquals(4d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, child.getCG().x, 0.000001);
185         
186     }
187
188     /**
189      * Test a centering ring
190      *
191      * @throws Exception thrown if something goes awry
192      */
193     @Test
194     public void testRing() throws Exception {
195         BodyTube tube = new BodyTube();
196         RingHandler handler = new RingHandler(tube, new WarningSet());
197         HashMap<String, String> attributes = new HashMap<String, String>();
198         WarningSet warnings = new WarningSet();
199
200         handler.closeElement("OD", attributes, "75", warnings);
201         handler.closeElement("ID", attributes, "0", warnings);
202         handler.closeElement("Len", attributes, "10", warnings);
203         handler.closeElement("Name", attributes, "Test Name", warnings);
204         handler.closeElement("KnownMass", attributes, "109.9", warnings);
205         handler.closeElement("UsageCode", attributes, "0", warnings);
206         handler.closeElement("UseKnownCG", attributes, "1", warnings);
207         handler.endHandler("", attributes, "", warnings);
208
209         Assert.assertEquals(1, tube.getChildren().size());
210         RingComponent child = (RingComponent)tube.getChild(0);
211
212         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getOuterRadius(), 0.001);
213         Assert.assertEquals(0d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, child.getInnerRadius(), 0.001);
214         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, child.getLength(), 0.001);
215         Assert.assertEquals("Test Name", child.getName());
216         Assert.assertEquals(109.9/1000, child.getMass(), 0.001);
217         Assert.assertEquals(0, child.getPositionValue(), 0.0);
218         Assert.assertEquals(RocketComponent.Position.TOP, child.getRelativePosition());
219         Assert.assertTrue(child instanceof CenteringRing);
220     }
221
222     /**
223      * Method: constructor
224      *
225      * @throws Exception thrown if something goes awry
226      */
227     @org.junit.Test
228     public void testConstructor() throws Exception {
229
230         try {
231             new RingHandler(null, new WarningSet());
232             Assert.fail("Should have thrown IllegalArgumentException");
233         }
234         catch (IllegalArgumentException iae) {
235             //success
236         }
237
238         BodyTube tube = new BodyTube();
239         RingHandler handler = new RingHandler(tube, new WarningSet());
240         CenteringRing component = (CenteringRing) getField(handler, "ring");
241     }
242
243     /**
244      * Method: setRelativePosition(RocketComponent.Position position)
245      *
246      * @throws Exception thrown if something goes awry
247      */
248     @org.junit.Test
249     public void testSetRelativePosition() throws Exception {
250         BodyTube tube = new BodyTube();
251         RingHandler handler = new RingHandler(tube, new WarningSet());
252         CenteringRing component = (CenteringRing) getField(handler, "ring");
253         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
254         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
255     }
256
257     
258     /**
259      * Method: getComponent()
260      *
261      * @throws Exception thrown if something goes awry
262      */
263     @org.junit.Test
264     public void testGetComponent() throws Exception {
265         Assert.assertTrue(new RingHandler(new BodyTube(), new WarningSet()).getComponent() instanceof CenteringRing);
266     }
267
268     /**
269      * Method: getMaterialType()
270      *
271      * @throws Exception thrown if something goes awry
272      */
273     @org.junit.Test
274     public void testGetMaterialType() throws Exception {
275         Assert.assertEquals(Material.Type.BULK, new RingHandler(new BodyTube(), new WarningSet()).getMaterialType());
276     }
277
278
279 }