Change nose cone preset to use Aft Outer Diameter, Aft Shoulder Length, and Aft Shoul...
[debian/openrocket] / core / test / net / sf / openrocket / preset / NoseConePresetTests.java
diff --git a/core/test/net/sf/openrocket/preset/NoseConePresetTests.java b/core/test/net/sf/openrocket/preset/NoseConePresetTests.java
new file mode 100644 (file)
index 0000000..14832a2
--- /dev/null
@@ -0,0 +1,156 @@
+package net.sf.openrocket.preset;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import net.sf.openrocket.material.Material;
+import net.sf.openrocket.motor.Manufacturer;
+import net.sf.openrocket.rocketcomponent.Transition;
+import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
+
+import org.junit.Test;
+
+/**
+ * Test construction of NOSE_CONE type ComponentPresets based on TypedPropertyMap through the
+ * ComponentPresetFactory.create() method.
+ * 
+ * Ensure required properties are populated
+ * 
+ * Ensure any computed values are correctly computed.
+ * 
+ */
+public class NoseConePresetTests extends BaseTestCase {
+
+       @Test
+       public void testManufacturerRequired() {
+               try {
+                       TypedPropertyMap presetspec = new TypedPropertyMap();
+                       presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+                       ComponentPresetFactory.create(presetspec);
+               } catch ( InvalidComponentPresetException ex ) {
+                       assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Manufacturer specified"));
+               }
+       }
+
+       @Test
+       public void testPartNoRequired() {
+               try {
+                       TypedPropertyMap presetspec = new TypedPropertyMap();
+                       presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+                       presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+                       ComponentPresetFactory.create(presetspec);
+               } catch ( InvalidComponentPresetException ex ) {
+                       assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No PartNo specified"));
+               }
+       }
+
+       @Test
+       public void testLengthRequired() {
+               try {
+                       TypedPropertyMap presetspec = new TypedPropertyMap();
+                       presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+                       presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+                       presetspec.put( ComponentPreset.PARTNO, "partno");
+                       ComponentPresetFactory.create(presetspec);
+               } catch ( InvalidComponentPresetException ex ) {
+                       assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Length specified"));
+               }
+       }
+
+       @Test
+       public void testShapeRequired() {
+               try {
+                       TypedPropertyMap presetspec = new TypedPropertyMap();
+                       presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+                       presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+                       presetspec.put( ComponentPreset.PARTNO, "partno");
+                       presetspec.put( ComponentPreset.LENGTH, 2.0);
+                       ComponentPresetFactory.create(presetspec);
+               } catch ( InvalidComponentPresetException ex ) {
+                       assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No Shape"));
+               }
+       }
+
+       @Test
+       public void testAftOuterDiameterRequired() {
+               try {
+                       TypedPropertyMap presetspec = new TypedPropertyMap();
+                       presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+                       presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+                       presetspec.put( ComponentPreset.PARTNO, "partno");
+                       presetspec.put( ComponentPreset.LENGTH, 2.0);
+                       presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
+                       ComponentPresetFactory.create(presetspec);
+               } catch ( InvalidComponentPresetException ex ) {
+                       assertTrue("Wrong Exception Thrown", ex.getMessage().contains("No AftOuterDiameter"));
+               }
+       }
+
+       @Test
+       public void testComputeDensityNoMaterial() throws Exception {
+               TypedPropertyMap presetspec = new TypedPropertyMap();
+               presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+               presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+               presetspec.put( ComponentPreset.PARTNO, "partno");
+               presetspec.put( ComponentPreset.LENGTH, 2.0);
+               presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
+               presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
+               presetspec.put( ComponentPreset.FILLED, true);
+               presetspec.put( ComponentPreset.MASS, 100.0);
+               ComponentPreset preset = ComponentPresetFactory.create(presetspec);
+
+               // constants put into the presetspec above.
+               double volume = /*base area*/ Math.PI;
+               volume *= 2.0 /* times height */ / 3.0; /* one third */
+               
+               double density = 100.0 / volume;
+               
+               assertEquals("NoseConeCustom",preset.get(ComponentPreset.MATERIAL).getName());
+               // FIXME - I would expect the nc volume computation to be closer for such a simple shape.
+               // simple math yields 47.74648
+               // 100.0/nc.getComponentVolume yields 48.7159
+               assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),1.0);
+       }
+
+       @Test
+       public void testMaterial() throws Exception {
+               TypedPropertyMap presetspec = new TypedPropertyMap();
+               presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+               presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+               presetspec.put( ComponentPreset.PARTNO, "partno");
+               presetspec.put( ComponentPreset.LENGTH, 2.0);
+               presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
+               presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
+               presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
+               ComponentPreset preset = ComponentPresetFactory.create(presetspec);
+
+               assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
+               assertEquals(2.0,preset.get(ComponentPreset.MATERIAL).getDensity(),0.0005);
+               
+       }
+
+       @Test
+       public void testComputeDensityWithMaterial() throws Exception {
+               TypedPropertyMap presetspec = new TypedPropertyMap();
+               presetspec.put(ComponentPreset.TYPE, ComponentPreset.Type.NOSE_CONE);
+               presetspec.put( ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer("manufacturer"));
+               presetspec.put( ComponentPreset.PARTNO, "partno");
+               presetspec.put( ComponentPreset.LENGTH, 2.0);
+               presetspec.put( ComponentPreset.SHAPE, Transition.Shape.CONICAL);
+               presetspec.put( ComponentPreset.AFT_OUTER_DIAMETER, 2.0);
+               presetspec.put( ComponentPreset.FILLED, true);
+               presetspec.put( ComponentPreset.MASS, 100.0);
+               presetspec.put( ComponentPreset.MATERIAL, new Material.Bulk("test", 2.0, true));
+               ComponentPreset preset = ComponentPresetFactory.create(presetspec);
+
+               // constants put into the presetspec above.
+               double volume = /*base area*/ Math.PI;
+               volume *= 2.0 /* times height */ / 3.0; /* one third */
+               
+               double density = 100.0 / volume;
+               
+               assertEquals("test",preset.get(ComponentPreset.MATERIAL).getName());
+               // FIXME - I would expect the nc volume computation to be closer for such a simple shape.
+               assertEquals(density,preset.get(ComponentPreset.MATERIAL).getDensity(),1.5);
+       }
+
+}