DGP - added isCompatible checks for all Rocksim components
[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.file.simplesax.PlainTextHandler;
10 import net.sf.openrocket.material.Material;
11 import net.sf.openrocket.rocketcomponent.BodyTube;
12 import net.sf.openrocket.rocketcomponent.InnerTube;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14
15 import java.util.HashMap;
16
17 /**
18  * InnerBodyTubeHandler Tester.
19  *
20  */
21 public class InnerBodyTubeHandlerTest extends RocksimTestBase {
22
23     /**
24      * The class under test.
25      */
26     public static final Class classUT = InnerBodyTubeHandler.class;
27
28     /**
29      * The test class (this class).
30      */
31     public static final Class testClass = InnerBodyTubeHandlerTest.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(InnerBodyTubeHandlerTest.class);
40     }
41
42     /**
43      * Test constructor.
44      *
45      * @param name the name of the test to run.
46      */
47     public InnerBodyTubeHandlerTest(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 InnerBodyTubeHandler(null, new WarningSet());
75             fail("Should have thrown IllegalArgumentException");
76         }
77         catch (IllegalArgumentException iae) {
78             //success
79         }
80
81         BodyTube tube = new BodyTube();
82         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube, new WarningSet());
83         InnerTube component = (InnerTube) getField(handler, "bodyTube");
84         assertContains(component, tube.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 InnerBodyTubeHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
94         assertNotNull(new InnerBodyTubeHandler(new BodyTube(), 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         BodyTube tube = new BodyTube();
105         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube, new WarningSet());
106         InnerTube component = (InnerTube) 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("Name", attributes, "Test Name", warnings);
158         assertEquals("Test Name", component.getName());
159     }
160     
161     /**
162      * Method: setRelativePosition(RocketComponent.Position position)
163      *
164      * @throws Exception thrown if something goes awry
165      */
166     public void testSetRelativePosition() throws Exception {
167         BodyTube tube = new BodyTube();
168         InnerBodyTubeHandler handler = new InnerBodyTubeHandler(tube, new WarningSet());
169         InnerTube component = (InnerTube) getField(handler, "bodyTube");
170         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
171         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
172     }
173
174     /**
175      * Method: getComponent()
176      *
177      * @throws Exception thrown if something goes awry
178      */
179     public void testGetComponent() throws Exception {
180         assertTrue(new InnerBodyTubeHandler(new BodyTube(), new WarningSet()).getComponent() instanceof InnerTube);
181     }
182
183     /**
184      * Method: getMaterialType()
185      *
186      * @throws Exception thrown if something goes awry
187      */
188     public void testGetMaterialType() throws Exception {
189         assertEquals(Material.Type.BULK, new InnerBodyTubeHandler(new BodyTube(), new WarningSet()).getMaterialType());
190     }
191
192
193
194
195 }