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.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());
113 dto.closeElement("ShapeCode", attributes, "1", warnings);
114 fins = dto.asOpenRocket(set);
116 assertEquals(0, set.size());
118 assertEquals("The name", fins.getName());
119 assertTrue(fins instanceof EllipticalFinSet);
120 assertEquals(4, fins.getFinCount());
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());
131 * Method: toCoordinates(String pointList)
133 * @throws Exception thrown if something goes awry
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);
140 WarningSet warnings = new WarningSet();
142 String finlist = null;
143 Coordinate[] result = (Coordinate[])method.invoke(holder, finlist, warnings);
144 assertNotNull(result);
145 assertTrue(0 == result.length);
147 //Empty string finlist
149 result = (Coordinate[])method.invoke(holder, finlist, warnings);
150 assertNotNull(result);
151 assertTrue(0 == result.length);
153 //Invalid finlist (only x coordinate)
155 result = (Coordinate[])method.invoke(holder, finlist, warnings);
156 assertNotNull(result);
157 assertTrue(0 == result.length);
158 assertEquals(1, warnings.size());
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());
169 //Invalid finlist (all delimiters)
171 result = (Coordinate[])method.invoke(holder, finlist, warnings);
172 assertNotNull(result);
173 assertTrue(0 == result.length);
174 assertEquals(0, warnings.size());
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());
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());
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());
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());
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());