a2752eb3afb7a944abfb6d9e48b35d9dad0777b4
[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 net.sf.openrocket.material.Material;
5 import net.sf.openrocket.motor.Manufacturer;
6 import net.sf.openrocket.rocketcomponent.Transition;
7 import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
8
9 import org.junit.Test;
10
11 /**
12  * Test construction of TRANSITION type ComponentPresets based on TypedPropertyMap through the
13  * ComponentPresetFactory.create() method.
14  * 
15  * Ensure required properties are populated
16  * 
17  * Ensure any computed values are correctly computed.
18  * 
19  */
20 public class TransitionPresetTests extends BaseTestCase {
21
22         @Test
23         public void testManufacturerRequired() {
24                 try {
25                         TypedPropertyMap presetspec = new TypedPropertyMap();
26                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
27                         ComponentPresetFactory.create(presetspec);
28                 } catch ( InvalidComponentPresetException ex ) {
29                         PresetAssertHelper.assertInvalidPresetException( ex,
30                                         new TypedKey<?>[] {
31                                         ComponentPreset.MANUFACTURER, 
32                                         ComponentPreset.PARTNO, 
33                                         ComponentPreset.LENGTH,
34                                         ComponentPreset.AFT_OUTER_DIAMETER,
35                                         ComponentPreset.FORE_OUTER_DIAMETER
36                         },
37                         new String[] {
38                                         "No Manufacturer specified",
39                                         "No PartNo specified",
40                                         "No Length specified",
41                                         "No AftOuterDiameter specified",
42                                         "No ForeOuterDiameter specified"
43                         }
44                                         );
45                 }
46         }
47
48         @Test
49         public void testPartNoRequired() {
50                 try {
51                         TypedPropertyMap presetspec = new TypedPropertyMap();
52                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
53                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
54                         ComponentPresetFactory.create(presetspec);
55                 } catch ( InvalidComponentPresetException ex ) {
56                         PresetAssertHelper.assertInvalidPresetException( ex,
57                                         new TypedKey<?>[] {
58                                         ComponentPreset.PARTNO, 
59                                         ComponentPreset.LENGTH,
60                                         ComponentPreset.AFT_OUTER_DIAMETER,
61                                         ComponentPreset.FORE_OUTER_DIAMETER
62                         },
63                         new String[] {
64                                         "No PartNo specified",
65                                         "No Length specified",
66                                         "No AftOuterDiameter specified",
67                                         "No ForeOuterDiameter specified"
68                         }
69                                         );
70                 }
71         }
72
73         @Test
74         public void testLengthRequired() {
75                 try {
76                         TypedPropertyMap presetspec = new TypedPropertyMap();
77                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
78                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
79                         presetspec.put( ComponentPreset.PARTNO, "partno");
80                         ComponentPresetFactory.create(presetspec);
81                 } catch ( InvalidComponentPresetException ex ) {
82                         PresetAssertHelper.assertInvalidPresetException( ex,
83                                         new TypedKey<?>[] {
84                                         ComponentPreset.LENGTH,
85                                         ComponentPreset.AFT_OUTER_DIAMETER,
86                                         ComponentPreset.FORE_OUTER_DIAMETER
87                         },
88                         new String[] {
89                                         "No Length specified",
90                                         "No AftOuterDiameter specified",
91                                         "No ForeOuterDiameter specified"
92                         }
93                                         );
94                 }
95         }
96
97         @Test
98         public void testAftOuterDiameterRequired() {
99                 try {
100                         TypedPropertyMap presetspec = new TypedPropertyMap();
101                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
102                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
103                         presetspec.put( ComponentPreset.PARTNO, "partno");
104                         presetspec.put( ComponentPreset.LENGTH, 2.0);
105                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
106                         ComponentPresetFactory.create(presetspec);
107                 } catch ( InvalidComponentPresetException ex ) {
108                         PresetAssertHelper.assertInvalidPresetException( ex,
109                                         new TypedKey<?>[] {
110                                         ComponentPreset.AFT_OUTER_DIAMETER,
111                                         ComponentPreset.FORE_OUTER_DIAMETER
112                         },
113                         new String[] {
114                                         "No AftOuterDiameter specified",
115                                         "No ForeOuterDiameter specified"
116                         }
117                                         );
118                 }
119         }
120
121
122         @Test
123         public void testForeOuterDiameterRequired() {
124                 try {
125                         TypedPropertyMap presetspec = new TypedPropertyMap();
126                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
127                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
128                         presetspec.put( ComponentPreset.PARTNO, "partno");
129                         presetspec.put( ComponentPreset.LENGTH, 2.0);
130                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
131                         presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
132                         ComponentPresetFactory.create(presetspec);
133                 } catch ( InvalidComponentPresetException ex ) {
134                         PresetAssertHelper.assertInvalidPresetException( ex,
135                                         new TypedKey<?>[] {
136                                         ComponentPreset.FORE_OUTER_DIAMETER
137                         },
138                         new String[] {
139                                         "No ForeOuterDiameter specified"
140                         }
141                                         );
142                 }
143         }
144
145         @Test
146         public void testComputeDensityNoMaterial() throws Exception {
147                 TypedPropertyMap presetspec = new TypedPropertyMap();
148                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
149                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
150                 presetspec.put( ComponentPreset.PARTNO, "partno");
151                 presetspec.put( ComponentPreset.LENGTH, 2.0);
152                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
153                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
154                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
155                 presetspec.put( ComponentPreset.FILLED, true);
156                 presetspec.put( ComponentPreset.MASS, 100.0);
157                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
158
159                 // constants put into the presetspec above.
160                 double volume = /*base area*/ Math.PI  * ( 1.0 * 1.0 + 1.0 * 0.5 + 0.5 * 0.5);
161                 
162                 volume *= 2.0 /* times height */ / 3.0; /* one third */
163
164                 double density = 100.0 / volume;
165                 
166                 assertEquals("TransitionCustom",preset.get(ComponentPreset.MATERIAL).getName());
167
168                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
169         }
170
171         @Test
172         public void testMaterial() throws Exception {
173                 TypedPropertyMap presetspec = new TypedPropertyMap();
174                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
175                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
176                 presetspec.put( ComponentPreset.PARTNO, "partno");
177                 presetspec.put( ComponentPreset.LENGTH, 2.0);
178                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
179                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
180                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
181                 presetspec.put( ComponentPreset.FILLED, true);
182                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
183                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
184
185                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
186                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
187                 
188         }
189
190         @Test
191         public void testComputeDensityWithMaterial() throws Exception {
192                 TypedPropertyMap presetspec = new TypedPropertyMap();
193                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.TRANSITION);
194                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
195                 presetspec.put( ComponentPreset.PARTNO, "partno");
196                 presetspec.put( ComponentPreset.LENGTH, 2.0);
197                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
198                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
199                 presetspec.put( ComponentPreset.FORE_OUTER_DIAMETER, 1.0);
200                 presetspec.put( ComponentPreset.FILLED, true);
201                 presetspec.put( ComponentPreset.MASS, 100.0);
202                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
203                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
204
205                 // constants put into the presetspec above.
206                 double totvolume = /*base area*/ Math.PI;
207                 
208                 totvolume *= 4.0 /* times height */ / 3.0; /* one third */
209
210                 double uppervolume = /*fore area*/ Math.PI * 0.5 * 0.5;
211                 uppervolume *= 2.0 /* times height */ / 3.0; /* one third */
212                 
213                 double volume = totvolume-uppervolume;
214
215                 double density = 100.0 / volume;
216                 
217                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
218
219                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
220         }
221
222 }