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