1cc4792edda5d4bf13f15415f1ca5b87ea8b5058
[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.012d, ((TrapezoidFinSet) fins).getHeight());
108         assertEquals(0.012d, fins.getSpan());
109         
110         assertEquals(0.2d, fins.getThickness());
111         assertEquals(0.4d, fins.getTabLength());
112         assertEquals(0.5d, fins.getTabHeight());
113         assertEquals(0.03d, fins.getTabShift());
114         assertEquals(.123d, fins.getBaseRotation());
115
116         dto.closeElement("ShapeCode", attributes, "1", warnings);
117         fins = dto.asOpenRocket(set);
118         assertNotNull(fins);
119         assertEquals(0, set.size());
120
121         assertEquals("The name", fins.getName());
122         assertTrue(fins instanceof EllipticalFinSet);
123         assertEquals(4, fins.getFinCount());
124
125         assertEquals(0.2d, fins.getThickness());
126         assertEquals(0.4d, fins.getTabLength());
127         assertEquals(0.5d, fins.getTabHeight());
128         assertEquals(0.03d, fins.getTabShift());
129         assertEquals(.123d, fins.getBaseRotation());
130     }
131
132
133     /**
134      * Method: toCoordinates(String pointList)
135      *
136      * @throws Exception thrown if something goes awry
137      */
138     public void testToCoordinates() throws Exception {
139         FinSetHandler holder = new FinSetHandler(new BodyTube());
140         Method method = FinSetHandler.class.getDeclaredMethod("toCoordinates", String.class, WarningSet.class);
141         method.setAccessible(true);
142
143         WarningSet warnings = new WarningSet();
144         //Null finlist
145         String finlist = null;
146         Coordinate[] result = (Coordinate[])method.invoke(holder, finlist, warnings);
147         assertNotNull(result);
148         assertTrue(0 == result.length);
149
150         //Empty string finlist
151         finlist = "";
152         result = (Coordinate[])method.invoke(holder, finlist, warnings);
153         assertNotNull(result);
154         assertTrue(0 == result.length);
155         
156         //Invalid finlist (only x coordinate)
157         finlist = "10.0";
158         result = (Coordinate[])method.invoke(holder, finlist, warnings);
159         assertNotNull(result);
160         assertTrue(0 == result.length);
161         assertEquals(1, warnings.size());
162         warnings.clear();
163
164         //Invalid finlist (non-numeric character)
165         finlist = "10.0,asdf";
166         result = (Coordinate[])method.invoke(holder, finlist, warnings);
167         assertNotNull(result);
168         assertTrue(0 == result.length);
169         assertEquals(1, warnings.size());
170         warnings.clear();
171
172         //Invalid finlist (all delimiters)
173         finlist = "||||||";
174         result = (Coordinate[])method.invoke(holder, finlist, warnings);
175         assertNotNull(result);
176         assertTrue(0 == result.length);
177         assertEquals(0, warnings.size());
178         warnings.clear();
179
180         //One point finlist - from a parsing view it's valid; from a practical view it may not be, but that's outside
181         //the scope of this test case
182         finlist = "10.0,5.0";
183         result = (Coordinate[])method.invoke(holder, finlist, warnings);
184         assertNotNull(result);
185         assertTrue(1 == result.length);
186         assertEquals(0, warnings.size());
187         warnings.clear();
188         
189         //Two point finlist - from a parsing view it's valid; from a practical view it may not be, but that's outside
190         //the scope of this test case
191         finlist = "10.0,5.0|3.3,4.4";
192         result = (Coordinate[])method.invoke(holder, finlist, warnings);
193         assertNotNull(result);
194         assertTrue(2 == result.length);
195         assertEquals(0, warnings.size());
196         warnings.clear();
197         
198         //Normal four point finlist.
199         finlist = "518.16,0|517.494,37.2145|1.31261,6.77283|0,0|";
200         result = (Coordinate[])method.invoke(holder, finlist, warnings);
201         assertNotNull(result);
202         assertTrue(4 == result.length);
203         assertEquals(new Coordinate(0,0), result[0]);
204         assertEquals(0, warnings.size());
205         warnings.clear();
206         
207         //Normal four point finlist with spaces.
208         finlist = "518.16 , 0 | 517.494 , 37.2145 | 1.31261,6.77283|0,0|";
209         result = (Coordinate[])method.invoke(holder, finlist, warnings);
210         assertNotNull(result);
211         assertTrue(4 == result.length);
212         assertEquals(new Coordinate(0,0), result[0]);
213         assertEquals(new Coordinate(.51816,0), result[3]);
214         assertEquals(0, warnings.size());
215         warnings.clear();
216         
217         //Reversed Normal four point finlist.
218         finlist = "0,0|1.31261,6.77283|517.494,37.2145|518.16,0|";
219         result = (Coordinate[])method.invoke(holder, finlist, warnings);
220         assertNotNull(result);
221         assertTrue(4 == result.length);
222         assertEquals(new Coordinate(0,0), result[0]);
223         assertEquals(new Coordinate(.51816,0), result[3]);
224         assertEquals(0, warnings.size());
225         warnings.clear();
226         
227     }
228 }