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