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