4 package net.sf.openrocket.file.rocksim;
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;
19 import java.util.HashMap;
24 public class RingHandlerTest extends RocksimTestBase {
27 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
29 * @throws Exception thrown if something goes awry
32 public void testOpenElement() throws Exception {
33 Assert.assertEquals(PlainTextHandler.INSTANCE, new RingHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
37 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
39 * @throws Exception thrown if something goes awry
42 public void testCloseElement() throws Exception {
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();
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());
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());
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());
76 handler.closeElement("Name", attributes, "Test Name", warnings);
77 Assert.assertEquals("Test Name", component.getName());
83 * @throws Exception thrown if something goes awry
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();
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);
102 Assert.assertEquals(1, tube.getChildren().size());
103 RingComponent child = (RingComponent)tube.getChild(0);
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);
117 * Test a tube coupler.
119 * @throws Exception thrown if something goes awry
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();
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);
137 Assert.assertEquals(1, tube.getChildren().size());
138 RingComponent child = (RingComponent)tube.getChild(0);
139 Assert.assertTrue(child instanceof TubeCoupler);
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());
151 * Test a engine block.
153 * @throws Exception thrown if something goes awry
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();
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);
172 Assert.assertEquals(1, tube.getChildren().size());
173 RingComponent child = (RingComponent)tube.getChild(0);
174 Assert.assertTrue(child instanceof EngineBlock);
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);
188 * Test a centering ring
190 * @throws Exception thrown if something goes awry
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();
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);
208 Assert.assertEquals(1, tube.getChildren().size());
209 RingComponent child = (RingComponent)tube.getChild(0);
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);
222 * Method: constructor
224 * @throws Exception thrown if something goes awry
227 public void testConstructor() throws Exception {
230 new RingHandler(null, new WarningSet());
231 Assert.fail("Should have thrown IllegalArgumentException");
233 catch (IllegalArgumentException iae) {
237 BodyTube tube = new BodyTube();
238 RingHandler handler = new RingHandler(tube, new WarningSet());
239 CenteringRing component = (CenteringRing) getField(handler, "ring");
243 * Method: setRelativePosition(RocketComponent.Position position)
245 * @throws Exception thrown if something goes awry
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());
258 * Method: getComponent()
260 * @throws Exception thrown if something goes awry
263 public void testGetComponent() throws Exception {
264 Assert.assertTrue(new RingHandler(new BodyTube(), new WarningSet()).getComponent() instanceof CenteringRing);
268 * Method: getMaterialType()
270 * @throws Exception thrown if something goes awry
273 public void testGetMaterialType() throws Exception {
274 Assert.assertEquals(Material.Type.BULK, new RingHandler(new BodyTube(), new WarningSet()).getMaterialType());