]> git.gag.com Git - debian/openrocket/blob - core/test/net/sf/openrocket/file/rocksim/importt/MassObjectHandlerTest.java
fa435229f4bd4c110cd538b7b77c1423546e0c9e
[debian/openrocket] / core / test / net / sf / openrocket / file / rocksim / importt / MassObjectHandlerTest.java
1 /*
2  * MassObjectHandlerTest.java
3  */
4 package net.sf.openrocket.file.rocksim.importt;
5
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
8 import net.sf.openrocket.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.MassComponent;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import org.junit.Assert;
14
15 import java.util.HashMap;
16
17 /**
18  * MassObjectHandler Tester.
19  *
20  */
21 public class MassObjectHandlerTest extends RocksimTestBase {
22
23     /**
24      * Method: constructor
25      *
26      * @throws Exception thrown if something goes awry
27      */
28     @org.junit.Test
29     public void testConstructor() throws Exception {
30
31         try {
32             new MassObjectHandler(null, new WarningSet());
33             Assert.fail("Should have thrown IllegalArgumentException");
34         }
35         catch (IllegalArgumentException iae) {
36             //success
37         }
38
39         BodyTube tube = new BodyTube();
40         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
41         MassComponent component = (MassComponent) getField(handler, "mass");
42         assertContains(component, tube.getChildren());
43     }
44
45     /**
46      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
47      *
48      * @throws Exception thrown if something goes awry
49      */
50     @org.junit.Test
51     public void testOpenElement() throws Exception {
52         Assert.assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
53     }
54
55     /**
56      *
57      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
58      *
59      * @throws Exception  thrown if something goes awry
60      */
61     @org.junit.Test
62     public void testCloseElement() throws Exception {
63         BodyTube tube = new BodyTube();
64         HashMap<String, String> attributes = new HashMap<String, String>();
65         WarningSet warnings = new WarningSet();
66
67         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
68         MassComponent component = (MassComponent) getField(handler, "mass");
69
70         handler.closeElement("Len", attributes, "-1", warnings);
71         Assert.assertEquals(0d, component.getLength(), 0.001);
72         handler.closeElement("Len", attributes, "10", warnings);
73         Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
74                 , component.getLength(), 0.001);
75         handler.closeElement("Len", attributes, "10.0", warnings);
76         Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
77                 , component.getLength(), 0.001);
78         handler.closeElement("Len", attributes, "foo", warnings);
79         Assert.assertEquals(1, warnings.size());
80         warnings.clear();
81
82         handler.closeElement("KnownMass", attributes, "-1", warnings);
83         Assert.assertEquals(0d, component.getComponentMass(), 0.001);
84         handler.closeElement("KnownMass", attributes, "100", warnings);
85         Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS, component.getComponentMass(), 0.001);
86         handler.closeElement("KnownMass", attributes, "foo", warnings);
87         Assert.assertEquals(1, warnings.size());
88         warnings.clear();
89
90     }
91     
92     /**
93      * Method: setRelativePosition(RocketComponent.Position position)
94      *
95      * @throws Exception thrown if something goes awry
96      */
97     @org.junit.Test
98     public void testSetRelativePosition() throws Exception {
99         BodyTube tube = new BodyTube();
100         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
101         MassComponent component = (MassComponent) getField(handler, "mass");
102         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
103         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
104     }
105
106     /**
107      * Method: getComponent()
108      *
109      * @throws Exception thrown if something goes awry
110      */
111     @org.junit.Test
112     public void testGetComponent() throws Exception {
113         Assert.assertTrue(new MassObjectHandler(new BodyTube(), new WarningSet()).getComponent() instanceof MassComponent);
114     }
115
116     /**
117      * Method: getMaterialType()
118      *
119      * @throws Exception thrown if something goes awry
120      */
121     @org.junit.Test
122     public void testGetMaterialType() throws Exception {
123         Assert.assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
124     }
125 }