create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / file / rocksim / importt / BodyTubeHandlerTest.java
1 /*
2  * BodyTubeHandlerTest.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.ExternalComponent;
12 import net.sf.openrocket.rocketcomponent.Stage;
13 import org.junit.Assert;
14 import org.junit.Test;
15
16 import java.util.HashMap;
17
18 /**
19  * BodyTubeHandler Tester.
20  *
21  */
22 public class BodyTubeHandlerTest extends RocksimTestBase {
23
24     /**
25      * Method: constructor
26      *
27      * @throws Exception thrown if something goes awry
28      */
29     @Test
30     public void testConstructor() throws Exception {
31
32         try {
33             new BodyTubeHandler(null, new WarningSet());
34             Assert.fail("Should have thrown IllegalArgumentException");
35         }
36         catch (IllegalArgumentException iae) {
37             //success
38         }
39
40         Stage stage = new Stage();
41         BodyTubeHandler handler = new BodyTubeHandler(stage, new WarningSet());
42         BodyTube component = (BodyTube) getField(handler, "bodyTube");
43         assertContains(component, stage.getChildren());
44     }
45
46     /**
47      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
48      *
49      * @throws Exception thrown if something goes awry
50      */
51     @Test
52     public void testOpenElement() throws Exception {
53         Assert.assertEquals(PlainTextHandler.INSTANCE, new BodyTubeHandler(new Stage(), new WarningSet()).openElement(null, null, null));
54         Assert.assertNotNull(new BodyTubeHandler(new Stage(), new WarningSet()).openElement("AttachedParts", null, null));
55     }
56
57     /**
58      *
59      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
60      *
61      * @throws Exception  thrown if something goes awry
62      */
63     @Test
64     public void testCloseElement() throws Exception {
65         Stage stage = new Stage();
66         BodyTubeHandler handler = new BodyTubeHandler(stage, new WarningSet());
67         BodyTube component = (BodyTube) getField(handler, "bodyTube");
68         HashMap<String, String> attributes = new HashMap<String, String>();
69         WarningSet warnings = new WarningSet();
70
71         handler.closeElement("OD", attributes, "-1", warnings);
72         Assert.assertEquals(0d, component.getInnerRadius(), 0.001);
73         handler.closeElement("OD", attributes, "0", warnings);
74         Assert.assertEquals(0d, component.getInnerRadius(), 0.001);
75         handler.closeElement("OD", attributes, "75", warnings);
76         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius(), 0.001);
77         handler.closeElement("OD", attributes, "foo", warnings);
78         Assert.assertEquals(1, warnings.size());
79         warnings.clear();
80
81         handler.closeElement("ID", attributes, "-1", warnings);
82         Assert.assertEquals(0d, component.getInnerRadius(), 0.001);
83         handler.closeElement("ID", attributes, "0", warnings);
84         Assert.assertEquals(0d, component.getInnerRadius(), 0.001);
85         handler.closeElement("ID", attributes, "75", warnings);
86         Assert.assertEquals(75d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius(), 0.001);
87         handler.closeElement("ID", attributes, "foo", warnings);
88         Assert.assertEquals(1, warnings.size());
89         warnings.clear();
90
91         handler.closeElement("Len", attributes, "-1", warnings);
92         Assert.assertEquals(0d, component.getLength(), 0.001);
93         handler.closeElement("Len", attributes, "10", warnings);
94         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
95         handler.closeElement("Len", attributes, "10.0", warnings);
96         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength(), 0.001);
97         handler.closeElement("Len", attributes, "foo", warnings);
98         Assert.assertEquals(1, warnings.size());
99         warnings.clear();
100
101         handler.closeElement("IsMotorMount", attributes, "1", warnings);
102         Assert.assertTrue(component.isMotorMount());
103         handler.closeElement("IsMotorMount", attributes, "0", warnings);
104         Assert.assertFalse(component.isMotorMount());
105         handler.closeElement("IsMotorMount", attributes, "foo", warnings);
106         Assert.assertFalse(component.isMotorMount());
107
108         handler.closeElement("EngineOverhang", attributes, "-1", warnings);
109         Assert.assertEquals(-1d/ RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang(), 0.001);
110         handler.closeElement("EngineOverhang", attributes, "10", warnings);
111         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang(), 0.001);
112         handler.closeElement("EngineOverhang", attributes, "10.0", warnings);
113         Assert.assertEquals(10d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH, component.getMotorOverhang(), 0.001);
114         handler.closeElement("EngineOverhang", attributes, "foo", warnings);
115         Assert.assertEquals(1, warnings.size());
116         warnings.clear();
117
118         handler.closeElement("FinishCode", attributes, "-1", warnings);
119         Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
120         handler.closeElement("FinishCode", attributes, "100", warnings);
121         Assert.assertEquals(ExternalComponent.Finish.NORMAL, component.getFinish());
122         handler.closeElement("FinishCode", attributes, "foo", warnings);
123         Assert.assertEquals(1, warnings.size());
124         warnings.clear();
125
126         handler.closeElement("Name", attributes, "Test Name", warnings);
127         Assert.assertEquals("Test Name", component.getName());
128     }
129     
130     /**
131      * Method: getComponent()
132      *
133      * @throws Exception thrown if something goes awry
134      */
135     @Test
136     public void testGetComponent() throws Exception {
137         Assert.assertTrue(new BodyTubeHandler(new Stage(), new WarningSet()).getComponent() instanceof BodyTube);
138     }
139
140     /**
141      * Method: getMaterialType()
142      *
143      * @throws Exception thrown if something goes awry
144      */
145     @Test
146     public void testGetMaterialType() throws Exception {
147         Assert.assertEquals(Material.Type.BULK, new BodyTubeHandler(new Stage(), new WarningSet()).getMaterialType());
148     }
149
150 }