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