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