/** 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; }
/** 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()); }
/** 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); }