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