create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / file / rocksim / importt / StreamerHandlerTest.java
1 /*
2  * StreamerHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim.importt;
5
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
8 import net.sf.openrocket.file.rocksim.RocksimDensityType;
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.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.Streamer;
14 import org.junit.Assert;
15 import org.junit.Test;
16
17 import java.util.HashMap;
18
19 /**
20  * StreamerHandler Tester.
21  */
22 public class StreamerHandlerTest extends RocksimTestBase {
23
24     /**
25      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
26      *
27      * @throws Exception thrown if something goes awry
28      */
29     @Test
30     public void testOpenElement() throws Exception {
31         Assert.assertEquals(PlainTextHandler.INSTANCE, new StreamerHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
32     }
33
34     /**
35      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
36      *
37      * @throws Exception thrown if something goes awry
38      */
39     @Test
40     public void testCloseElement() throws Exception {
41
42         BodyTube tube = new BodyTube();
43         StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
44         Streamer component = (Streamer) getField(handler, "streamer");
45         HashMap<String, String> attributes = new HashMap<String, String>();
46         WarningSet warnings = new WarningSet();
47
48         handler.closeElement("Width", attributes, "0", warnings);
49         Assert.assertEquals(0d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
50         handler.closeElement("Width", attributes, "10", warnings);
51         Assert.assertEquals(10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth(), 0.001);
52         handler.closeElement("Width", attributes, "foo", warnings);
53         Assert.assertEquals(1, warnings.size());
54         warnings.clear();
55
56         handler.closeElement("Len", attributes, "-1", warnings);
57         Assert.assertEquals(0d, component.getStripLength(), 0.001);
58         handler.closeElement("Len", attributes, "10", warnings);
59         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
60         handler.closeElement("Len", attributes, "10.0", warnings);
61         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength(), 0.001);
62         handler.closeElement("Len", attributes, "foo", warnings);
63         Assert.assertEquals(1, warnings.size());
64         warnings.clear();
65
66         handler.closeElement("Name", attributes, "Test Name", warnings);
67         Assert.assertEquals("Test Name", component.getName());
68
69         handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
70         Assert.assertEquals(0.94d, component.getCD(), 0.001);
71         handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
72         Assert.assertEquals(-0.94d, component.getCD(), 0.001);
73         handler.closeElement("DragCoefficient", attributes, "foo", warnings);
74         Assert.assertEquals(1, warnings.size());
75         warnings.clear();
76
77     }
78
79     /**
80      * Method: constructor
81      *
82      * @throws Exception thrown if something goes awry
83      */
84     @Test
85     public void testConstructor() throws Exception {
86
87         try {
88             new StreamerHandler(null, new WarningSet());
89             Assert.fail("Should have thrown IllegalArgumentException");
90         }
91         catch (IllegalArgumentException iae) {
92             //success
93         }
94
95         BodyTube tube = new BodyTube();
96         StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
97         Streamer component = (Streamer) getField(handler, "streamer");
98         assertContains(component, tube.getChildren());
99     }
100
101     /**
102      * Method: setRelativePosition(RocketComponent.Position position)
103      *
104      * @throws Exception thrown if something goes awry
105      */
106     @Test
107     public void testSetRelativePosition() throws Exception {
108         BodyTube tube = new BodyTube();
109         StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
110         Streamer component = (Streamer) getField(handler, "streamer");
111         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
112         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
113     }
114
115     /**
116      * Method: getComponent()
117      *
118      * @throws Exception thrown if something goes awry
119      */
120     @Test
121     public void testGetComponent() throws Exception {
122         Assert.assertTrue(new StreamerHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Streamer);
123     }
124
125     /**
126      * Method: getMaterialType()
127      *
128      * @throws Exception thrown if something goes awry
129      */
130     @Test
131     public void testGetMaterialType() throws Exception {
132         Assert.assertEquals(Material.Type.SURFACE, new StreamerHandler(new BodyTube(), new WarningSet()).getMaterialType());
133     }
134
135     /**
136      * Method: endHandler()
137      *
138      * @throws Exception thrown if something goes awry
139      */
140     @Test
141     public void testEndHandler() throws Exception {
142         BodyTube tube = new BodyTube();
143         StreamerHandler handler = new StreamerHandler(tube, new WarningSet());
144         Streamer component = (Streamer) getField(handler, "streamer");
145         HashMap<String, String> attributes = new HashMap<String, String>();
146         WarningSet warnings = new WarningSet();
147
148         handler.closeElement("Xb", attributes, "-10", warnings);
149         handler.closeElement("LocationMode", attributes, "1", warnings);
150         handler.endHandler("Streamer", attributes, null, warnings);
151         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
152         Assert.assertEquals(component.getPositionValue(), -10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
153
154         handler.closeElement("Xb", attributes, "-10", warnings);
155         handler.closeElement("LocationMode", attributes, "2", warnings);
156         handler.endHandler("Streamer", attributes, null, warnings);
157         Assert.assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
158         Assert.assertEquals(component.getPositionValue(), 10d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
159
160         handler.closeElement("Thickness", attributes, "0.02", warnings);
161         Assert.assertEquals(0.01848, handler.computeDensity(RocksimDensityType.ROCKSIM_BULK, 924d), 0.001);
162
163         //Test Density Type 0 (Bulk)
164         handler.closeElement("Density", attributes, "924.0", warnings);
165         handler.closeElement("DensityType", attributes, "0", warnings);
166         handler.endHandler("Streamer", attributes, null, warnings);
167         Assert.assertEquals(0.01848d, component.getMaterial().getDensity(), 0.001);
168
169         //Test Density Type 1 (Surface)
170         handler.closeElement("Density", attributes, "0.006685", warnings);
171         handler.closeElement("DensityType", attributes, "1", warnings);
172         handler.endHandler("Streamer", attributes, null, warnings);
173         Assert.assertTrue(Math.abs(0.06685d - component.getMaterial().getDensity()) < 0.00001);
174
175         //Test Density Type 2 (Line)
176         handler.closeElement("Density", attributes, "0.223225", warnings);
177         handler.closeElement("DensityType", attributes, "2", warnings);
178         handler.closeElement("Len", attributes, "3810.", warnings);
179         handler.closeElement("Width", attributes, "203.2", warnings);
180         handler.endHandler("Streamer", attributes, null, warnings);
181
182         Assert.assertEquals(1.728190092, component.getMass(), 0.001);
183
184     }
185
186 }