d106fefdb93f1fa54d1c6c20e0a168c3e67dae38
[debian/openrocket] / core / test / net / sf / openrocket / preset / CenteringRingPresetTests.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
7 import org.junit.Test;
8
9 /**
10  * Test construction of CENTERING_RING type ComponentPresets based on TypedPropertyMap through the
11  * ComponentPresetFactory.create() method.
12  * 
13  * Ensure required properties are populated
14  * 
15  * Ensure any computed values are correctly computed.
16  * 
17  */
18 public class CenteringRingPresetTests {
19
20         @Test
21         public void testManufacturerRequired() {
22                 try {
23                         TypedPropertyMap presetspec = new TypedPropertyMap();
24                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
25                         ComponentPresetFactory.create(presetspec);
26                 } catch ( InvalidComponentPresetException ex ) {
27                         PresetAssertHelper.assertInvalidPresetException( ex,
28                                         new TypedKey<?>[] {
29                                         ComponentPreset.MANUFACTURER, 
30                                         ComponentPreset.PARTNO, 
31                                         ComponentPreset.LENGTH
32                         },
33                         new String[] {
34                                         "No Manufacturer specified",
35                                         "No PartNo specified",
36                                         "No Length specified",
37                                         "Preset dimensions underspecified"
38                         }
39                                         );
40                 }
41         }
42
43         @Test
44         public void testPartNoRequired() {
45                 try {
46                         TypedPropertyMap presetspec = new TypedPropertyMap();
47                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
48                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
49                         ComponentPresetFactory.create(presetspec);
50                 } catch ( InvalidComponentPresetException ex ) {
51                         PresetAssertHelper.assertInvalidPresetException( ex,
52                                         new TypedKey<?>[] {
53                                         ComponentPreset.PARTNO, 
54                                         ComponentPreset.LENGTH
55                         },
56                         new String[] {
57                                         "No PartNo specified",
58                                         "No Length specified",
59                                         "Preset dimensions underspecified"
60                         }
61                                         );
62                 }
63         }
64
65         @Test
66         public void testLengthRequired() {
67                 try {
68                         TypedPropertyMap presetspec = new TypedPropertyMap();
69                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
70                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
71                         presetspec.put( ComponentPreset.PARTNO, "partno");
72                         ComponentPresetFactory.create(presetspec);
73                 } catch ( InvalidComponentPresetException ex ) {
74                         PresetAssertHelper.assertInvalidPresetException( ex,
75                                         new TypedKey<?>[] {
76                                         ComponentPreset.LENGTH
77                         },
78                         new String[] {
79                                         "No Length specified",
80                                         "Preset dimensions underspecified"
81                         }
82                                         );
83                 }
84         }
85
86         @Test
87         public void testOnlyOuterDiameter() {
88                 try {
89                         TypedPropertyMap presetspec = new TypedPropertyMap();
90                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
91                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
92                         presetspec.put( ComponentPreset.PARTNO, "partno");
93                         presetspec.put( ComponentPreset.LENGTH, 2.0);
94                         presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
95                         ComponentPresetFactory.create(presetspec);
96                 } catch ( InvalidComponentPresetException ex ) {
97                         PresetAssertHelper.assertInvalidPresetException( ex,
98                                         null,
99                         new String[] {
100                                         "Preset dimensions underspecified"
101                         }
102                                         );
103                 }
104         }
105
106         @Test
107         public void testOnlyInnerDiameter() {
108                 try {
109                         TypedPropertyMap presetspec = new TypedPropertyMap();
110                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
111                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
112                         presetspec.put( ComponentPreset.PARTNO, "partno");
113                         presetspec.put( ComponentPreset.LENGTH, 2.0);
114                         presetspec.put( ComponentPreset.INNER_DIAMETER, 2.0);
115                         ComponentPresetFactory.create(presetspec);
116                 } catch ( InvalidComponentPresetException ex ) {
117                         PresetAssertHelper.assertInvalidPresetException( ex,
118                                         null,
119                         new String[] {
120                                         "Preset dimensions underspecified"
121                         }
122                                         );
123                 }
124         }
125
126         @Test
127         public void testOnlyThicknessDiameter() {
128                 try {
129                         TypedPropertyMap presetspec = new TypedPropertyMap();
130                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
131                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
132                         presetspec.put( ComponentPreset.PARTNO, "partno");
133                         presetspec.put( ComponentPreset.LENGTH, 2.0);
134                         presetspec.put( ComponentPreset.THICKNESS, 2.0);
135                         ComponentPresetFactory.create(presetspec);
136                 } catch ( InvalidComponentPresetException ex ) {
137                         PresetAssertHelper.assertInvalidPresetException( ex,
138                                         null,
139                         new String[] {
140                                         "Preset dimensions underspecified"
141                         }
142                                         );
143                 }
144         }
145
146         @Test
147         public void testComputeInnerDiameter() throws Exception {
148                 TypedPropertyMap presetspec = new TypedPropertyMap();
149                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
150                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
151                 presetspec.put( ComponentPreset.PARTNO, "partno");
152                 presetspec.put( ComponentPreset.LENGTH, 2.0);
153                 presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
154                 presetspec.put( ComponentPreset.THICKNESS, 0.5);
155                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
156
157                 assertEquals(1.0,preset.get(ComponentPreset.INNER_DIAMETER).doubleValue(),0.0);
158         }
159
160         @Test
161         public void testComputeOuterDiameter() throws Exception {
162                 TypedPropertyMap presetspec = new TypedPropertyMap();
163                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
164                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
165                 presetspec.put( ComponentPreset.PARTNO, "partno");
166                 presetspec.put( ComponentPreset.LENGTH, 2.0);
167                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
168                 presetspec.put( ComponentPreset.THICKNESS, 0.5);
169                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
170
171                 assertEquals(2.0,preset.get(ComponentPreset.OUTER_DIAMETER).doubleValue(),0.0);
172         }
173
174         @Test
175         public void testComputeThickness() throws Exception {
176                 TypedPropertyMap presetspec = new TypedPropertyMap();
177                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
178                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
179                 presetspec.put( ComponentPreset.PARTNO, "partno");
180                 presetspec.put( ComponentPreset.LENGTH, 2.0);
181                 presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
182                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
183                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
184
185                 assertEquals(0.5,preset.get(ComponentPreset.THICKNESS).doubleValue(),0.0);
186         }
187
188         @Test
189         public void testComputeThicknessLooses() throws Exception {
190                 // If all OUTER_DIAMETER, INNER_DIAMETER and THICKNESS are
191                 // specified, THICKNESS is recomputed.
192                 TypedPropertyMap presetspec = new TypedPropertyMap();
193                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
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.OUTER_DIAMETER, 2.0);
198                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
199                 presetspec.put( ComponentPreset.THICKNESS, 15.0);
200                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
201
202                 assertEquals(0.5,preset.get(ComponentPreset.THICKNESS).doubleValue(),0.0);
203         }
204
205         @Test
206         public void testComputeDensityNoMaterial() throws Exception {
207                 TypedPropertyMap presetspec = new TypedPropertyMap();
208                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
209                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
210                 presetspec.put( ComponentPreset.PARTNO, "partno");
211                 presetspec.put( ComponentPreset.LENGTH, 2.0);
212                 presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
213                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
214                 presetspec.put( ComponentPreset.MASS, 100.0);
215                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
216
217                 // Compute the volume by hand here using a slightly different formula from
218                 // the real implementation.  The magic numbers are based on the 
219                 // constants put into the presetspec above.
220                 double volume = /*outer area*/ (Math.PI * 1.0) - /* inner area */ (Math.PI * .25);
221                 volume *= 2.0; /* times length */
222
223                 double density = 100.0 / volume;
224
225                 assertEquals("CenteringRingCustom",preset.get(ComponentPreset.MATERIAL).getName());
226                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
227         }
228
229         @Test
230         public void testMaterial() throws Exception {
231                 TypedPropertyMap presetspec = new TypedPropertyMap();
232                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
233                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
234                 presetspec.put( ComponentPreset.PARTNO, "partno");
235                 presetspec.put( ComponentPreset.LENGTH, 2.0);
236                 presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
237                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
238                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
239                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
240
241                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
242                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
243
244         }
245
246         @Test
247         public void testComputeDensityWithMaterial() throws Exception {
248                 TypedPropertyMap presetspec = new TypedPropertyMap();
249                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.CENTERING_RING);
250                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
251                 presetspec.put( ComponentPreset.PARTNO, "partno");
252                 presetspec.put( ComponentPreset.LENGTH, 2.0);
253                 presetspec.put( ComponentPreset.OUTER_DIAMETER, 2.0);
254                 presetspec.put( ComponentPreset.INNER_DIAMETER, 1.0);
255                 presetspec.put( ComponentPreset.MASS, 100.0);
256                 presetspec.put( ComponentPreset.MATERIAL, Material.newUserMaterial(Material.Type.BULK,"test", 2.0));
257                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
258
259                 // Compute the volume by hand here using a slightly different formula from
260                 // the real implementation.  The magic numbers are based on the 
261                 // constants put into the presetspec above.
262                 double volume = /*outer area*/ (Math.PI * 1.0) - /* inner area */ (Math.PI * .25);
263                 volume *= 2.0; /* times length */
264
265                 double density = 100.0 / volume;
266
267                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
268                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
269         }
270
271 }