36f17c11ac38d9d46dc6df1f764f140a73b6ec79
[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.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;
14
15 import java.util.HashMap;
16
17 /**
18  * MassObjectHandler Tester.
19  *
20  */
21 public class MassObjectHandlerTest extends RocksimTestBase {
22
23     /**
24      * The class under test.
25      */
26     public static final Class classUT = MassObjectHandler.class;
27
28     /**
29      * The test class (this class).
30      */
31     public static final Class testClass = MassObjectHandlerTest.class;
32
33     /**
34      * Create a test suite of all tests within this test class.
35      *
36      * @return a suite of tests
37      */
38     public static Test suite() {
39         return new TestSuite(MassObjectHandlerTest.class);
40     }
41
42     /**
43      * Test constructor.
44      *
45      * @param name the name of the test to run.
46      */
47     public MassObjectHandlerTest(String name) {
48         super(name);
49     }
50
51     /**
52      * Setup the fixture.
53      */
54     public void setUp() throws Exception {
55         super.setUp();
56     }
57
58     /**
59      * Teardown the fixture.
60      */
61     public void tearDown() throws Exception {
62         super.tearDown();
63     }
64
65     /**
66      * Method: constructor
67      *
68      * @throws Exception thrown if something goes awry
69      */
70     public void testConstructor() throws Exception {
71
72         try {
73             new MassObjectHandler(null, new WarningSet());
74             fail("Should have thrown IllegalArgumentException");
75         }
76         catch (IllegalArgumentException iae) {
77             //success
78         }
79
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());
84     }
85
86     /**
87      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
88      *
89      * @throws Exception thrown if something goes awry
90      */
91     public void testOpenElement() throws Exception {
92         assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
93     }
94
95     /**
96      *
97      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
98      *
99      * @throws Exception  thrown if something goes awry
100      */
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();
105
106         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
107         MassComponent component = (MassComponent) getField(handler, "mass");
108
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());
119         warnings.clear();
120
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());
127         warnings.clear();
128
129     }
130     
131     /**
132      * Method: setRelativePosition(RocketComponent.Position position)
133      *
134      * @throws Exception thrown if something goes awry
135      */
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());
142     }
143
144     /**
145      * Method: getComponent()
146      *
147      * @throws Exception thrown if something goes awry
148      */
149     public void testGetComponent() throws Exception {
150         assertTrue(new MassObjectHandler(new BodyTube(), new WarningSet()).getComponent() instanceof MassComponent);
151     }
152
153     /**
154      * Method: getMaterialType()
155      *
156      * @throws Exception thrown if something goes awry
157      */
158     public void testGetMaterialType() throws Exception {
159         assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
160     }
161
162
163 }