logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / MassObjectHandlerTest.java
1 /*
2  * MassObjectHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
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 net.sf.openrocket.rocketcomponent.Stage;
14 import net.sf.openrocket.rocketcomponent.NoseCone;
15 import net.sf.openrocket.rocketcomponent.Transition;
16 import net.sf.openrocket.aerodynamics.WarningSet;
17
18 import java.util.HashMap;
19
20 /**
21  * MassObjectHandler Tester.
22  *
23  */
24 public class MassObjectHandlerTest extends RocksimTestBase {
25
26     /**
27      * The class under test.
28      */
29     public static final Class classUT = MassObjectHandler.class;
30
31     /**
32      * The test class (this class).
33      */
34     public static final Class testClass = MassObjectHandlerTest.class;
35
36     /**
37      * Create a test suite of all tests within this test class.
38      *
39      * @return a suite of tests
40      */
41     public static Test suite() {
42         return new TestSuite(MassObjectHandlerTest.class);
43     }
44
45     /**
46      * Test constructor.
47      *
48      * @param name the name of the test to run.
49      */
50     public MassObjectHandlerTest(String name) {
51         super(name);
52     }
53
54     /**
55      * Setup the fixture.
56      */
57     public void setUp() throws Exception {
58         super.setUp();
59     }
60
61     /**
62      * Teardown the fixture.
63      */
64     public void tearDown() throws Exception {
65         super.tearDown();
66     }
67
68     /**
69      * Method: constructor
70      *
71      * @throws Exception thrown if something goes awry
72      */
73     public void testConstructor() throws Exception {
74
75         try {
76             new MassObjectHandler(null);
77             fail("Should have thrown IllegalArgumentException");
78         }
79         catch (IllegalArgumentException iae) {
80             //success
81         }
82
83         BodyTube tube = new BodyTube();
84         MassObjectHandler handler = new MassObjectHandler(tube);
85         MassComponent component = (MassComponent) getField(handler, "mass");
86         assertContains(component, tube.getChildren());
87     }
88
89     /**
90      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
91      *
92      * @throws Exception thrown if something goes awry
93      */
94     public void testOpenElement() throws Exception {
95         assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube()).openElement(null, null, null));
96     }
97
98     /**
99      *
100      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
101      *
102      * @throws Exception  thrown if something goes awry
103      */
104     public void testCloseElement() throws Exception {
105         BodyTube tube = new BodyTube();
106         HashMap<String, String> attributes = new HashMap<String, String>();
107         WarningSet warnings = new WarningSet();
108
109         MassObjectHandler handler = new MassObjectHandler(tube);
110         MassComponent component = (MassComponent) getField(handler, "mass");
111
112         handler.closeElement("Len", attributes, "-1", warnings);
113         assertEquals(0d, component.getLength());
114         handler.closeElement("Len", attributes, "10", warnings);
115         assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH)
116                 , component.getLength());
117         handler.closeElement("Len", attributes, "10.0", warnings);
118         assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH)
119                 , component.getLength());
120         handler.closeElement("Len", attributes, "foo", warnings);
121         assertEquals(1, warnings.size());
122         warnings.clear();
123
124         handler.closeElement("KnownMass", attributes, "-1", warnings);
125         assertEquals(0d, component.getComponentMass());
126         handler.closeElement("KnownMass", attributes, "100", warnings);
127         assertEquals(100d / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS, component.getComponentMass());
128         handler.closeElement("KnownMass", attributes, "foo", warnings);
129         assertEquals(1, warnings.size());
130         warnings.clear();
131
132     }
133     
134     /**
135      * Method: setRelativePosition(RocketComponent.Position position)
136      *
137      * @throws Exception thrown if something goes awry
138      */
139     public void testSetRelativePosition() throws Exception {
140         BodyTube tube = new BodyTube();
141         MassObjectHandler handler = new MassObjectHandler(tube);
142         MassComponent component = (MassComponent) getField(handler, "mass");
143         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
144         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
145     }
146
147     /**
148      * Method: getComponent()
149      *
150      * @throws Exception thrown if something goes awry
151      */
152     public void testGetComponent() throws Exception {
153         assertTrue(new MassObjectHandler(new BodyTube()).getComponent() instanceof MassComponent);
154     }
155
156     /**
157      * Method: getMaterialType()
158      *
159      * @throws Exception thrown if something goes awry
160      */
161     public void testGetMaterialType() throws Exception {
162         assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube()).getMaterialType());
163     }
164
165
166 }