/** This operation checks that the Material class can be loaded and written with JAXB. */ @Test public void checkPersistence() { // Local Declarations ICEJAXBHandler xmlHandler = new ICEJAXBHandler(); ArrayList<Class> classList = new ArrayList<Class>(); classList.add(Material.class); // Use the ICE JAXB Manipulator instead of raw JAXB. Waste not want not. ICEJAXBHandler jaxbHandler = new ICEJAXBHandler(); // Create a Material that will be written to XML Material material = TestMaterialFactory.createCO2(); try { // Write the material to a byte stream so that it can be converted // easily and read back in. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); jaxbHandler.write(material, classList, outputStream); // Read it back from the stream into a second Material by converting // the output stream into a byte array and then an input stream. ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); Material readMaterial = (Material) jaxbHandler.read(classList, inputStream); // They should be equal. assertTrue(readMaterial.equals(material)); } catch (NullPointerException | JAXBException | IOException e) { // Just learned about Multicatch! Is this not the coolest #!@%? e.printStackTrace(); } return; }
/** * Test method for {@link * org.eclipse.ice.materials.MaterialWritableTableFormat#setColumnValue(org.eclipse.ice.datastructures.form.Material, * java.lang.Object, int)} . */ @Test public void testSetColumnValue() { assertEquals(material, tableFormat.setColumnValue(material, 2.5, 0)); // Make sure that a blank material's name can be set Material blank = new Material(); String newName = "New Material"; assertEquals(blank, tableFormat.setColumnValue(blank, newName, 0)); assertEquals(blank.getName(), newName); }
/** This operation checks that Material.copy() and Material.clone() work. */ @Test public void checkCopying() { // Create a material to copy and check Material material = TestMaterialFactory.createCO2(); Material materialCopy = new Material(), materialClone = null; // Copy it and check it materialCopy.copy(material); // Make sure they are equal assertTrue(material.equals(materialCopy)); // Checking cloning materialClone = (Material) material.clone(); assertEquals(material, materialClone); }
/** @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { // Create the Material for the test material = new Material(); material.setName("Fluffy"); material.setProperty("A", 1.0); material.setProperty("B", 2.0); // Store the property names columnNames = new ArrayList<String>(); columnNames.addAll(material.getProperties().keySet()); // Create the table format tableFormat = new MaterialWritableTableFormat(columnNames); return; }
/** * This operation creates a simple H2O Material that can be used for testing. * * @return the test material */ public static Material createH2O() { // Create a simple H2O Material Material testMaterial = new Material(); testMaterial.setName("H2O"); testMaterial.setProperty("molar mass (g/mol)", 18.01); // Vapor pressure at 273 K testMaterial.setProperty("vapor pressure (MPa)", 611.0e-6); // Create component 1 - Hydrogen Material hydrogen = new Material(); hydrogen.setName("H"); // Create component 2 - Oxygen Material oxygen = new Material(); oxygen.setName("O"); // Add them as components testMaterial.addComponent(hydrogen); testMaterial.addComponent(hydrogen); testMaterial.addComponent(oxygen); return testMaterial; }
/** * This operation creates a simple CO2 Material that can be used for testing. * * @return the test material */ public static Material createCO2() { // Create a simple CO2 Material Material testMaterial = new Material(); testMaterial.setName("CO2"); testMaterial.setProperty("molar mass (g/mol)", 44.01); testMaterial.setProperty("vapor pressure (MPa)", 5.73); // Create component 1 - Carbon Material carbon = new Material(); carbon.setName("C"); // Create component 2 - Oxygen Material oxygen = new Material(); oxygen.setName("O"); // Note that the "size" of O is 2 since we need O2 // Add them as components testMaterial.addComponent(carbon); testMaterial.addComponent(oxygen); testMaterial.addComponent(oxygen); return testMaterial; }
/** * This operation checks that the Material class can properly manage a set of component Materials * that define its detailed composition. */ @Test public void checkComponents() { // Local Declarations Material testMaterial = TestMaterialFactory.createCO2(); // Check its components assertNotNull(testMaterial.getComponents()); List<MaterialStack> components = testMaterial.getComponents(); assertEquals(2, components.size()); // Get the Materials Material firstMaterial = components.get(0).getMaterial(); Material secondMaterial = components.get(1).getMaterial(); // Check them in an order independent way. It is enough to check that // the components are there. There is no need to check their sizes. assertTrue( ("C".equals(firstMaterial.getName()) && "O".equals(secondMaterial.getName())) || ("O".equals(firstMaterial.getName()) && "C".equals(secondMaterial.getName()))); }
/** This operation checks that Materials.equals() and Materials.hashCode() work. */ @Test public void checkEquality() { // Create two test materials to compare against each other Material testMat1 = TestMaterialFactory.createCO2(); Material testMat2 = TestMaterialFactory.createCO2(); // They should be equal assertTrue(testMat1.equals(testMat2)); // Make sure that a self comparison works assertTrue(testMat1.equals(testMat1)); // Make sure that passing something else in fails assertFalse(testMat1.equals(1)); // Check that the hash code doesn't change with no changes in state assertEquals(testMat1.hashCode(), testMat1.hashCode()); // Check that they have the same hash codes assertEquals(testMat1.hashCode(), testMat2.hashCode()); }
/** * Test method for {@link * org.eclipse.ice.materials.MaterialWritableTableFormat#getColumnValue(org.eclipse.ice.datastructures.form.Material, * int)} . */ @Test public void testGetColumnValue() { // Reset the property values since the tests can run out of order. material.setProperty("A", 1.0); material.setProperty("B", 2.0); // Check that the value is either the value for A or the value for B. // Since there is no guarantee on order, this is the only good way to do // it. Object value = tableFormat.getColumnValue(material, 0); assertEquals(value, "Fluffy"); Double dValue = (Double) tableFormat.getColumnValue(material, 1); assertTrue(dValue.equals(1.0) || dValue.equals(2.0)); dValue = (Double) tableFormat.getColumnValue(material, 2); assertTrue(dValue.equals(1.0) || dValue.equals(2.0)); return; }
/** * This operation checks the Material class to make sure that simple attributes such as names and * properties can be changed. */ @Test public void checkSimpleAttributes() { // Local Declarations Material testMaterial = TestMaterialFactory.createCO2(); // Check the name testMaterial.setName("CO2"); assertEquals("CO2", testMaterial.getName()); // Check properties - just test handling a few testMaterial.setProperty("molar mass (g/mol)", 44.01); double mass = testMaterial.getProperty("molar mass (g/mol)"); assertEquals(44.01, mass, 1.0e-16); testMaterial.setProperty("vapor pressure (MPa)", 5.73); double pressure = testMaterial.getProperty("vapor pressure (MPa)"); assertEquals(5.73, pressure, 1.0e-16); // Get all the properties and make sure they have molar mass and vapor // pressure. Map<String, Double> properties = testMaterial.getProperties(); assertTrue(properties.containsKey("molar mass (g/mol)")); assertTrue(properties.containsKey("vapor pressure (MPa)")); // Make sure that requesting a property that isn't in the map returns // 0.0. assertEquals(0.0, testMaterial.getProperty("penguin"), 1.0e-8); // Make some more test materials Material carbon = new Material(); carbon.setName("C"); Material c1 = new Material(); c1.setName("12C"); // Test the get isotopic number and get elemental name assertEquals(12, c1.getIsotopicNumber()); assertEquals(carbon.getName(), carbon.getElementalName()); assertEquals(0, carbon.getIsotopicNumber()); assertEquals(carbon.getName(), c1.getElementalName()); return; }
/** * This operation checks that a set of materials can properly sort itself using the compare to * method on the material's names. For example, sorting a list such as b, c, 1c, d, f, 5c should * give the resulting order of a, b, c, 1c, 5c, d, f. */ @Test public void checkSorting() { // Make some test materials Material A = new Material(); A.setName("A"); Material B = new Material(); B.setName("B"); Material co2 = TestMaterialFactory.createCO2(); Material B1 = new Material(); B1.setName("3B"); // Make sure that alphabetical order is followed assertTrue(A.compareTo(B) < 0); // Make sure that isotopes go in numeric order assertTrue(B.compareTo(B1) < 0); // Make sure that a blank material would be at the front of a list assertTrue(A.compareTo(new Material()) > 0); // Make sure that if materials have the same name they are the same when // compared. Material A1 = new Material(); A1.setName(A.getName()); assertTrue(A.compareTo(A1) == 0); // Make sure that the compound is in proper alphabetic order (no numeric // sorting). assertTrue(co2.compareTo(B1) > 0); Material C = new Material(); C.setName("C"); assertTrue(C.compareTo(co2) < 0); }