2 * MassObjectHandlerTest.java
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.MassComponent;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import org.junit.Assert;
15 import java.util.HashMap;
18 * MassObjectHandler Tester.
21 public class MassObjectHandlerTest extends RocksimTestBase {
26 * @throws Exception thrown if something goes awry
29 public void testConstructor() throws Exception {
32 new MassObjectHandler(null, new WarningSet());
33 Assert.fail("Should have thrown IllegalArgumentException");
35 catch (IllegalArgumentException iae) {
39 BodyTube tube = new BodyTube();
40 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
41 MassComponent component = (MassComponent) getField(handler, "mass");
42 assertContains(component, tube.getChildren());
46 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
48 * @throws Exception thrown if something goes awry
51 public void testOpenElement() throws Exception {
52 Assert.assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
57 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
59 * @throws Exception thrown if something goes awry
62 public void testCloseElement() throws Exception {
63 BodyTube tube = new BodyTube();
64 HashMap<String, String> attributes = new HashMap<String, String>();
65 WarningSet warnings = new WarningSet();
67 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
68 MassComponent component = (MassComponent) getField(handler, "mass");
70 handler.closeElement("Len", attributes, "-1", warnings);
71 Assert.assertEquals(0d, component.getLength(), 0.001);
72 handler.closeElement("Len", attributes, "10", warnings);
73 Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
74 , component.getLength(), 0.001);
75 handler.closeElement("Len", attributes, "10.0", warnings);
76 Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
77 , component.getLength(), 0.001);
78 handler.closeElement("Len", attributes, "foo", warnings);
79 Assert.assertEquals(1, warnings.size());
82 handler.closeElement("KnownMass", attributes, "-1", warnings);
83 Assert.assertEquals(0d, component.getComponentMass(), 0.001);
84 handler.closeElement("KnownMass", attributes, "100", warnings);
85 Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS, component.getComponentMass(), 0.001);
86 handler.closeElement("KnownMass", attributes, "foo", warnings);
87 Assert.assertEquals(1, warnings.size());
93 * Method: setRelativePosition(RocketComponent.Position position)
95 * @throws Exception thrown if something goes awry
98 public void testSetRelativePosition() throws Exception {
99 BodyTube tube = new BodyTube();
100 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
101 MassComponent component = (MassComponent) getField(handler, "mass");
102 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
103 Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
107 * Method: getComponent()
109 * @throws Exception thrown if something goes awry
112 public void testGetComponent() throws Exception {
113 Assert.assertTrue(new MassObjectHandler(new BodyTube(), new WarningSet()).getComponent() instanceof MassComponent);
117 * Method: getMaterialType()
119 * @throws Exception thrown if something goes awry
122 public void testGetMaterialType() throws Exception {
123 Assert.assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());