logging and unit test updates
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / StreamerHandlerTest.java
1 /*
2  * StreamerHandlerTest.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.RocketComponent;
14 import net.sf.openrocket.rocketcomponent.Streamer;
15
16 import java.util.HashMap;
17
18 /**
19  * StreamerHandler Tester.
20  */
21 public class StreamerHandlerTest extends RocksimTestBase {
22
23     /**
24      * The class under test.
25      */
26     public static final Class classUT = StreamerHandler.class;
27
28     /**
29      * The test class (this class).
30      */
31     public static final Class testClass = StreamerHandlerTest.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(StreamerHandlerTest.class);
40     }
41
42     /**
43      * Test constructor.
44      *
45      * @param name the name of the test to run.
46      */
47     public StreamerHandlerTest(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 StreamerHandler(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         StreamerHandler handler = new StreamerHandler(tube);
83         Streamer component = (Streamer) getField(handler, "streamer");
84         HashMap<String, String> attributes = new HashMap<String, String>();
85         WarningSet warnings = new WarningSet();
86
87         handler.closeElement("Width", attributes, "0", warnings);
88         assertEquals(0d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
89         handler.closeElement("Width", attributes, "10", warnings);
90         assertEquals(10d/ RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripWidth());
91         handler.closeElement("Width", attributes, "foo", warnings);
92         assertEquals(1, warnings.size());
93         warnings.clear();
94         
95         handler.closeElement("Len", attributes, "-1", warnings);
96         assertEquals(0d, component.getStripLength());
97         handler.closeElement("Len", attributes, "10", warnings);
98         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
99         handler.closeElement("Len", attributes, "10.0", warnings);
100         assertEquals(10d / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH, component.getStripLength());
101         handler.closeElement("Len", attributes, "foo", warnings);
102         assertEquals(1, warnings.size());
103         warnings.clear();
104
105         handler.closeElement("Name", attributes, "Test Name", warnings);
106         assertEquals("Test Name", component.getName());
107         
108         handler.closeElement("DragCoefficient", attributes, "0.94", warnings);
109         assertEquals(0.94d, component.getCD());
110         handler.closeElement("DragCoefficient", attributes, "-0.94", warnings);
111         assertEquals(-0.94d, component.getCD());
112         handler.closeElement("DragCoefficient", attributes, "foo", warnings);
113         assertEquals(1, warnings.size());
114         warnings.clear();
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 StreamerHandler(null);
127             fail("Should have thrown IllegalArgumentException");
128         }
129         catch (IllegalArgumentException iae) {
130             //success
131         }
132
133         BodyTube tube = new BodyTube();
134         StreamerHandler handler = new StreamerHandler(tube);
135         Streamer component = (Streamer) getField(handler, "streamer");
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         StreamerHandler handler = new StreamerHandler(tube);
147         Streamer component = (Streamer) getField(handler, "streamer");
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 StreamerHandler(new BodyTube()).getComponent() instanceof Streamer);
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.SURFACE, new StreamerHandler(new BodyTube()).getMaterialType());
168     }
169
170     /**
171      * Method: endHandler()
172      *
173      * @throws Exception thrown if something goes awry
174      */
175     public void testEndHandler() throws Exception {
176         BodyTube tube = new BodyTube();
177         StreamerHandler handler = new StreamerHandler(tube);
178         Streamer component = (Streamer) getField(handler, "streamer");
179         HashMap<String, String> attributes = new HashMap<String, String>();
180         WarningSet warnings = new WarningSet();
181
182         handler.closeElement("Xb", attributes, "-10", warnings);
183         handler.closeElement("LocationMode", attributes, "1", warnings);
184         handler.endHandler("Streamer", attributes, null, warnings);
185         assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
186         assertEquals(component.getPositionValue(), -10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);  
187         
188         handler.closeElement("Xb", attributes, "-10", warnings);
189         handler.closeElement("LocationMode", attributes, "2", warnings);
190         handler.endHandler("Streamer", attributes, null, warnings);
191         assertEquals(RocketComponent.Position.BOTTOM, component.getRelativePosition());
192         assertEquals(component.getPositionValue(), 10d/RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);  
193     }
194     
195 }