create changelog entry
[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 mass = (MassComponent) getField(handler, "mass");
42         MassComponent current = (MassComponent) getField(handler, "current");
43         Assert.assertEquals(mass, current);
44     }
45
46     /**
47      * Method: openElement(String element, HashMap<String, String> attributes, WarningSet warnings)
48      *
49      * @throws Exception thrown if something goes awry
50      */
51     @org.junit.Test
52     public void testOpenElement() throws Exception {
53         Assert.assertEquals(PlainTextHandler.INSTANCE, new MassObjectHandler(new BodyTube(), new WarningSet()).openElement(null, null, null));
54     }
55
56     /**
57      *
58      * Method: closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
59      *
60      * @throws Exception  thrown if something goes awry
61      */
62     @org.junit.Test
63     public void testCloseElement() throws Exception {
64         BodyTube tube = new BodyTube();
65         HashMap<String, String> attributes = new HashMap<String, String>();
66         WarningSet warnings = new WarningSet();
67
68         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
69         MassComponent component = (MassComponent) getField(handler, "mass");
70
71         handler.closeElement("Len", attributes, "-1", warnings);
72         Assert.assertEquals(0d, component.getLength(), 0.001);
73         handler.closeElement("Len", attributes, "10", warnings);
74         Assert.assertEquals(0.01
75                 , component.getLength(), 0.001);
76         handler.closeElement("Len", attributes, "10.0", warnings);
77         Assert.assertEquals(0.01
78                 , component.getLength(), 0.001);
79         handler.closeElement("Len", attributes, "foo", warnings);
80         Assert.assertEquals(1, warnings.size());
81         warnings.clear();
82
83         handler.closeElement("KnownMass", attributes, "-1", warnings);
84         Assert.assertEquals(0d, component.getComponentMass(), 0.001);
85         handler.closeElement("KnownMass", attributes, "100", warnings);
86         Assert.assertEquals(100d / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS, component.getComponentMass(), 0.001);
87         handler.closeElement("KnownMass", attributes, "foo", warnings);
88         Assert.assertEquals(1, warnings.size());
89         warnings.clear();
90
91     }
92     
93     /**
94      * Method: setRelativePosition(RocketComponent.Position position)
95      *
96      * @throws Exception thrown if something goes awry
97      */
98     @org.junit.Test
99     public void testSetRelativePosition() throws Exception {
100         BodyTube tube = new BodyTube();
101         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
102         MassComponent component = (MassComponent) getField(handler, "mass");
103         handler.setRelativePosition(RocketComponent.Position.ABSOLUTE);
104         Assert.assertEquals(RocketComponent.Position.ABSOLUTE, component.getRelativePosition());
105     }
106
107     /**
108      * Method: getComponent()
109      *
110      * @throws Exception thrown if something goes awry
111      */
112     @org.junit.Test
113     public void testGetComponent() throws Exception {
114         Assert.assertTrue(new MassObjectHandler(new BodyTube(), new WarningSet()).getComponent() instanceof MassComponent);
115     }
116
117     /**
118      * Method: getMaterialType()
119      *
120      * @throws Exception thrown if something goes awry
121      */
122     @org.junit.Test
123     public void testGetMaterialType() throws Exception {
124         Assert.assertEquals(Material.Type.LINE, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
125     }
126 }