logging and unit test updates
[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.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.ExternalComponent;
14 import net.sf.openrocket.rocketcomponent.LaunchLug;
15 import net.sf.openrocket.rocketcomponent.RocketComponent;
16
17 import java.util.HashMap;
18
19 /**
20  * LaunchLugHandler Tester.
21  *
22  */
23 public class LaunchLugHandlerTest extends RocksimTestBase {
24
25     /**
26      * The class under test.
27      */
28     public static final Class classUT = LaunchLugHandler.class;
29
30     /**
31      * The test class (this class).
32      */
33     public static final Class testClass = LaunchLugHandlerTest.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(LaunchLugHandlerTest.class);
42     }
43
44     /**
45      * Test constructor.
46      *
47      * @param name the name of the test to run.
48      */
49     public LaunchLugHandlerTest(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      * Method: constructor
69      *
70      * @throws Exception thrown if something goes awry
71      */
72     public void testConstructor() throws Exception {
73
74         try {
75             new LaunchLugHandler(null);
76             fail("Should have thrown IllegalArgumentException");
77         }
78         catch (IllegalArgumentException iae) {
79             //success
80         }
81
82         BodyTube tube = new BodyTube();
83         LaunchLugHandler handler = new LaunchLugHandler(tube);
84         LaunchLug component = (LaunchLug) getField(handler, "lug");
85         assertContains(component, tube.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 LaunchLugHandler(new BodyTube()).openElement(null, 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         LaunchLugHandler handler = new LaunchLugHandler(tube);
106         LaunchLug component = (LaunchLug) getField(handler, "lug");
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.getRadius());
112         handler.closeElement("OD", attributes, "0", warnings);
113         assertEquals(0d, component.getRadius());
114         handler.closeElement("OD", attributes, "75", warnings);
115         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getRadius());
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("FinishCode", attributes, "-1", warnings);
141         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
142         handler.closeElement("FinishCode", attributes, "100", warnings);
143         assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
144         handler.closeElement("FinishCode", attributes, "foo", warnings);
145         assertEquals(1, warnings.size());
146         warnings.clear();
147
148         handler.closeElement("Name", attributes, "Test Name", warnings);
149         assertEquals("Test Name", component.getName());
150     }
151     
152     /**
153      * Method: setRelativePosition(RocketComponent.Position position)
154      *
155      * @throws Exception thrown if something goes awry
156      */
157     public void testSetRelativePosition() throws Exception {
158         BodyTube tube = new BodyTube();
159         LaunchLugHandler handler = new LaunchLugHandler(tube);
160         LaunchLug component = (LaunchLug) getField(handler, "lug");
161         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
162         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
163     }
164
165     /**
166      * Method: getComponent()
167      *
168      * @throws Exception thrown if something goes awry
169      */
170     public void testGetComponent() throws Exception {
171         assertTrue(new LaunchLugHandler(new BodyTube()).getComponent() instanceof LaunchLug);
172     }
173
174     /**
175      * Method: getMaterialType()
176      *
177      * @throws Exception thrown if something goes awry
178      */
179     public void testGetMaterialType() throws Exception {
180         assertEquals(Material.Type.BULK, new LaunchLugHandler(new BodyTube()).getMaterialType());
181     }
182
183
184 }