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