create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / preset / BulkHeadPresetTests.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 BULK_HEAD 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 BulkHeadPresetTests {
19         
20         @Test
21         public void testManufacturerRequired() {
22                 try {
23                         TypedPropertyMap presetspec = new TypedPropertyMap();
24                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
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                                                         ComponentPreset.OUTER_DIAMETER
33                                         },
34                                         new String[] {
35                                                         "No Manufacturer specified",
36                                                         "No PartNo specified",
37                                                         "No Length specified",
38                                                         "No OuterDiameter specified"
39                                         }
40                                         );
41                 }
42         }
43         
44         @Test
45         public void testPartNoRequired() {
46                 try {
47                         TypedPropertyMap presetspec = new TypedPropertyMap();
48                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
49                         presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
50                         ComponentPresetFactory.create(presetspec);
51                 } catch (InvalidComponentPresetException ex) {
52                         PresetAssertHelper.assertInvalidPresetException(ex,
53                                         new TypedKey<?>[] {
54                                                         ComponentPreset.PARTNO,
55                                                         ComponentPreset.LENGTH,
56                                                         ComponentPreset.OUTER_DIAMETER
57                                         },
58                                         new String[] {
59                                                         "No PartNo specified",
60                                                         "No Length specified",
61                                                         "No OuterDiameter specified"
62                                         }
63                                         );
64                 }
65         }
66         
67         @Test
68         public void testLengthRequired() {
69                 try {
70                         TypedPropertyMap presetspec = new TypedPropertyMap();
71                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
72                         presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
73                         presetspec.put(ComponentPreset.PARTNO, "partno");
74                         ComponentPresetFactory.create(presetspec);
75                 } catch (InvalidComponentPresetException ex) {
76                         PresetAssertHelper.assertInvalidPresetException(ex,
77                                         new TypedKey<?>[] {
78                                                         ComponentPreset.LENGTH,
79                                                         ComponentPreset.OUTER_DIAMETER
80                                         },
81                                         new String[] {
82                                                         "No Length specified",
83                                                         "No OuterDiameter specified"
84                                         }
85                                         );
86                 }
87         }
88         
89         @Test
90         public void testOuterDiameterRequired() {
91                 try {
92                         TypedPropertyMap presetspec = new TypedPropertyMap();
93                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
94                         presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
95                         presetspec.put(ComponentPreset.PARTNO, "partno");
96                         presetspec.put(ComponentPreset.LENGTH, 2.0);
97                         ComponentPresetFactory.create(presetspec);
98                 } catch (InvalidComponentPresetException ex) {
99                         PresetAssertHelper.assertInvalidPresetException(ex,
100                                         new TypedKey<?>[] {
101                                         ComponentPreset.OUTER_DIAMETER
102                                         },
103                                         new String[] {
104                                         "No OuterDiameter specified"
105                                         }
106                                         );
107                 }
108         }
109         
110         @Test
111         public void testComputeDensityNoMaterial() throws Exception {
112                 TypedPropertyMap presetspec = new TypedPropertyMap();
113                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
114                 presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
115                 presetspec.put(ComponentPreset.PARTNO, "partno");
116                 presetspec.put(ComponentPreset.LENGTH, 2.0);
117                 presetspec.put(ComponentPreset.OUTER_DIAMETER, 2.0);
118                 presetspec.put(ComponentPreset.MASS, 100.0);
119                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
120                 
121                 // Compute the volume by hand here using a slightly different formula from
122                 // the real implementation.  The magic numbers are based on the 
123                 // constants put into the presetspec above.
124                 double volume = /*outer area*/(Math.PI * 1.0);
125                 volume *= 2.0; /* times length */
126                 
127                 double density = 100.0 / volume;
128                 
129                 assertEquals("BulkHeadCustom", preset.get(ComponentPreset.MATERIAL).getName());
130                 assertEquals(density, preset.get(ComponentPreset.MATERIAL).getDensity(), 0.0005);
131         }
132         
133         @Test
134         public void testMaterial() throws Exception {
135                 TypedPropertyMap presetspec = new TypedPropertyMap();
136                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
137                 presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
138                 presetspec.put(ComponentPreset.PARTNO, "partno");
139                 presetspec.put(ComponentPreset.LENGTH, 2.0);
140                 presetspec.put(ComponentPreset.OUTER_DIAMETER, 2.0);
141                 presetspec.put(ComponentPreset.MATERIAL, Material.newMaterial(Material.Type.BULK, "test", 2.0, true));
142                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
143                 
144                 assertEquals("test", preset.get(ComponentPreset.MATERIAL).getName());
145                 assertEquals(2.0, preset.get(ComponentPreset.MATERIAL).getDensity(), 0.0005);
146                 
147         }
148         
149         @Test
150         public void testComputeDensityWithMaterial() throws Exception {
151                 TypedPropertyMap presetspec = new TypedPropertyMap();
152                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.BULK_HEAD);
153                 presetspec.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
154                 presetspec.put(ComponentPreset.PARTNO, "partno");
155                 presetspec.put(ComponentPreset.LENGTH, 2.0);
156                 presetspec.put(ComponentPreset.OUTER_DIAMETER, 2.0);
157                 presetspec.put(ComponentPreset.MASS, 100.0);
158                 presetspec.put(ComponentPreset.MATERIAL, Material.newMaterial(Material.Type.BULK, "test", 2.0, true));
159                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
160                 
161                 // Compute the volume by hand here using a slightly different formula from
162                 // the real implementation.  The magic numbers are based on the 
163                 // constants put into the presetspec above.
164                 double volume = /*outer area*/(Math.PI * 1.0);
165                 volume *= 2.0; /* times length */
166                 
167                 double density = 100.0 / volume;
168                 
169                 assertEquals("test", preset.get(ComponentPreset.MATERIAL).getName());
170                 assertEquals(density, preset.get(ComponentPreset.MATERIAL).getDensity(), 0.0005);
171         }
172         
173 }