4 package net.sf.openrocket.file.rocksim.importt;
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;
20 import java.util.HashMap;
25 public class RingHandlerTest extends RocksimTestBase {
28 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
30 * @throws Exception thrown if something goes awry
33 public void testOpenElement() throws Exception {
34 Assert.assertEquals(PlainTextHandler.INSTANCE, new RingHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
38 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
40 * @throws Exception thrown if something goes awry
43 public void testCloseElement() throws Exception {
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();
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());
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());
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());
77 handler.closeElement("Name", attributes, "Test Name", warnings);
78 Assert.assertEquals("Test Name", component.getName());
84 * @throws Exception thrown if something goes awry
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();
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);
103 Assert.assertEquals(1, tube.getChildren().size());
104 RingComponent child = (RingComponent)tube.getChild(0);
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);
118 * Test a tube coupler.
120 * @throws Exception thrown if something goes awry
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();
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);
138 Assert.assertEquals(1, tube.getChildren().size());
139 RingComponent child = (RingComponent)tube.getChild(0);
140 Assert.assertTrue(child instanceof TubeCoupler);
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());
152 * Test a engine block.
154 * @throws Exception thrown if something goes awry
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();
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);
173 Assert.assertEquals(1, tube.getChildren().size());
174 RingComponent child = (RingComponent)tube.getChild(0);
175 Assert.assertTrue(child instanceof EngineBlock);
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);
189 * Test a centering ring
191 * @throws Exception thrown if something goes awry
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();
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);
209 Assert.assertEquals(1, tube.getChildren().size());
210 RingComponent child = (RingComponent)tube.getChild(0);
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);
223 * Method: constructor
225 * @throws Exception thrown if something goes awry
228 public void testConstructor() throws Exception {
231 new RingHandler(null, new WarningSet());
232 Assert.fail("Should have thrown IllegalArgumentException");
234 catch (IllegalArgumentException iae) {
238 BodyTube tube = new BodyTube();
239 RingHandler handler = new RingHandler(tube, new WarningSet());
240 CenteringRing component = (CenteringRing) getField(handler, "ring");
244 * Method: setRelativePosition(RocketComponent.Position position)
246 * @throws Exception thrown if something goes awry
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());
259 * Method: getComponent()
261 * @throws Exception thrown if something goes awry
264 public void testGetComponent() throws Exception {
265 Assert.assertTrue(new RingHandler(new BodyTube(), new WarningSet()).getComponent() instanceof CenteringRing);
269 * Method: getMaterialType()
271 * @throws Exception thrown if something goes awry
274 public void testGetMaterialType() throws Exception {
275 Assert.assertEquals(Material.Type.BULK, new RingHandler(new BodyTube(), new WarningSet()).getMaterialType());