f36d026efe08e771cd358ce7f029ee1e210eec01
[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 static org.junit.Assert.assertTrue;
5 import net.sf.openrocket.material.Material;
6 import net.sf.openrocket.motor.Manufacturer;
7 import net.sf.openrocket.rocketcomponent.Transition;
8 import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
9
10 import org.junit.Test;
11
12 /**
13  * Test construction of NOSE_CONE type ComponentPresets based on TypedPropertyMap through the
14  * ComponentPresetFactory.create() method.
15  * 
16  * Ensure required properties are populated
17  * 
18  * Ensure any computed values are correctly computed.
19  * 
20  */
21 public class NoseConePresetTests extends BaseTestCase {
22
23         @Test
24         public void testManufacturerRequired() {
25                 try {
26                         TypedPropertyMap presetspec = new TypedPropertyMap();
27                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
28                         ComponentPresetFactory.create(presetspec);
29                 } catch ( InvalidComponentPresetException ex ) {
30                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Manufacturer specified"));
31                 }
32         }
33
34         @Test
35         public void testPartNoRequired() {
36                 try {
37                         TypedPropertyMap presetspec = new TypedPropertyMap();
38                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
39                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
40                         ComponentPresetFactory.create(presetspec);
41                 } catch ( InvalidComponentPresetException ex ) {
42                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No PartNo specified"));
43                 }
44         }
45
46         @Test
47         public void testLengthRequired() {
48                 try {
49                         TypedPropertyMap presetspec = new TypedPropertyMap();
50                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
51                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
52                         presetspec.put( ComponentPreset.PARTNO, "partno");
53                         ComponentPresetFactory.create(presetspec);
54                 } catch ( InvalidComponentPresetException ex ) {
55                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Length specified"));
56                 }
57         }
58
59         @Test
60         public void testShapeRequired() {
61                 try {
62                         TypedPropertyMap presetspec = new TypedPropertyMap();
63                         presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
64                         presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
65                         presetspec.put( ComponentPreset.PARTNO, "partno");
66                         presetspec.put( ComponentPreset.LENGTH, 2.0);
67                         ComponentPresetFactory.create(presetspec);
68                 } catch ( InvalidComponentPresetException ex ) {
69                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Shape"));
70                 }
71         }
72
73         @Test
74         public void testAftOuterDiameterRequired() {
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                         presetspec.put( ComponentPreset.LENGTH, 2.0);
81                         presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
82                         ComponentPresetFactory.create(presetspec);
83                 } catch ( InvalidComponentPresetException ex ) {
84                         assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No AftOuterDiameter"));
85                 }
86         }
87
88         @Test
89         public void testComputeDensityNoMaterial() throws Exception {
90                 TypedPropertyMap presetspec = new TypedPropertyMap();
91                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
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.SHAPE, Transition.Shape.CONICAL);
96                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
97                 presetspec.put( ComponentPreset.FILLED, true);
98                 presetspec.put( ComponentPreset.MASS, 100.0);
99                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
100
101                 // constants put into the presetspec above.
102                 double volume = /*base area*/ Math.PI;
103                 volume *= 2.0 /* times height */ / 3.0; /* one third */
104                 
105                 double density = 100.0 / volume;
106                 
107                 assertEquals("NoseConeCustom",preset.get(ComponentPreset.MATERIAL).getName());
108                 // note - epsilon is 1% of the simple computation of density
109                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
110         }
111
112         @Test
113         public void testMaterial() throws Exception {
114                 TypedPropertyMap presetspec = new TypedPropertyMap();
115                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
116                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
117                 presetspec.put( ComponentPreset.PARTNO, "partno");
118                 presetspec.put( ComponentPreset.LENGTH, 2.0);
119                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
120                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
121                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
122                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
123
124                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
125                 assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
126                 
127         }
128
129         @Test
130         public void testComputeDensityWithMaterial() throws Exception {
131                 TypedPropertyMap presetspec = new TypedPropertyMap();
132                 presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
133                 presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
134                 presetspec.put( ComponentPreset.PARTNO, "partno");
135                 presetspec.put( ComponentPreset.LENGTH, 2.0);
136                 presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
137                 presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
138                 presetspec.put( ComponentPreset.FILLED, true);
139                 presetspec.put( ComponentPreset.MASS, 100.0);
140                 presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
141                 ComponentPreset preset = ComponentPresetFactory.create(presetspec);
142
143                 // constants put into the presetspec above.
144                 double volume = /*base area*/ Math.PI;
145                 volume *= 2.0 /* times height */ / 3.0; /* one third */
146                 
147                 double density = 100.0 / volume;
148                 
149                 assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
150                 // note - epsilon is 1% of the simple computation of density
151                 assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),0.01*density);
152         }
153
154 }