@Test public void checkVisitation() { // Create a new component to visit. PlantComposite component = new PlantComposite(); // Create an invalid visitor, and try to visit the component. FakeComponentVisitor visitor = null; component.accept(visitor); // Check that the component wasn't visited yet. assertFalse(wasVisited); // Create a valid visitor, and try to visit the component. visitor = new FakeComponentVisitor(); component.accept(visitor); // Check that the component was visited. assertTrue(wasVisited); // Grab the visitor's visited component. Component visitorComponent = visitor.component; // Check that the visitor's component is the same component we initially // created. assertTrue(component == visitorComponent); assertTrue(component.equals(visitorComponent)); // ---- Check PlantComponent visitation. ---- // wasVisited = false; // Create an invalid visitor, and try to visit the component. FakePlantComponentVisitor plantVisitor = null; component.accept(plantVisitor); // Check that the component wasn't visited yet. assertFalse(wasVisited); // Create a valid visitor, and try to visit the component. plantVisitor = new FakePlantComponentVisitor(); component.accept(plantVisitor); // Check that the component was visited. assertTrue(wasVisited); // Grab the visitor's visited component. PlantComponent visitorPlantComponent = plantVisitor.component; // Check that the visitor's component is the same component we initially // created. assertTrue(component == visitorPlantComponent); assertTrue(component.equals(visitorPlantComponent)); return; }
/** Checks the copy and clone methods for PlantComposites. */ @Test public void checkCopying() { // Initialize objects for testing. PlantComposite object = new PlantComposite(); PlantComposite copy = new PlantComposite(); PlantComposite clone = null; // Set up the object. object.addPlantComponent(new PlantComponent("bumblebee")); // Make sure the objects are not equal before copying. assertFalse(object == copy); assertFalse(object.equals(copy)); // Copy the object. copy.copy(object); // Make sure the references are different but contents the same. assertFalse(object == copy); assertTrue(object.equals(copy)); // Do the same for the clone operation. // Make sure the objects are not equal before copying. assertFalse(object == clone); assertFalse(object.equals(clone)); // Clone the object. clone = (PlantComposite) object.clone(); // Make sure the references are different but contents the same. assertFalse(object == clone); assertTrue(object.equals(clone)); assertFalse(copy == clone); assertTrue(copy.equals(clone)); return; }
/** * Checks equality and hash codes for PlantComposites. All child components from two composites * should be compared. */ @Test public void checkEquality() { // Initialize objects for testing. PlantComposite object = new PlantComposite(); PlantComposite equalObject = new PlantComposite(); // Set up the two objects. object.addPlantComponent(new PlantComponent("optimus")); equalObject.addPlantComponent(new PlantComponent("optimus")); // Make sure the references are different but the objects are equal. assertFalse(object == equalObject); assertTrue(object.equals(equalObject)); assertTrue(equalObject.equals(object)); // Check the hash codes. assertEquals(object.hashCode(), equalObject.hashCode()); // ---- Check inequality. ---- // // Create an unequal object. PlantComposite unequalObject = equalObject; unequalObject.getComponents().get(0).setName("megatron"); // Make sure the references are different and the objects unequal. assertFalse(object == equalObject); assertFalse(object.equals(unequalObject)); assertFalse(unequalObject.equals(object)); // Check the hash codes. assertFalse(object.hashCode() == unequalObject.hashCode()); // Try other invalid objects. assertFalse(object == null); assertFalse("starscream".equals(object)); return; }