Fixed problems in computation of SymmetricComponent.integrate found by new unit tests...
[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
114                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
115         }
116
117         @Test
118         public void testMaterial() throws Exception {
119                 TypedPropertyMap presetspec = new TypedPropertyMap();
120                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
121                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
122                 presetspec.put( ComponentPreset.PARTNO, "partno");
123                 presetspec.put( ComponentPreset.LENGTH, 2.0);
124                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
125                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
126                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
127                 presetspec.put( ComponentPreset.FILLED, true);
128                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
129                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
130
131                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
132                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
133                 
134         }
135
136         @Test
137         public void testComputeDensityWithMaterial() throws Exception {
138                 TypedPropertyMap presetspec = new TypedPropertyMap();
139                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
140                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
141                 presetspec.put( ComponentPreset.PARTNO, "partno");
142                 presetspec.put( ComponentPreset.LENGTH, 2.0);
143                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
144                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
145                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
146                 presetspec.put( ComponentPreset.FILLED, true);
147                 presetspec.put( ComponentPreset.MASS, 100.0);
148                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
149                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
150
151                 // constants put into the presetspec above.
152                 double totvolume = /*base area*/ Math.PI;
153                 
154                 totvolume *= 4.0 /* times height */ / 3.0; /* one third */
155
156                 double uppervolume = /*fore area*/ Math.PI * 0.5 * 0.5;
157                 uppervolume *= 2.0 /* times height */ / 3.0; /* one third */
158                 
159                 double volume = totvolume-uppervolume;
160
161                 double density = 100.0 / volume;
162                 
163                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
164
165                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
166         }
167
168 }