DGP - added isCompatible checks for all Rocksim components
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / RingHandlerTest.java
1 /*
2  * RingHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import net.sf.openrocket.aerodynamics.WarningSet;
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.CenteringRing;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14
15 import java.util.HashMap;
16
17 /**
18  * RingHandler Tester.
19  */
20 public class RingHandlerTest extends RocksimTestBase {
21
22     /**
23      * The class under test.
24      */
25     public static final Class classUT = RingHandler.class;
26
27     /**
28      * The test class (this class).
29      */
30     public static final Class testClass = RingHandlerTest.class;
31
32     /**
33      * Create a test suite of all tests within this test class.
34      *
35      * @return a suite of tests
36      */
37     public static Test suite() {
38         return new TestSuite(RingHandlerTest.class);
39     }
40
41     /**
42      * Test constructor.
43      *
44      * @param name the name of the test to run.
45      */
46     public RingHandlerTest(String name) {
47         super(name);
48     }
49
50     /**
51      * Setup the fixture.
52      */
53     public void setUp() throws Exception {
54         super.setUp();
55     }
56
57     /**
58      * Teardown the fixture.
59      */
60     public void tearDown() throws Exception {
61         super.tearDown();
62     }
63
64     /**
65      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
66      *
67      * @throws Exception thrown if something goes awry
68      */
69     public void testOpenElement() throws Exception {
70         assertEquals(PlainTextHandler.INSTANCE, new RingHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
71     }
72
73     /**
74      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
75      *
76      * @throws Exception thrown if something goes awry
77      */
78     public void testCloseElement() throws Exception {
79
80         BodyTube tube = new BodyTube();
81         RingHandler handler = new RingHandler(tube, new WarningSet());
82         CenteringRing component = (CenteringRing) getField(handler, "ring");
83         HashMap<String, String> attributes = new HashMap<String, String>();
84         WarningSet warnings = new WarningSet();
85
86         handler.closeElement("OD", attributes, "0", warnings);
87         assertEquals(0d, component.getOuterRadius());
88         handler.closeElement("OD", attributes, "75", warnings);
89         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getOuterRadius());
90         handler.closeElement("OD", attributes, "foo", warnings);
91         assertEquals(1, warnings.size());
92         warnings.clear();
93
94         handler.closeElement("ID", attributes, "0", warnings);
95         assertEquals(0d, component.getInnerRadius());
96         handler.closeElement("ID", attributes, "75", warnings);
97         assertEquals(75d / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS, component.getInnerRadius());
98         handler.closeElement("ID", attributes, "foo", warnings);
99         assertEquals(1, warnings.size());
100         warnings.clear();
101
102         handler.closeElement("Len", attributes, "-1", warnings);
103         assertEquals(0d, component.getLength());
104         handler.closeElement("Len", attributes, "10", warnings);
105         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
106         handler.closeElement("Len", attributes, "10.0", warnings);
107         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getLength());
108         handler.closeElement("Len", attributes, "foo", warnings);
109         assertEquals(1, warnings.size());
110         warnings.clear();
111
112         handler.closeElement("Name", attributes, "Test Name", warnings);
113         assertEquals("Test Name", component.getName());
114     }
115
116
117     /**
118      * Method: constructor
119      *
120      * @throws Exception thrown if something goes awry
121      */
122     public void testConstructor() throws Exception {
123
124         try {
125             new RingHandler(null, new WarningSet());
126             fail("Should have thrown IllegalArgumentException");
127         }
128         catch (IllegalArgumentException iae) {
129             //success
130         }
131
132         BodyTube tube = new BodyTube();
133         RingHandler handler = new RingHandler(tube, new WarningSet());
134         CenteringRing component = (CenteringRing) getField(handler, "ring");
135         assertContains(component, tube.getChildren());
136     }
137
138     /**
139      * Method: setRelativePosition(RocketComponent.Position position)
140      *
141      * @throws Exception thrown if something goes awry
142      */
143     public void testSetRelativePosition() throws Exception {
144         BodyTube tube = new BodyTube();
145         RingHandler handler = new RingHandler(tube, new WarningSet());
146         CenteringRing component = (CenteringRing) getField(handler, "ring");
147         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
148         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
149     }
150
151     /**
152      * Method: getComponent()
153      *
154      * @throws Exception thrown if something goes awry
155      */
156     public void testGetComponent() throws Exception {
157         assertTrue(new RingHandler(new BodyTube(), new WarningSet()).getComponent() instanceof CenteringRing);
158     }
159
160     /**
161      * Method: getMaterialType()
162      *
163      * @throws Exception thrown if something goes awry
164      */
165     public void testGetMaterialType() throws Exception {
166         assertEquals(Material.Type.BULK, new RingHandler(new BodyTube(), new WarningSet()).getMaterialType());
167     }
168
169
170 }