DGP - added isCompatible checks for all Rocksim components
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / LaunchLugHandlerTest.java
1 /*
2  * LaunchLugHandlerTest.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.LaunchLug;
14 import net.sf.openrocket.rocketcomponent.RocketComponent;
15
16 import java.util.HashMap;
17
18 /**
19  * LaunchLugHandler Tester.
20  *
21  */
22 public class LaunchLugHandlerTest extends RocksimTestBase {
23
24     /**
25      * The class under test.
26      */
27     public static final Class classUT = LaunchLugHandler.class;
28
29     /**
30      * The test class (this class).
31      */
32     public static final Class testClass = LaunchLugHandlerTest.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(LaunchLugHandlerTest.class);
41     }
42
43     /**
44      * Test constructor.
45      *
46      * @param name the name of the test to run.
47      */
48     public LaunchLugHandlerTest(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      * Method: constructor
68      *
69      * @throws Exception thrown if something goes awry
70      */
71     public void testConstructor() throws Exception {
72
73         try {
74             new LaunchLugHandler(null, new WarningSet());
75             fail("Should have thrown IllegalArgumentException");
76         }
77         catch (IllegalArgumentException iae) {
78             //success
79         }
80
81         BodyTube tube = new BodyTube();
82         LaunchLugHandler handler = new LaunchLugHandler(tube, new WarningSet());
83         LaunchLug component = (LaunchLug) getField(handler, "lug");
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 LaunchLugHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
94     }
95
96     /**
97      *
98      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
99      *
100      * @throws Exception  thrown if something goes awry
101      */
102     public void testCloseElement() throws Exception {
103         BodyTube tube = new BodyTube();
104         LaunchLugHandler handler = new LaunchLugHandler(tube, new WarningSet());
105         LaunchLug component = (LaunchLug) getField(handler, "lug");
106         HashMap<String, String> attributes = new HashMap<String, String>();
107         WarningSet warnings = new WarningSet();
108
109         handler.closeElement("OD", attributes, "-1", warnings);
110         assertEquals(0d, component.getRadius());
111         handler.closeElement("OD", attributes, "0", warnings);
112         assertEquals(0d, component.getRadius());
113         handler.closeElement("OD", attributes, "75", warnings);
114         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getRadius());
115         handler.closeElement("OD", attributes, "foo", warnings);
116         assertEquals(1, warnings.size());
117         warnings.clear();
118
119         handler.closeElement("ID", attributes, "-1", warnings);
120         assertEquals(0d, component.getInnerRadius());
121         handler.closeElement("ID", attributes, "0", warnings);
122         assertEquals(0d, component.getInnerRadius());
123         handler.closeElement("ID", attributes, "75", warnings);
124         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius());
125         handler.closeElement("ID", attributes, "foo", warnings);
126         assertEquals(1, warnings.size());
127         warnings.clear();
128
129         handler.closeElement("Len", attributes, "-1", warnings);
130         assertEquals(0d, component.getLength());
131         handler.closeElement("Len", attributes, "10", warnings);
132         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
133         handler.closeElement("Len", attributes, "10.0", warnings);
134         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
135         handler.closeElement("Len", attributes, "foo", warnings);
136         assertEquals(1, warnings.size());
137         warnings.clear();
138
139         handler.closeElement("FinishCode", attributes, "-1", warnings);
140         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
141         handler.closeElement("FinishCode", attributes, "100", warnings);
142         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
143         handler.closeElement("FinishCode", attributes, "foo", warnings);
144         assertEquals(1, warnings.size());
145         warnings.clear();
146
147         handler.closeElement("Name", attributes, "Test Name", warnings);
148         assertEquals("Test Name", component.getName());
149     }
150     
151     /**
152      * Method: setRelativePosition(RocketComponent.Position position)
153      *
154      * @throws Exception thrown if something goes awry
155      */
156     public void testSetRelativePosition() throws Exception {
157         BodyTube tube = new BodyTube();
158         LaunchLugHandler handler = new LaunchLugHandler(tube, new WarningSet());
159         LaunchLug component = (LaunchLug) getField(handler, "lug");
160         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
161         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
162     }
163
164     /**
165      * Method: getComponent()
166      *
167      * @throws Exception thrown if something goes awry
168      */
169     public void testGetComponent() throws Exception {
170         assertTrue(new LaunchLugHandler(new BodyTube(), new WarningSet()).getComponent() instanceof LaunchLug);
171     }
172
173     /**
174      * Method: getMaterialType()
175      *
176      * @throws Exception thrown if something goes awry
177      */
178     public void testGetMaterialType() throws Exception {
179         assertEquals(Material.Type.BULK, new LaunchLugHandler(new BodyTube(), new WarningSet()).getMaterialType());
180     }
181
182
183 }