logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / ParachuteHandlerTest.java
1 /*
2  * ParachuteHandlerTest.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.Parachute;
14 import net.sf.openrocket.rocketcomponent.RocketComponent;
15
16 import java.util.HashMap;
17
18 /**
19  * ParachuteHandler Tester.
20  */
21 public class ParachuteHandlerTest extends RocksimTestBase {
22
23     /**
24      * The class under test.
25      */
26     public static final Class classUT = ParachuteHandler.class;
27
28     /**
29      * The test class (this class).
30      */
31     public static final Class testClass = ParachuteHandlerTest.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(ParachuteHandlerTest.class);
40     }
41
42     /**
43      * Test constructor.
44      *
45      * @param name the name of the test to run.
46      */
47     public ParachuteHandlerTest(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      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
67      *
68      * @throws Exception thrown if something goes awry
69      */
70     public void testOpenElement() throws Exception {
71         assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(new BodyTube()).openElement(null, null, null));
72     }
73
74     /**
75      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
76      *
77      * @throws Exception thrown if something goes awry
78      */
79     public void testCloseElement() throws Exception {
80
81         BodyTube tube = new BodyTube();
82         ParachuteHandler handler = new ParachuteHandler(tube);
83         Parachute component = (Parachute) getField(handler, "chute");
84         HashMap<String, String> attributes = new HashMap<String, String>();
85         WarningSet warnings = new WarningSet();
86
87         handler.closeElement("Name", attributes, "Test Name", warnings);
88         assertEquals("Test Name", component.getName());
89
90         handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
91         assertEquals(0.94d, component.getCD());
92         handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
93         assertEquals(-0.94d, component.getCD());
94         handler.closeElement("DragCoefficient", attributes, "foo", warnings);
95         assertEquals(1, warnings.size());
96         warnings.clear();
97
98         handler.closeElement("Dia", attributes, "-1", warnings);
99         assertEquals(-1d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
100         handler.closeElement("Dia", attributes, "10", warnings);
101         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter());
102         handler.closeElement("Dia", attributes, "foo", warnings);
103         assertEquals(1, warnings.size());
104         warnings.clear();
105
106         handler.closeElement("ShroudLineCount", attributes, "-1", warnings);
107         assertEquals(0, component.getLineCount());
108         handler.closeElement("ShroudLineCount", attributes, "10", warnings);
109         assertEquals(10, component.getLineCount());
110         handler.closeElement("ShroudLineCount", attributes, "foo", warnings);
111         assertEquals(1, warnings.size());
112         warnings.clear();
113
114         handler.closeElement("ShroudLineLen", attributes, "-1", warnings);
115         assertEquals(0d, component.getLineLength());
116         handler.closeElement("ShroudLineLen", attributes, "10", warnings);
117         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLineLength());
118         handler.closeElement("ShroudLineLen", attributes, "foo", warnings);
119         assertEquals(1, warnings.size());
120         warnings.clear();
121
122     }
123
124     /**
125      * Method: constructor
126      *
127      * @throws Exception thrown if something goes awry
128      */
129     public void testConstructor() throws Exception {
130
131         try {
132             new ParachuteHandler(null);
133             fail("Should have thrown IllegalArgumentException");
134         }
135         catch (IllegalArgumentException iae) {
136             //success
137         }
138
139         BodyTube tube = new BodyTube();
140         ParachuteHandler handler = new ParachuteHandler(tube);
141         Parachute component = (Parachute) getField(handler, "chute");
142         assertContains(component, tube.getChildren());
143     }
144
145     /**
146      * Method: setRelativePosition(RocketComponent.Position position)
147      *
148      * @throws Exception thrown if something goes awry
149      */
150     public void testSetRelativePosition() throws Exception {
151         BodyTube tube = new BodyTube();
152         ParachuteHandler handler = new ParachuteHandler(tube);
153         Parachute component = (Parachute) getField(handler, "chute");
154         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
155         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
156     }
157
158     /**
159      * Method: getComponent()
160      *
161      * @throws Exception thrown if something goes awry
162      */
163     public void testGetComponent() throws Exception {
164         assertTrue(new ParachuteHandler(new BodyTube()).getComponent() instanceof Parachute);
165     }
166
167     /**
168      * Method: getMaterialType()
169      *
170      * @throws Exception thrown if something goes awry
171      */
172     public void testGetMaterialType() throws Exception {
173         assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube()).getMaterialType());
174     }
175
176     /**
177      * Method: endHandler()
178      *
179      * @throws Exception thrown if something goes awry
180      */
181     public void testEndHandler() throws Exception {
182         BodyTube tube = new BodyTube();
183         ParachuteHandler handler = new ParachuteHandler(tube);
184         Parachute component = (Parachute) getField(handler, "chute");
185         HashMap<String, String> attributes = new HashMap<String, String>();
186         WarningSet warnings = new WarningSet();
187
188         handler.closeElement("Xb", attributes, "-10", warnings);
189         handler.closeElement("LocationMode", attributes, "1", warnings);
190         handler.endHandler("Parachute", attributes, null, warnings);
191         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
192         assertEquals(component.getPositionValue(), -10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
193
194         handler.closeElement("Xb", attributes, "-10", warnings);
195         handler.closeElement("LocationMode", attributes, "2", warnings);
196         handler.endHandler("Parachute", attributes, null, warnings);
197         assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
198         assertEquals(component.getPositionValue(), 10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
199     }
200
201
202 }