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