create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / file / rocksim / importt / ParachuteHandlerTest.java
1 /*
2  * ParachuteHandlerTest.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.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.Parachute;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import org.junit.Assert;
14
15 import java.util.HashMap;
16
17 /**
18  * ParachuteHandler Tester.
19  */
20 public class ParachuteHandlerTest 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     @org.junit.Test
28     public void testOpenElement() throws Exception {
29         Assert.assertEquals(PlainTextHandler.INSTANCE, new ParachuteHandler(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     @org.junit.Test
38     public void testCloseElement() throws Exception {
39
40         BodyTube tube = new BodyTube();
41         ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
42         Parachute component = (Parachute) getField(handler, "chute");
43         HashMap<String, String> attributes = new HashMap<String, String>();
44         WarningSet warnings = new WarningSet();
45
46         handler.closeElement("Name", attributes, "Test Name", warnings);
47         Assert.assertEquals("Test Name", component.getName());
48
49         handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
50         Assert.assertEquals(0.94d, component.getCD(), 0.001);
51         handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
52         Assert.assertEquals(-0.94d, component.getCD(), 0.001);
53         handler.closeElement("DragCoefficient", attributes, "foo", warnings);
54         Assert.assertEquals(1, warnings.size());
55         warnings.clear();
56
57         handler.closeElement("Dia", attributes, "-1", warnings);
58         Assert.assertEquals(-1d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter(), 0.001);
59         handler.closeElement("Dia", attributes, "10", warnings);
60         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getDiameter(), 0.001);
61         handler.closeElement("Dia", attributes, "foo", warnings);
62         Assert.assertEquals(1, warnings.size());
63         warnings.clear();
64
65         handler.closeElement("ShroudLineCount", attributes, "-1", warnings);
66         Assert.assertEquals(0, component.getLineCount());
67         handler.closeElement("ShroudLineCount", attributes, "10", warnings);
68         Assert.assertEquals(10, component.getLineCount());
69         handler.closeElement("ShroudLineCount", attributes, "foo", warnings);
70         Assert.assertEquals(1, warnings.size());
71         warnings.clear();
72
73         handler.closeElement("ShroudLineLen", attributes, "-1", warnings);
74         Assert.assertEquals(0d, component.getLineLength(), 0.001);
75         handler.closeElement("ShroudLineLen", attributes, "10", warnings);
76         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLineLength(), 0.001);
77         handler.closeElement("ShroudLineLen", attributes, "foo", warnings);
78         Assert.assertEquals(1, warnings.size());
79         warnings.clear();
80
81     }
82
83     /**
84      * Method: constructor
85      *
86      * @throws Exception thrown if something goes awry
87      */
88     @org.junit.Test
89     public void testConstructor() throws Exception {
90
91         try {
92             new ParachuteHandler(null, new WarningSet());
93             Assert.fail("Should have thrown IllegalArgumentException");
94         }
95         catch (IllegalArgumentException iae) {
96             //success
97         }
98
99         BodyTube tube = new BodyTube();
100         ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
101         Parachute component = (Parachute) getField(handler, "chute");
102         assertContains(component, tube.getChildren());
103     }
104
105     /**
106      * Method: setRelativePosition(RocketComponent.Position position)
107      *
108      * @throws Exception thrown if something goes awry
109      */
110     @org.junit.Test
111     public void testSetRelativePosition() throws Exception {
112         BodyTube tube = new BodyTube();
113         ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
114         Parachute component = (Parachute) getField(handler, "chute");
115         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
116         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
117     }
118
119     /**
120      * Method: getComponent()
121      *
122      * @throws Exception thrown if something goes awry
123      */
124     @org.junit.Test
125     public void testGetComponent() throws Exception {
126         Assert.assertTrue(new ParachuteHandler(new BodyTube(), new WarningSet()).getComponent() instanceof Parachute);
127     }
128
129     /**
130      * Method: getMaterialType()
131      *
132      * @throws Exception thrown if something goes awry
133      */
134     @org.junit.Test
135     public void testGetMaterialType() throws Exception {
136         Assert.assertEquals(Material.Type.SURFACE, new ParachuteHandler(new BodyTube(), new WarningSet()).getMaterialType());
137     }
138
139     /**
140      * Method: endHandler()
141      *
142      * @throws Exception thrown if something goes awry
143      */
144     @org.junit.Test
145     public void testEndHandler() throws Exception {
146         BodyTube tube = new BodyTube();
147         ParachuteHandler handler = new ParachuteHandler(tube, new WarningSet());
148         Parachute component = (Parachute) getField(handler, "chute");
149         HashMap<String, String> attributes = new HashMap<String, String>();
150         WarningSet warnings = new WarningSet();
151
152         handler.closeElement("Xb", attributes, "-10", warnings);
153         handler.closeElement("LocationMode", attributes, "1", warnings);
154         handler.endHandler("Parachute", attributes, null, warnings);
155         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
156         Assert.assertEquals(component.getPositionValue(), -10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
157
158         handler.closeElement("Xb", attributes, "-10", warnings);
159         handler.closeElement("LocationMode", attributes, "2", warnings);
160         handler.endHandler("Parachute", attributes, null, warnings);
161         Assert.assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
162         Assert.assertEquals(component.getPositionValue(), 10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, 0.001);
163     }
164 }