2 * MassObjectHandlerTest.java
4 package net.sf.openrocket.file.rocksim;
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import net.sf.openrocket.aerodynamics.WarningSet;
9 import net.sf.openrocket.file.simplesax.PlainTextHandler;
10 import net.sf.openrocket.material.Material;
11 import net.sf.openrocket.rocketcomponent.BodyTube;
12 import net.sf.openrocket.rocketcomponent.MassComponent;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
15 import java.util.HashMap;
18 * MassObjectHandler Tester.
21 public class MassObjectHandlerTest extends RocksimTestBase {
24 * The class under test.
26 public static final Class classUT = MassObjectHandler.class;
29 * The test class (this class).
31 public static final Class testClass = MassObjectHandlerTest.class;
34 * Create a test suite of all tests within this test class.
36 * @return a suite of tests
38 public static Test suite() {
39 return new TestSuite(MassObjectHandlerTest.class);
45 * @param name the name of the test to run.
47 public MassObjectHandlerTest(String name) {
54 public void setUp() throws Exception {
59 * Teardown the fixture.
61 public void tearDown() throws Exception {
68 * @throws Exception thrown if something goes awry
70 public void testConstructor() throws Exception {
73 new MassObjectHandler(null, new WarningSet());
74 fail("Should have thrown IllegalArgumentException");
76 catch (IllegalArgumentException iae) {
80 BodyTube tube = new BodyTube();
81 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
82 MassComponent component = (MassComponent) getField(handler, "mass");
83 assertContains(component, tube.getChildren());
87 * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
89 * @throws Exception thrown if something goes awry
91 public void testOpenElement() throws Exception {
92 assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
97 * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
99 * @throws Exception thrown if something goes awry
101 public void testCloseElement() throws Exception {
102 BodyTube tube = new BodyTube();
103 HashMap<String, String> attributes = new HashMap<String, String>();
104 WarningSet warnings = new WarningSet();
106 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
107 MassComponent component = (MassComponent) getField(handler, "mass");
109 handler.closeElement("Len", attributes, "-1", warnings);
110 assertEquals(0d, component.getLength());
111 handler.closeElement("Len", attributes, "10", warnings);
112 assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH)
113 , component.getLength());
114 handler.closeElement("Len", attributes, "10.0", warnings);
115 assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH)
116 , component.getLength());
117 handler.closeElement("Len", attributes, "foo", warnings);
118 assertEquals(1, warnings.size());
121 handler.closeElement("KnownMass", attributes, "-1", warnings);
122 assertEquals(0d, component.getComponentMass());
123 handler.closeElement("KnownMass", attributes, "100", warnings);
124 assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS, component.getComponentMass());
125 handler.closeElement("KnownMass", attributes, "foo", warnings);
126 assertEquals(1, warnings.size());
132 * Method: setRelativePosition(RocketComponent.Position position)
134 * @throws Exception thrown if something goes awry
136 public void testSetRelativePosition() throws Exception {
137 BodyTube tube = new BodyTube();
138 MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
139 MassComponent component = (MassComponent) getField(handler, "mass");
140 handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
141 assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
145 * Method: getComponent()
147 * @throws Exception thrown if something goes awry
149 public void testGetComponent() throws Exception {
150 assertTrue(new MassObjectHandler(new BodyTube(), new WarningSet()).getComponent() instanceof MassComponent);
154 * Method: getMaterialType()
156 * @throws Exception thrown if something goes awry
158 public void testGetMaterialType() throws Exception {
159 assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());