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