36743672ed4b24423e681ccc0c5d1bf05afe8f96
[debian/openrocket] / core / test / net / sf / openrocket / preset / NoseConePresetTests.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 NOSE_CONE 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 NoseConePresetTests extends BaseTestCase {
21
22         @Test
23         public void testManufacturerRequired() {
24                 try {
25                         TypedPropertyMap presetspec = new TypedPropertyMap();
26                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
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.SHAPE
36                         },
37                         new String[] {
38                                         "No Manufacturer specified",
39                                         "No PartNo specified",
40                                         "No Length specified",
41                                         "No AftOuterDiameter specified",
42                                         "No Shape 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.NOSE_CONE);
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.SHAPE
62                         },
63                         new String[] {
64                                         "No PartNo specified",
65                                         "No Length specified",
66                                         "No AftOuterDiameter specified",
67                                         "No Shape 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.NOSE_CONE);
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.SHAPE
87                         },
88                         new String[] {
89                                         "No Length specified",
90                                         "No AftOuterDiameter specified",
91                                         "No Shape specified"
92                         }
93                                         );
94                 }
95         }
96
97         @Test
98         public void testShapeRequired() {
99                 try {
100                         TypedPropertyMap presetspec = new TypedPropertyMap();
101                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
102                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
103                         presetspec.put( ComponentPreset.PARTNO, "partno");
104                         presetspec.put( ComponentPreset.LENGTH, 2.0);
105                         ComponentPresetFactory.create(presetspec);
106                 } catch ( InvalidComponentPresetException ex ) {
107                         PresetAssertHelper.assertInvalidPresetException( ex,
108                                         new TypedKey<?>[] {
109                                         ComponentPreset.AFT_OUTER_DIAMETER,
110                                         ComponentPreset.SHAPE
111                         },
112                         new String[] {
113                                         "No AftOuterDiameter specified",
114                                         "No Shape specified"
115                         }
116                                         );
117                 }
118         }
119
120         @Test
121         public void testAftOuterDiameterRequired() {
122                 try {
123                         TypedPropertyMap presetspec = new TypedPropertyMap();
124                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
125                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
126                         presetspec.put( ComponentPreset.PARTNO, "partno");
127                         presetspec.put( ComponentPreset.LENGTH, 2.0);
128                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
129                         ComponentPresetFactory.create(presetspec);
130                 } catch ( InvalidComponentPresetException ex ) {
131                         PresetAssertHelper.assertInvalidPresetException( ex,
132                                         new TypedKey<?>[] {
133                                         ComponentPreset.AFT_OUTER_DIAMETER
134                         },
135                         new String[] {
136                                         "No AftOuterDiameter specified"
137                         }
138                                         );
139                 }
140         }
141
142         @Test
143         public void testComputeDensityNoMaterial() throws Exception {
144                 TypedPropertyMap presetspec = new TypedPropertyMap();
145                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
146                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
147                 presetspec.put( ComponentPreset.PARTNO, "partno");
148                 presetspec.put( ComponentPreset.LENGTH, 2.0);
149                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
150                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
151                 presetspec.put( ComponentPreset.FILLED, true);
152                 presetspec.put( ComponentPreset.MASS, 100.0);
153                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
154
155                 // constants put into the presetspec above.
156                 double volume = /*base area*/ Math.PI;
157                 volume *= 2.0 /* times height */ / 3.0; /* one third */
158                 
159                 double density = 100.0 / volume;
160                 
161                 assertEquals("NoseConeCustom",preset.get(ComponentPreset.MATERIAL).getName());
162                 // note - epsilon is 1% of the simple computation of density
163                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
164         }
165
166         @Test
167         public void testMaterial() throws Exception {
168                 TypedPropertyMap presetspec = new TypedPropertyMap();
169                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
170                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
171                 presetspec.put( ComponentPreset.PARTNO, "partno");
172                 presetspec.put( ComponentPreset.LENGTH, 2.0);
173                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
174                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
175                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
176                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
177
178                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
179                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
180                 
181         }
182
183         @Test
184         public void testComputeDensityWithMaterial() throws Exception {
185                 TypedPropertyMap presetspec = new TypedPropertyMap();
186                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
187                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
188                 presetspec.put( ComponentPreset.PARTNO, "partno");
189                 presetspec.put( ComponentPreset.LENGTH, 2.0);
190                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
191                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
192                 presetspec.put( ComponentPreset.FILLED, true);
193                 presetspec.put( ComponentPreset.MASS, 100.0);
194                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
195                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
196
197                 // constants put into the presetspec above.
198                 double volume = /*base area*/ Math.PI;
199                 volume *= 2.0 /* times height */ / 3.0; /* one third */
200                 
201                 double density = 100.0 / volume;
202                 
203                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
204                 // note - epsilon is 1% of the simple computation of density
205                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
206         }
207
208 }