2 * FinSetHandlerTest.java
4 package net.sf.openrocket.file.rocksim;
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;
16 import java.lang.reflect.Method;
17 import java.util.HashMap;
20 * FinSetHandler Tester.
23 public class FinSetHandlerTest extends TestCase {
26 * The class under test.
28 public static final Class classUT = FinSetHandler.class;
31 * The test class (this class).
33 public static final Class testClass = FinSetHandlerTest.class;
36 * Create a test suite of all tests within this test class.
38 * @return a suite of tests
40 public static Test suite() {
41 return new TestSuite(FinSetHandlerTest.class);
47 * @param name the name of the test to run.
49 public FinSetHandlerTest(String name) {
56 public void setUp() throws Exception {
61 * Teardown the fixture.
63 public void tearDown() throws Exception {
69 * Method: asOpenRocket(WarningSet warnings)
71 * @throws Exception thrown if something goes awry
73 public void testAsOpenRocket() throws Exception {
75 FinSetHandler dto = new FinSetHandler(new BodyTube());
77 HashMap<String, String> attributes = new HashMap<String, String>();
78 WarningSet warnings = new WarningSet();
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);
98 WarningSet set = new WarningSet();
99 FinSet fins = dto.asOpenRocket(set);
101 assertEquals(0, set.size());
103 assertEquals("The name", fins.getName());
104 assertTrue(fins instanceof TrapezoidFinSet);
105 assertEquals(4, fins.getFinCount());
107 assertEquals(0.012d, ((TrapezoidFinSet) fins).getHeight());
108 assertEquals(0.012d, fins.getSpan());
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());
116 dto.closeElement("ShapeCode", attributes, "1", warnings);
117 fins = dto.asOpenRocket(set);
119 assertEquals(0, set.size());
121 assertEquals("The name", fins.getName());
122 assertTrue(fins instanceof EllipticalFinSet);
123 assertEquals(4, fins.getFinCount());
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());
134 * Method: toCoordinates(String pointList)
136 * @throws Exception thrown if something goes awry
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);
143 WarningSet warnings = new WarningSet();
145 String finlist = null;
146 Coordinate[] result = (Coordinate[])method.invoke(holder, finlist, warnings);
147 assertNotNull(result);
148 assertTrue(0 == result.length);
150 //Empty string finlist
152 result = (Coordinate[])method.invoke(holder, finlist, warnings);
153 assertNotNull(result);
154 assertTrue(0 == result.length);
156 //Invalid finlist (only x coordinate)
158 result = (Coordinate[])method.invoke(holder, finlist, warnings);
159 assertNotNull(result);
160 assertTrue(0 == result.length);
161 assertEquals(1, warnings.size());
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());
172 //Invalid finlist (all delimiters)
174 result = (Coordinate[])method.invoke(holder, finlist, warnings);
175 assertNotNull(result);
176 assertTrue(0 == result.length);
177 assertEquals(0, warnings.size());
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());
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());
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());
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());
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());