Repository: incubator-brooklyn Updated Branches: refs/heads/master 371009293 -> c5536105e
Add EntityDynamicType.removeEffector(Effector) Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/42d24535 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/42d24535 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/42d24535 Branch: refs/heads/master Commit: 42d245355e09b341f95730471be4a0bb1e8c15b4 Parents: 3e36892 Author: Sam Corbett <[email protected]> Authored: Mon Jul 20 12:09:23 2015 +0100 Committer: Sam Corbett <[email protected]> Committed: Mon Jul 20 12:09:23 2015 +0100 ---------------------------------------------------------------------- .../entity/basic/EntityDynamicType.java | 25 +++++++++- .../entity/basic/DynamicEntityTest.java | 52 +++++++++----------- 2 files changed, 47 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/42d24535/core/src/main/java/brooklyn/entity/basic/EntityDynamicType.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/entity/basic/EntityDynamicType.java b/core/src/main/java/brooklyn/entity/basic/EntityDynamicType.java index 14500d3..c4a455b 100644 --- a/core/src/main/java/brooklyn/entity/basic/EntityDynamicType.java +++ b/core/src/main/java/brooklyn/entity/basic/EntityDynamicType.java @@ -130,17 +130,38 @@ public class EntityDynamicType extends BrooklynDynamicType<Entity, AbstractEntit instance.emit(AbstractEntity.EFFECTOR_ADDED, newEffector.getName()); } - /** Adds an effector with an explicit body */ + /** + * Adds an effector with an explicit body to this entity. + */ @Beta public <T> void addEffector(Effector<T> effector, EffectorTaskFactory<T> body) { addEffector(new EffectorAndBody<T>(effector, body)); } - /** Adds an effector with an explicit body */ + + /** + * Adds an effector with an explicit body to this entity. + */ @Beta public <T> void addEffector(Effector<T> effector, EffectorBody<T> body) { addEffector(effector, new EffectorBodyTaskFactory<T>(body)); } + /** + * Removes the given {@link Effector} from this entity. + * <p> + * Note that if the argument is an instance of {@link EffectorWithBody} it will + * still be possible to invoke the effector on the entity by calling + * <code>entity.invoke(effector, argumentsMap)</code>. + */ + @Beta + public void removeEffector(Effector<?> effector) { + Effector<?> removed = effectors.remove(effector.getName()); + invalidateSnapshot(); + if (removed != null) { + instance.emit(AbstractEntity.EFFECTOR_REMOVED, removed.getName()); + } + } + // -------------------------------------------------- /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/42d24535/core/src/test/java/brooklyn/entity/basic/DynamicEntityTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/entity/basic/DynamicEntityTest.java b/core/src/test/java/brooklyn/entity/basic/DynamicEntityTest.java index 1b5928d..221e0c2 100644 --- a/core/src/test/java/brooklyn/entity/basic/DynamicEntityTest.java +++ b/core/src/test/java/brooklyn/entity/basic/DynamicEntityTest.java @@ -18,45 +18,41 @@ */ package brooklyn.entity.basic; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; + import org.testng.annotations.Test; -import brooklyn.entity.Application; +import brooklyn.entity.BrooklynAppUnitTestSupport; import brooklyn.entity.effector.EffectorTaskTest; import brooklyn.entity.proxying.EntityInitializer; import brooklyn.entity.proxying.EntitySpec; -import brooklyn.test.entity.LocalManagementContextForTests; +import brooklyn.test.entity.TestEntity; import brooklyn.util.collections.MutableMap; -public class DynamicEntityTest { +public class DynamicEntityTest extends BrooklynAppUnitTestSupport { - Application app; - - @BeforeMethod(alwaysRun=true) - public void setup() throws Exception { - app = ApplicationBuilder.newManagedApp(EntitySpec.create(BasicApplication.class), LocalManagementContextForTests.newInstance()); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); + @Test + public void testEffectorAddedDuringInit() { + BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) + .addInitializer(new EntityInitializer() { + public void apply(EntityLocal entity) { + ((EntityInternal) entity).getMutableEntityType().addEffector(EffectorTaskTest.DOUBLE_1); + } + })); + assertEquals(entity.invoke(EffectorTaskTest.DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 5)).getUnchecked(), (Integer) 10); } @Test - public void testEffectorAddedDuringInit() { - BasicEntity entity = app.addChild(EntitySpec.create(BasicEntity.class) - .addInitializer(new EntityInitializer() { - public void apply(EntityLocal entity) { - ((EntityInternal)entity).getMutableEntityType().addEffector(EffectorTaskTest.DOUBLE_1); - } - })); - // TODO why doesn't the call to addChild above automatically manage the child (now that we use specs for creation) ? - // (if there is a good reason, put it in addChild!) - Entities.manage(entity); - - Assert.assertEquals(entity.invoke(EffectorTaskTest.DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 5)).getUnchecked(), (Integer)10); + public void testEffectorRemovedDuringInit() { + TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) + .addInitializer(new EntityInitializer() { + @Override + public void apply(EntityLocal entity) { + ((EntityInternal) entity).getMutableEntityType().removeEffector(TestEntity.IDENTITY_EFFECTOR); + } + })); + assertFalse(entity.getMutableEntityType().getEffectors().containsKey(TestEntity.IDENTITY_EFFECTOR.getName())); } }
