committed Doug's Rocksim loader
[debian/openrocket] / test / net / sf / openrocket / file / rocksim / FinSetHandlerTest.java
1 /*
2  * FinSetHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import net.sf.openrocket.aerodynamics.WarningSet;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
12 import net.sf.openrocket.rocketcomponent.FinSet;
13 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
14 import net.sf.openrocket.util.Coordinate;
15
16 import java.lang.reflect.Method;
17 import java.util.HashMap;
18
19 /**
20  * FinSetHandler Tester.
21  *
22  */
23 public class FinSetHandlerTest extends TestCase {
24
25     /**
26      * The class under test.
27      */
28     public static final Class classUT = FinSetHandler.class;
29
30     /**
31      * The test class (this class).
32      */
33     public static final Class testClass = FinSetHandlerTest.class;
34
35     /**
36      * Create a test suite of all tests within this test class.
37      *
38      * @return a suite of tests
39      */
40     public static Test suite() {
41         return new TestSuite(FinSetHandlerTest.class);
42     }
43
44     /**
45      * Test constructor.
46      *
47      * @param name the name of the test to run.
48      */
49     public FinSetHandlerTest(String name) {
50         super(name);
51     }
52
53     /**
54      * Setup the fixture.
55      */
56     public void setUp() throws Exception {
57         super.setUp();
58     }
59
60     /**
61      * Teardown the fixture.
62      */
63     public void tearDown() throws Exception {
64         super.tearDown();
65     }
66
67
68     /**
69      * Method: asOpenRocket(WarningSet warnings)
70      *
71      * @throws Exception thrown if something goes awry
72      */
73     public void testAsOpenRocket() throws Exception {
74
75         FinSetHandler dto = new FinSetHandler(new BodyTube());
76
77         HashMap<String, String> attributes = new HashMap<String, String>();
78         WarningSet warnings = new WarningSet();
79
80         dto.closeElement("Name", attributes, "The name", warnings);
81         dto.closeElement("ShapeCode", attributes, "0", warnings);
82         dto.closeElement("Xb", attributes, "2", warnings);
83         dto.closeElement("FinCount", attributes, "4", warnings);
84         dto.closeElement("RootChord", attributes, "10", warnings);
85         dto.closeElement("TipChord", attributes, "11", warnings);
86         dto.closeElement("SemiSpan", attributes, "12", warnings);
87         dto.closeElement("MidChordLen", attributes, "13", warnings);
88         dto.closeElement("SweepDistance", attributes, "14", warnings);
89         dto.closeElement("Thickness", attributes, "200", warnings);
90         dto.closeElement("TipShapeCode", attributes, "1", warnings);
91         dto.closeElement("TabLength", attributes, "400", warnings);
92         dto.closeElement("TabDepth", attributes, "500", warnings);
93         dto.closeElement("TabOffset", attributes, "30", warnings);
94         dto.closeElement("RadialAngle", attributes, ".123", warnings);
95         dto.closeElement("PointList", attributes, "20,0|2,2|0,0", warnings);
96         dto.closeElement("LocationMode", attributes, "0", warnings);
97
98         WarningSet set = new WarningSet();
99         FinSet fins = dto.asOpenRocket(set);
100         assertNotNull(fins);
101         assertEquals(0, set.size());
102
103         assertEquals("The name", fins.getName());
104         assertTrue(fins instanceof TrapezoidFinSet);
105         assertEquals(4, fins.getFinCount());
106
107         assertEquals(0.2d, fins.getThickness());
108         assertEquals(0.4d, fins.getTabLength());
109         assertEquals(0.5d, fins.getTabHeight());
110         assertEquals(0.03d, fins.getTabShift());
111         assertEquals(.123d, fins.getBaseRotation());
112
113         dto.closeElement("ShapeCode", attributes, "1", warnings);
114         fins = dto.asOpenRocket(set);
115         assertNotNull(fins);
116         assertEquals(0, set.size());
117
118         assertEquals("The name", fins.getName());
119         assertTrue(fins instanceof EllipticalFinSet);
120         assertEquals(4, fins.getFinCount());
121
122         assertEquals(0.2d, fins.getThickness());
123         assertEquals(0.4d, fins.getTabLength());
124         assertEquals(0.5d, fins.getTabHeight());
125         assertEquals(0.03d, fins.getTabShift());
126         assertEquals(.123d, fins.getBaseRotation());
127     }
128
129
130     /**
131      * Method: toCoordinates(String pointList)
132      *
133      * @throws Exception thrown if something goes awry
134      */
135     public void testToCoordinates() throws Exception {
136         FinSetHandler holder = new FinSetHandler(new BodyTube());
137         Method method = FinSetHandler.class.getDeclaredMethod("toCoordinates", String.class, WarningSet.class);
138         method.setAccessible(true);
139
140         WarningSet warnings = new WarningSet();
141         //Null finlist
142         String finlist = null;
143         Coordinate[] result = (Coordinate[])method.invoke(holder, finlist, warnings);
144         assertNotNull(result);
145         assertTrue(0 == result.length);
146
147         //Empty string finlist
148         finlist = "";
149         result = (Coordinate[])method.invoke(holder, finlist, warnings);
150         assertNotNull(result);
151         assertTrue(0 == result.length);
152         
153         //Invalid finlist (only x coordinate)
154         finlist = "10.0";
155         result = (Coordinate[])method.invoke(holder, finlist, warnings);
156         assertNotNull(result);
157         assertTrue(0 == result.length);
158         assertEquals(1, warnings.size());
159         warnings.clear();
160
161         //Invalid finlist (non-numeric character)
162         finlist = "10.0,asdf";
163         result = (Coordinate[])method.invoke(holder, finlist, warnings);
164         assertNotNull(result);
165         assertTrue(0 == result.length);
166         assertEquals(1, warnings.size());
167         warnings.clear();
168
169         //Invalid finlist (all delimiters)
170         finlist = "||||||";
171         result = (Coordinate[])method.invoke(holder, finlist, warnings);
172         assertNotNull(result);
173         assertTrue(0 == result.length);
174         assertEquals(0, warnings.size());
175         warnings.clear();
176
177         //One point finlist - from a parsing view it's valid; from a practical view it may not be, but that's outside
178         //the scope of this test case
179         finlist = "10.0,5.0";
180         result = (Coordinate[])method.invoke(holder, finlist, warnings);
181         assertNotNull(result);
182         assertTrue(1 == result.length);
183         assertEquals(0, warnings.size());
184         warnings.clear();
185         
186         //Two point finlist - from a parsing view it's valid; from a practical view it may not be, but that's outside
187         //the scope of this test case
188         finlist = "10.0,5.0|3.3,4.4";
189         result = (Coordinate[])method.invoke(holder, finlist, warnings);
190         assertNotNull(result);
191         assertTrue(2 == result.length);
192         assertEquals(0, warnings.size());
193         warnings.clear();
194         
195         //Normal four point finlist.
196         finlist = "518.16,0|517.494,37.2145|1.31261,6.77283|0,0|";
197         result = (Coordinate[])method.invoke(holder, finlist, warnings);
198         assertNotNull(result);
199         assertTrue(4 == result.length);
200         assertEquals(new Coordinate(0,0), result[0]);
201         assertEquals(0, warnings.size());
202         warnings.clear();
203         
204         //Normal four point finlist with spaces.
205         finlist = "518.16 , 0 | 517.494 , 37.2145 | 1.31261,6.77283|0,0|";
206         result = (Coordinate[])method.invoke(holder, finlist, warnings);
207         assertNotNull(result);
208         assertTrue(4 == result.length);
209         assertEquals(new Coordinate(0,0), result[0]);
210         assertEquals(new Coordinate(.51816,0), result[3]);
211         assertEquals(0, warnings.size());
212         warnings.clear();
213         
214         //Reversed Normal four point finlist.
215         finlist = "0,0|1.31261,6.77283|517.494,37.2145|518.16,0|";
216         result = (Coordinate[])method.invoke(holder, finlist, warnings);
217         assertNotNull(result);
218         assertTrue(4 == result.length);
219         assertEquals(new Coordinate(0,0), result[0]);
220         assertEquals(new Coordinate(.51816,0), result[3]);
221         assertEquals(0, warnings.size());
222         warnings.clear();
223         
224     }
225 }