a3f636a7b1f583dbc77f513c9b0e6ca9d107ff4d
[debian/openrocket] / core / test / net / sf / openrocket / preset / TransitionPresetTests.java
1 package net.sf.openrocket.preset;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import net.sf.openrocket.material.Material;
6 import net.sf.openrocket.motor.Manufacturer;
7 import net.sf.openrocket.rocketcomponent.Transition;
8 import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
9
10 import org.junit.Test;
11
12 /**
13  * Test construction of TRANSITION type ComponentPresets based on TypedPropertyMap through the
14  * ComponentPresetFactory.create() method.
15  * 
16  * Ensure required properties are populated
17  * 
18  * Ensure any computed values are correctly computed.
19  * 
20  */
21 public class TransitionPresetTests extends BaseTestCase {
22
23         @Test
24         public void testManufacturerRequired() {
25                 try {
26                         TypedPropertyMap presetspec = new TypedPropertyMap();
27                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
28                         ComponentPresetFactory.create(presetspec);
29                 } catch ( InvalidComponentPresetException ex ) {
30                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Manufacturer specified"));
31                 }
32         }
33
34         @Test
35         public void testPartNoRequired() {
36                 try {
37                         TypedPropertyMap presetspec = new TypedPropertyMap();
38                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
39                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
40                         ComponentPresetFactory.create(presetspec);
41                 } catch ( InvalidComponentPresetException ex ) {
42                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No PartNo specified"));
43                 }
44         }
45
46         @Test
47         public void testLengthRequired() {
48                 try {
49                         TypedPropertyMap presetspec = new TypedPropertyMap();
50                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
51                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
52                         presetspec.put( ComponentPreset.PARTNO, "partno");
53                         ComponentPresetFactory.create(presetspec);
54                 } catch ( InvalidComponentPresetException ex ) {
55                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Length specified"));
56                 }
57         }
58
59         @Test
60         public void testAftOuterDiameterRequired() {
61                 try {
62                         TypedPropertyMap presetspec = new TypedPropertyMap();
63                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
64                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
65                         presetspec.put( ComponentPreset.PARTNO, "partno");
66                         presetspec.put( ComponentPreset.LENGTH, 2.0);
67                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
68                         ComponentPresetFactory.create(presetspec);
69                 } catch ( InvalidComponentPresetException ex ) {
70                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No AftOuterDiameter"));
71                 }
72         }
73
74
75         @Test
76         public void testForeOuterDiameterRequired() {
77                 try {
78                         TypedPropertyMap presetspec = new TypedPropertyMap();
79                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
80                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
81                         presetspec.put( ComponentPreset.PARTNO, "partno");
82                         presetspec.put( ComponentPreset.LENGTH, 2.0);
83                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
84                         presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
85                         ComponentPresetFactory.create(presetspec);
86                 } catch ( InvalidComponentPresetException ex ) {
87                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No ForeOuterDiameter"));
88                 }
89         }
90
91         @Test
92         public void testComputeDensityNoMaterial() throws Exception {
93                 TypedPropertyMap presetspec = new TypedPropertyMap();
94                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
95                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
96                 presetspec.put( ComponentPreset.PARTNO, "partno");
97                 presetspec.put( ComponentPreset.LENGTH, 2.0);
98                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
99                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
100                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
101                 presetspec.put( ComponentPreset.FILLED, true);
102                 presetspec.put( ComponentPreset.MASS, 100.0);
103                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
104
105                 // constants put into the presetspec above.
106                 double volume = /*base area*/ Math.PI  * ( 1.0 * 1.0 + 1.0 * 0.5 + 0.5 * 0.5);
107                 
108                 volume *= 2.0 /* times height */ / 3.0; /* one third */
109
110                 double density = 100.0 / volume;
111                 
112                 assertEquals("TransitionCustom",preset.get(ComponentPreset.MATERIAL).getName());
113                 // FIXME - I would expect the nc volume computation to be closer for such a simple shape.
114                 // simple math yields 27.2837
115                 // 100/nc.getComponentVolume yields 27.59832
116                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.5);
117         }
118
119         @Test
120         public void testMaterial() throws Exception {
121                 TypedPropertyMap presetspec = new TypedPropertyMap();
122                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
123                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
124                 presetspec.put( ComponentPreset.PARTNO, "partno");
125                 presetspec.put( ComponentPreset.LENGTH, 2.0);
126                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
127                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
128                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
129                 presetspec.put( ComponentPreset.FILLED, true);
130                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
131                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
132
133                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
134                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
135                 
136         }
137
138         @Test
139         public void testComputeDensityWithMaterial() throws Exception {
140                 TypedPropertyMap presetspec = new TypedPropertyMap();
141                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
142                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
143                 presetspec.put( ComponentPreset.PARTNO, "partno");
144                 presetspec.put( ComponentPreset.LENGTH, 2.0);
145                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
146                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
147                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
148                 presetspec.put( ComponentPreset.FILLED, true);
149                 presetspec.put( ComponentPreset.MASS, 100.0);
150                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
151                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
152
153                 // constants put into the presetspec above.
154                 double totvolume = /*base area*/ Math.PI;
155                 
156                 totvolume *= 4.0 /* times height */ / 3.0; /* one third */
157
158                 double uppervolume = /*fore area*/ Math.PI * 0.5 * 0.5;
159                 uppervolume *= 2.0 /* times height */ / 3.0; /* one third */
160                 
161                 double volume = totvolume-uppervolume;
162
163                 double density = 100.0 / volume;
164                 
165                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
166                 // FIXME - I would expect the nc volume computation to be closer for such a simple shape.
167                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),1.5);
168         }
169
170 }