logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / InnerBodyTubeHandlerTest.java
1 /*
2  * InnerBodyTubeHandlerTest.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.database.Databases;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.InnerTube;
14 import net.sf.openrocket.rocketcomponent.RocketComponent;
15 import net.sf.openrocket.rocketcomponent.Stage;
16
17 import java.util.HashMap;
18
19 /**
20  * InnerBodyTubeHandler Tester.
21  *
22  */
23 public class InnerBodyTubeHandlerTest extends RocksimTestBase {
24
25     /**
26      * The class under test.
27      */
28     public static final Class classUT = InnerBodyTubeHandler.class;
29
30     /**
31      * The test class (this class).
32      */
33     public static final Class testClass = InnerBodyTubeHandlerTest.class;
34
35     /**
36      * Create a test suite of all tests within this test class.
37      *
38      * @return a suite of tests
39      */
40     public static Test suite() {
41         return new TestSuite(InnerBodyTubeHandlerTest.class);
42     }
43
44     /**
45      * Test constructor.
46      *
47      * @param name the name of the test to run.
48      */
49     public InnerBodyTubeHandlerTest(String name) {
50         super(name);
51     }
52
53     /**
54      * Setup the fixture.
55      */
56     public void setUp() throws Exception {
57         super.setUp();
58     }
59
60     /**
61      * Teardown the fixture.
62      */
63     public void tearDown() throws Exception {
64         super.tearDown();
65     }
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 InnerBodyTubeHandler(null);
77             fail("Should have thrown IllegalArgumentException");
78         }
79         catch (IllegalArgumentException iae) {
80             //success
81         }
82
83         BodyTube tube = new BodyTube();
84         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube);
85         InnerTube component = (InnerTube) getField(handler, "bodyTube");
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 InnerBodyTubeHandler(new BodyTube()).openElement(null, null, null));
96         assertNotNull(new InnerBodyTubeHandler(new BodyTube()).openElement("AttachedParts", null, null));
97     }
98
99     /**
100      *
101      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
102      *
103      * @throws Exception  thrown if something goes awry
104      */
105     public void testCloseElement() throws Exception {
106         BodyTube tube = new BodyTube();
107         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube);
108         InnerTube component = (InnerTube) getField(handler, "bodyTube");
109         HashMap<String, String> attributes = new HashMap<String, String>();
110         WarningSet warnings = new WarningSet();
111
112         handler.closeElement("OD", attributes, "-1", warnings);
113         assertEquals(0d, component.getInnerRadius());
114         handler.closeElement("OD", attributes, "0", warnings);
115         assertEquals(0d, component.getInnerRadius());
116         handler.closeElement("OD", attributes, "75", warnings);
117         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius());
118         handler.closeElement("OD", attributes, "foo", warnings);
119         assertEquals(1, warnings.size());
120         warnings.clear();
121
122         handler.closeElement("ID", attributes, "-1", warnings);
123         assertEquals(0d, component.getInnerRadius());
124         handler.closeElement("ID", attributes, "0", warnings);
125         assertEquals(0d, component.getInnerRadius());
126         handler.closeElement("ID", attributes, "75", warnings);
127         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius());
128         handler.closeElement("ID", attributes, "foo", warnings);
129         assertEquals(1, warnings.size());
130         warnings.clear();
131
132         handler.closeElement("Len", attributes, "-1", warnings);
133         assertEquals(0d, component.getLength());
134         handler.closeElement("Len", attributes, "10", warnings);
135         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
136         handler.closeElement("Len", attributes, "10.0", warnings);
137         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
138         handler.closeElement("Len", attributes, "foo", warnings);
139         assertEquals(1, warnings.size());
140         warnings.clear();
141
142         handler.closeElement("IsMotorMount", attributes, "1", warnings);
143         assertTrue(component.isMotorMount());
144         handler.closeElement("IsMotorMount", attributes, "0", warnings);
145         assertFalse(component.isMotorMount());
146         handler.closeElement("IsMotorMount", attributes, "foo", warnings);
147         assertFalse(component.isMotorMount());
148
149         handler.closeElement("EngineOverhang", attributes, "-1", warnings);
150         assertEquals(-1d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang());
151         handler.closeElement("EngineOverhang", attributes, "10", warnings);
152         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang());
153         handler.closeElement("EngineOverhang", attributes, "10.0", warnings);
154         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang());
155         handler.closeElement("EngineOverhang", attributes, "foo", warnings);
156         assertEquals(1, warnings.size());
157         warnings.clear();
158
159         handler.closeElement("Name", attributes, "Test Name", warnings);
160         assertEquals("Test Name", component.getName());
161     }
162     
163     /**
164      * Method: setRelativePosition(RocketComponent.Position position)
165      *
166      * @throws Exception thrown if something goes awry
167      */
168     public void testSetRelativePosition() throws Exception {
169         BodyTube tube = new BodyTube();
170         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube);
171         InnerTube component = (InnerTube) getField(handler, "bodyTube");
172         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
173         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
174     }
175
176     /**
177      * Method: getComponent()
178      *
179      * @throws Exception thrown if something goes awry
180      */
181     public void testGetComponent() throws Exception {
182         assertTrue(new InnerBodyTubeHandler(new BodyTube()).getComponent() instanceof InnerTube);
183     }
184
185     /**
186      * Method: getMaterialType()
187      *
188      * @throws Exception thrown if something goes awry
189      */
190     public void testGetMaterialType() throws Exception {
191         assertEquals(Material.Type.BULK, new InnerBodyTubeHandler(new BodyTube()).getMaterialType());
192     }
193
194
195
196
197 }