/** * Checks the construction of a PlantComposite. Its properties inherited from PlantComponent * should be their defaults, and its child component lists should be empty and not null. */ @Test public void checkConstruction() { // Create a composite to test. PlantComposite composite = new PlantComposite(); // Check the name, ID, and description. assertEquals("Plant Composite 1", composite.getName()); assertEquals(1, composite.getId()); assertEquals("Container for plant-level reactor components", composite.getDescription()); // The current set of components in the Composite should be empty. assertEquals(0, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertTrue(composite.getComponents().isEmpty()); assertNotNull(composite.getPlantComponents()); assertTrue(composite.getPlantComponents().isEmpty()); 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; }
/** Checks the ability to add PlantComponents to the Composite. */ @Test public void checkCompositeImplementation() { // Create a PlantComposite for testing. PlantComposite composite = new PlantComposite(); // Create a PlantComponent for adding to the composite. PlantComponent component = new PlantComponent("snake eyes"); // ---- addComponent should do nothing. ---- // composite.addComponent(component); // None of the lists should contain components. assertEquals(0, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertTrue(composite.getComponents().isEmpty()); assertNotNull(composite.getPlantComponents()); assertTrue(composite.getPlantComponents().isEmpty()); // ---- addPlantComponent should add a PlantComponent. ---- // composite.addPlantComponent(component); // There should be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // Adding a duplicate component should make no difference. composite.addPlantComponent(component); // There should be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // Adding a different component with the same ID should not work. composite.addPlantComponent(new PlantComponent("stormshadow")); // There should be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // ---- removeComponent should remove a PlantComponent. ---- // composite.removeComponent(component.getId()); // None of the lists should contain components. assertEquals(0, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertTrue(composite.getComponents().isEmpty()); assertNotNull(composite.getPlantComponents()); assertTrue(composite.getPlantComponents().isEmpty()); // We should no longer be able to get the same component out. assertNull(composite.getComponent(component.getId())); assertNull(composite.getPlantComponent(component.getId())); return; }
/** * Checks that registered IPlantCompositeListeners are notified correctly when PlantComponents are * added or removed from the PlantComposite. */ @Test public void checkCompositeListeners() { // Create a PlantComposite for testing. PlantComposite composite = new PlantComposite(); // Create a listener for testing the IPlantCompositeListener interface. TestPlantCompositeListener listener = new TestPlantCompositeListener(); composite.registerPlantCompositeListener(listener); // Create a PlantComponent for adding to the composite. PlantComponent component = new PlantComponent("roadblock"); // ---- addComponent should do nothing. ---- // composite.addComponent(component); // None of the lists should contain components. assertEquals(0, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertTrue(composite.getComponents().isEmpty()); assertNotNull(composite.getPlantComponents()); assertTrue(composite.getPlantComponents().isEmpty()); // ---- addPlantComponent should add a PlantComponent. ---- // composite.addPlantComponent(component); // There should be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // The listener should have been notified of the added component. assertTrue(listener.wasNotified()); assertSame(component, listener.getAddedComponents().get(0)); listener.reset(); // Adding a duplicate component should make no difference. composite.addPlantComponent(component); // There should still be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // The listener should not have been notified. assertFalse(listener.wasNotified()); listener.reset(); // Adding a different component with the same ID should not work. composite.addPlantComponent(new PlantComponent("stormshadow")); // There should be one component. assertEquals(1, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertEquals(1, composite.getComponents().size()); assertNotNull(composite.getPlantComponents()); assertEquals(1, composite.getPlantComponents().size()); // We should be able to get the same component out. assertSame(component, composite.getComponent(component.getId())); assertSame(component, composite.getPlantComponent(component.getId())); // ---- removeComponent should remove a PlantComponent. ---- // composite.removeComponent(component.getId()); // None of the lists should contain components. assertEquals(0, composite.getNumberOfComponents()); assertNotNull(composite.getComponents()); assertTrue(composite.getComponents().isEmpty()); assertNotNull(composite.getPlantComponents()); assertTrue(composite.getPlantComponents().isEmpty()); // We should no longer be able to get the same component out. assertNull(composite.getComponent(component.getId())); assertNull(composite.getPlantComponent(component.getId())); // The listener should have been notified of the removed component. assertTrue(listener.wasNotified()); assertSame(component, listener.getRemovedComponents().get(0)); listener.reset(); return; }