You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sv...@apache.org on 2016/05/30 14:24:37 UTC
[3/8] brooklyn-server git commit: Merge ConfigMapTest into
EntityConfigTest
Merge ConfigMapTest into EntityConfigTest
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/87335648
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/87335648
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/87335648
Branch: refs/heads/master
Commit: 87335648c04bb36b51023db813dc16a6d7ed80c6
Parents: a8f3e82
Author: Aled Sage <al...@gmail.com>
Authored: Sat May 28 00:40:26 2016 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Sat May 28 15:15:30 2016 +0100
----------------------------------------------------------------------
.../entity/ConfigEntityInheritanceTest.java | 4 +-
.../core/entity/EntityConfigGroovyTest.groovy | 68 +++++
.../brooklyn/core/entity/EntityConfigTest.java | 253 ++++++++++++++++--
.../entity/internal/ConfigMapGroovyTest.groovy | 68 -----
.../core/entity/internal/ConfigMapTest.java | 257 -------------------
5 files changed, 298 insertions(+), 352 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/87335648/core/src/test/java/org/apache/brooklyn/core/entity/ConfigEntityInheritanceTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/ConfigEntityInheritanceTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/ConfigEntityInheritanceTest.java
index 93dff9a..a6b1602 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/ConfigEntityInheritanceTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/ConfigEntityInheritanceTest.java
@@ -24,8 +24,8 @@ import org.apache.brooklyn.config.ConfigInheritance;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.AbstractEntity;
-import org.apache.brooklyn.core.entity.internal.ConfigMapTest.MyOtherEntity;
-import org.apache.brooklyn.core.entity.internal.ConfigMapTest.MyOtherEntityImpl;
+import org.apache.brooklyn.core.entity.EntityConfigTest.MyOtherEntity;
+import org.apache.brooklyn.core.entity.EntityConfigTest.MyOtherEntityImpl;
import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey.IntegerAttributeSensorAndConfigKey;
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/87335648/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigGroovyTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigGroovyTest.groovy b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigGroovyTest.groovy
new file mode 100644
index 0000000..40d66be
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigGroovyTest.groovy
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.entity;
+
+import static org.testng.Assert.assertEquals
+import static org.testng.Assert.assertFalse
+import static org.testng.Assert.assertTrue
+
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.test.entity.TestApplication
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.core.entity.Entities
+import org.apache.brooklyn.core.entity.EntityConfigTest.MyOtherEntity
+import org.apache.brooklyn.core.entity.EntityConfigTest.MySubEntity
+import org.testng.annotations.AfterMethod
+import org.testng.annotations.BeforeMethod
+import org.testng.annotations.Test
+
+import com.google.common.base.Predicate;
+
+public class EntityConfigGroovyTest extends BrooklynAppUnitTestSupport {
+
+ private MySubEntity entity;
+
+ @BeforeMethod(alwaysRun=true)
+ @Override
+ public void setUp() {
+ super.setUp();
+ entity = app.addChild(EntitySpec.create(MySubEntity.class));
+ }
+
+ @Test
+ public void testGetConfigOfTypeClosureReturnsClosure() throws Exception {
+ MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
+ entity2.config().set(MyOtherEntity.CLOSURE_KEY, { return "abc" } );
+
+ Closure configVal = entity2.getConfig(MyOtherEntity.CLOSURE_KEY);
+ assertTrue(configVal instanceof Closure, "configVal="+configVal);
+ assertEquals(configVal.call(), "abc");
+ }
+
+ @Test
+ public void testGetConfigOfPredicateTaskReturnsCoercedClosure() throws Exception {
+ MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
+ entity2.setConfig(MyOtherEntity.PREDICATE_KEY, { return it != null } );
+
+ Predicate<?> predicate = entity2.getConfig(MyOtherEntity.PREDICATE_KEY);
+ assertTrue(predicate instanceof Predicate, "predicate="+predicate);
+ assertTrue(predicate.apply(1));
+ assertFalse(predicate.apply(null));
+ }
+}
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/87335648/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
index 41284fb..2d3fd26 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
@@ -19,45 +19,70 @@
package org.apache.brooklyn.core.entity;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.api.entity.ImplementedBy;
+import org.apache.brooklyn.api.mgmt.ExecutionManager;
import org.apache.brooklyn.api.mgmt.Task;
-import org.apache.brooklyn.api.mgmt.TaskAdaptable;
-import org.apache.brooklyn.api.mgmt.TaskFactory;
import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.config.ConfigMap;
import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
+import org.apache.brooklyn.core.config.ConfigPredicates;
+import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey.IntegerAttributeSensorAndConfigKey;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
import org.apache.brooklyn.core.test.entity.TestEntity;
import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.core.task.BasicTask;
+import org.apache.brooklyn.util.core.task.DeferredSupplier;
import org.apache.brooklyn.util.core.task.Tasks;
+import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.util.concurrent.MoreExecutors;
-public class EntityConfigTest {
+import groovy.lang.Closure;
- private ManagementContext managementContext;
+public class EntityConfigTest extends BrooklynAppUnitTestSupport {
+
+ private static final int TIMEOUT_MS = 10*1000;
+
+ private ExecutorService executor;
+ private ExecutionManager executionManager;
@BeforeMethod(alwaysRun=true)
+ @Override
public void setUp() throws Exception {
- managementContext = LocalManagementContextForTests.newInstance();
+ super.setUp();
+ executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
+ executionManager = mgmt.getExecutionManager();
}
-
+
@AfterMethod(alwaysRun=true)
- public void tearDown() {
- if (managementContext != null) Entities.destroyAll(managementContext);
+ @Override
+ public void tearDown() throws Exception {
+ if (executor != null) executor.shutdownNow();
+ super.tearDown();
}
@Test
public void testConfigBagContainsMatchesForConfigKeyName() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("myentity.myconfig", "myval1")
.configure("myentity.myconfigwithflagname", "myval2"));
@@ -69,7 +94,7 @@ public class EntityConfigTest {
@Test
public void testConfigBagContainsMatchesForFlagName() throws Exception {
// Prefers flag-name, over config-key's name
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("myconfigflagname", "myval"));
assertEquals(entity.getAllConfig(), ImmutableMap.of(MyEntity.MY_CONFIG_WITH_FLAGNAME, "myval"));
@@ -80,7 +105,7 @@ public class EntityConfigTest {
// TODO Which way round should it be?!
@Test(enabled=false)
public void testPrefersFlagNameOverConfigKeyName() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("myconfigflagname", "myval")
.configure("myentity.myconfigwithflagname", "shouldIgnoreAndPreferFlagName"));
@@ -89,7 +114,7 @@ public class EntityConfigTest {
@Test
public void testConfigBagContainsUnmatched() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("notThere", "notThereVal"));
assertEquals(entity.getAllConfig(), ImmutableMap.of(ConfigKeys.newConfigKey(Object.class, "notThere"), "notThereVal"));
@@ -99,12 +124,12 @@ public class EntityConfigTest {
@Test
public void testChildConfigBagInheritsUnmatchedAtParent() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("mychildentity.myconfig", "myval1")
.configure("mychildconfigflagname", "myval2")
.configure("notThere", "notThereVal"));
- EntityInternal child = managementContext.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
+ EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
.parent(entity));
assertEquals(child.getAllConfig(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG, "myval1",
@@ -116,10 +141,10 @@ public class EntityConfigTest {
@Test
public void testChildInheritsFromParent() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("myentity.myconfig", "myval1"));
- EntityInternal child = managementContext.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
+ EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
.parent(entity));
assertEquals(child.getAllConfig(), ImmutableMap.of(MyEntity.MY_CONFIG, "myval1"));
@@ -129,11 +154,11 @@ public class EntityConfigTest {
@Test
public void testChildCanOverrideConfigUsingKeyName() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure("mychildentity.myconfigwithflagname", "myval")
.configure("notThere", "notThereVal"));
- EntityInternal child = managementContext.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
+ EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
.parent(entity)
.configure("mychildentity.myconfigwithflagname", "overrideMyval")
.configure("notThere", "overrideNotThereVal"));
@@ -146,11 +171,11 @@ public class EntityConfigTest {
@Test
public void testChildCanOverrideConfigUsingFlagName() throws Exception {
- EntityInternal entity = managementContext.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
+ EntityInternal entity = mgmt.getEntityManager().createEntity(EntitySpec.create(MyEntity.class)
.configure(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "myval"));
assertEquals(entity.getAllConfig(), ImmutableMap.of(MyChildEntity.MY_CHILD_CONFIG_WITH_FLAGNAME, "myval"));
- EntityInternal child = managementContext.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
+ EntityInternal child = mgmt.getEntityManager().createEntity(EntitySpec.create(MyChildEntity.class)
.parent(entity)
.configure("mychildconfigflagname", "overrideMyval"));
@@ -161,7 +186,7 @@ public class EntityConfigTest {
@Test
public void testGetConfigMapWithSubKeys() throws Exception {
- TestEntity entity = managementContext.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+ TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
.configure(TestEntity.CONF_MAP_THING.subKey("mysub"), "myval"));
assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "myval"));
@@ -173,7 +198,7 @@ public class EntityConfigTest {
@Test
public void testGetConfigMapWithExplicitMap() throws Exception {
- TestEntity entity = managementContext.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+ TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
.configure(TestEntity.CONF_MAP_THING, ImmutableMap.of("mysub", "myval")));
assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "myval"));
@@ -194,7 +219,7 @@ public class EntityConfigTest {
return "myval";
}})
.build();
- TestEntity entity = managementContext.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+ TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
.configure(TestEntity.CONF_MAP_THING_OBJECT, ImmutableMap.<String, Object>of("mysub", task))
.configure(TestEntity.CONF_NAME, task));
@@ -212,6 +237,184 @@ public class EntityConfigTest {
assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING_OBJECT.subKey("mysub")).get(), "myval");
}
+ @Test
+ public void testGetConfigKeysReturnsFromSuperAndInterfacesAndSubClass() throws Exception {
+ MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class));
+ assertEquals(entity.getEntityType().getConfigKeys(), ImmutableSet.of(
+ MySubEntity.SUPER_KEY_1, MySubEntity.SUPER_KEY_2, MySubEntity.SUB_KEY_2, MySubEntity.INTERFACE_KEY_1));
+ }
+
+ @Test
+ public void testConfigKeyDefaultUsesValueInSubClass() throws Exception {
+ MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class));
+ assertEquals(entity.getConfig(MyBaseEntity.SUPER_KEY_1), "overridden superKey1 default");
+ }
+
+ @Test
+ public void testConfigureFromKey() throws Exception {
+ MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class)
+ .configure(MySubEntity.SUPER_KEY_1, "changed"));
+ assertEquals(entity.getConfig(MySubEntity.SUPER_KEY_1), "changed");
+ }
+
+ @Test
+ public void testConfigureFromSuperKey() throws Exception {
+ MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class)
+ .configure(MyBaseEntity.SUPER_KEY_1, "changed"));
+ assertEquals(entity.getConfig(MySubEntity.SUPER_KEY_1), "changed");
+ }
+
+ @Test
+ public void testConfigSubMap() throws Exception {
+ MySubEntity entity = app.addChild(EntitySpec.create(MySubEntity.class));
+ entity.config().set(MyBaseEntity.SUPER_KEY_1, "s1");
+ entity.config().set(MySubEntity.SUB_KEY_2, "s2");
+ ConfigMap sub = entity.getConfigMap().submap(ConfigPredicates.nameMatchesGlob("sup*"));
+ Assert.assertEquals(sub.getConfigRaw(MyBaseEntity.SUPER_KEY_1, true).get(), "s1");
+ Assert.assertFalse(sub.getConfigRaw(MySubEntity.SUB_KEY_2, true).isPresent());
+ }
+
+ @Test(expectedExceptions=IllegalArgumentException.class)
+ public void testFailFastOnInvalidConfigKeyCoercion() throws Exception {
+ MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class));
+ ConfigKey<Integer> key = MyOtherEntity.INT_KEY;
+ entity.config().set((ConfigKey)key, "thisisnotanint");
+ }
+
+ @Test
+ public void testGetConfigWithDeferredSupplierReturnsSupplied() throws Exception {
+ DeferredSupplier<Integer> supplier = new DeferredSupplier<Integer>() {
+ volatile int next = 0;
+ public Integer get() {
+ return next++;
+ }
+ };
+
+ MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class));
+ entity.config().set((ConfigKey)MyOtherEntity.INT_KEY, supplier);
+
+ assertEquals(entity.getConfig(MyOtherEntity.INT_KEY), Integer.valueOf(0));
+ assertEquals(entity.getConfig(MyOtherEntity.INT_KEY), Integer.valueOf(1));
+ }
+
+ @Test
+ public void testGetConfigWithFutureWaitsForResult() throws Exception {
+ LatchingCallable<String> work = new LatchingCallable<String>("abc");
+ Future<String> future = executor.submit(work);
+
+ final MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class));
+ entity.config().set((ConfigKey)MyOtherEntity.STRING_KEY, future);
+
+ Future<String> getConfigFuture = executor.submit(new Callable<String>() {
+ public String call() {
+ return entity.getConfig(MyOtherEntity.STRING_KEY);
+ }});
+
+ assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
+ assertFalse(getConfigFuture.isDone());
+
+ work.latchContinued.countDown();
+ assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
+ }
+
+ @Test
+ public void testGetConfigWithExecutedTaskWaitsForResult() throws Exception {
+ LatchingCallable<String> work = new LatchingCallable<String>("abc");
+ Task<String> task = executionManager.submit(work);
+
+ final MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class)
+ .configure(MyOtherEntity.STRING_KEY, task));
+
+ Future<String> getConfigFuture = executor.submit(new Callable<String>() {
+ public String call() {
+ return entity.getConfig(MyOtherEntity.STRING_KEY);
+ }});
+
+ assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
+ assertFalse(getConfigFuture.isDone());
+
+ work.latchContinued.countDown();
+ assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
+ assertEquals(work.callCount.get(), 1);
+ }
+
+ @Test
+ public void testGetConfigWithUnexecutedTaskIsExecutedAndWaitsForResult() throws Exception {
+ LatchingCallable<String> work = new LatchingCallable<String>("abc");
+ Task<String> task = new BasicTask<String>(work);
+
+ final MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class)
+ .configure(MyOtherEntity.STRING_KEY, task));
+
+ Future<String> getConfigFuture = executor.submit(new Callable<String>() {
+ public String call() {
+ return entity.getConfig(MyOtherEntity.STRING_KEY);
+ }});
+
+ assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
+ assertFalse(getConfigFuture.isDone());
+
+ work.latchContinued.countDown();
+ assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
+ assertEquals(work.callCount.get(), 1);
+ }
+
+ @ImplementedBy(MyBaseEntityImpl.class)
+ public interface MyBaseEntity extends EntityInternal {
+ public static final ConfigKey<String> SUPER_KEY_1 = ConfigKeys.newStringConfigKey("superKey1", "superKey1 key", "superKey1 default");
+ public static final ConfigKey<String> SUPER_KEY_2 = ConfigKeys.newStringConfigKey("superKey2", "superKey2 key", "superKey2 default");
+ }
+
+ public static class MyBaseEntityImpl extends AbstractEntity implements MyBaseEntity {
+ }
+
+ @ImplementedBy(MySubEntityImpl.class)
+ public interface MySubEntity extends MyBaseEntity, MyInterface {
+ public static final ConfigKey<String> SUPER_KEY_1 = ConfigKeys.newConfigKeyWithDefault(MyBaseEntity.SUPER_KEY_1, "overridden superKey1 default");
+ public static final ConfigKey<String> SUB_KEY_2 = ConfigKeys.newStringConfigKey("subKey2", "subKey2 key", "subKey2 default");
+ }
+
+ public static class MySubEntityImpl extends MyBaseEntityImpl implements MySubEntity {
+ }
+
+ public interface MyInterface {
+ public static final ConfigKey<String> INTERFACE_KEY_1 = ConfigKeys.newStringConfigKey("interfaceKey1", "interface key 1", "interfaceKey1 default");
+ }
+
+ @ImplementedBy(MyOtherEntityImpl.class)
+ public interface MyOtherEntity extends Entity {
+ public static final ConfigKey<Integer> INT_KEY = ConfigKeys.newIntegerConfigKey("intKey", "int key", 1);
+ public static final ConfigKey<String> STRING_KEY = ConfigKeys.newStringConfigKey("stringKey", "string key", null);
+ public static final ConfigKey<Object> OBJECT_KEY = ConfigKeys.newConfigKey(Object.class, "objectKey", "object key", null);
+ public static final ConfigKey<Closure> CLOSURE_KEY = ConfigKeys.newConfigKey(Closure.class, "closureKey", "closure key", null);
+ public static final ConfigKey<Future> FUTURE_KEY = ConfigKeys.newConfigKey(Future.class, "futureKey", "future key", null);
+ public static final ConfigKey<Task> TASK_KEY = ConfigKeys.newConfigKey(Task.class, "taskKey", "task key", null);
+ public static final ConfigKey<Predicate> PREDICATE_KEY = ConfigKeys.newConfigKey(Predicate.class, "predicateKey", "predicate key", null);
+ public static final IntegerAttributeSensorAndConfigKey SENSOR_AND_CONFIG_KEY = new IntegerAttributeSensorAndConfigKey("sensorConfigKey", "sensor+config key", 1);
+ }
+
+ public static class MyOtherEntityImpl extends AbstractEntity implements MyOtherEntity {
+ }
+
+ static class LatchingCallable<T> implements Callable<T> {
+ final CountDownLatch latchCalled = new CountDownLatch(1);
+ final CountDownLatch latchContinued = new CountDownLatch(1);
+ final AtomicInteger callCount = new AtomicInteger(0);
+ final T result;
+
+ public LatchingCallable(T result) {
+ this.result = result;
+ }
+
+ @Override
+ public T call() throws Exception {
+ callCount.incrementAndGet();
+ latchCalled.countDown();
+ latchContinued.await();
+ return result;
+ }
+ }
+
public static class MyEntity extends AbstractEntity {
public static final ConfigKey<String> MY_CONFIG = ConfigKeys.newStringConfigKey("myentity.myconfig");
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/87335648/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapGroovyTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapGroovyTest.groovy b/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapGroovyTest.groovy
deleted file mode 100644
index c12c197..0000000
--- a/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapGroovyTest.groovy
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.core.entity.internal;
-
-import static org.testng.Assert.assertEquals
-import static org.testng.Assert.assertFalse
-import static org.testng.Assert.assertTrue
-
-import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
-import org.apache.brooklyn.core.test.entity.TestApplication
-import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.core.entity.Entities
-import org.apache.brooklyn.core.entity.internal.ConfigMapTest.MyOtherEntity
-import org.apache.brooklyn.core.entity.internal.ConfigMapTest.MySubEntity
-import org.testng.annotations.AfterMethod
-import org.testng.annotations.BeforeMethod
-import org.testng.annotations.Test
-
-import com.google.common.base.Predicate;
-
-public class ConfigMapGroovyTest extends BrooklynAppUnitTestSupport {
-
- private MySubEntity entity;
-
- @BeforeMethod(alwaysRun=true)
- @Override
- public void setUp() {
- super.setUp();
- entity = app.addChild(EntitySpec.create(MySubEntity.class));
- }
-
- @Test
- public void testGetConfigOfTypeClosureReturnsClosure() throws Exception {
- MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
- entity2.config().set(MyOtherEntity.CLOSURE_KEY, { return "abc" } );
-
- Closure configVal = entity2.getConfig(MyOtherEntity.CLOSURE_KEY);
- assertTrue(configVal instanceof Closure, "configVal="+configVal);
- assertEquals(configVal.call(), "abc");
- }
-
- @Test
- public void testGetConfigOfPredicateTaskReturnsCoercedClosure() throws Exception {
- MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
- entity2.setConfig(MyOtherEntity.PREDICATE_KEY, { return it != null } );
-
- Predicate<?> predicate = entity2.getConfig(MyOtherEntity.PREDICATE_KEY);
- assertTrue(predicate instanceof Predicate, "predicate="+predicate);
- assertTrue(predicate.apply(1));
- assertFalse(predicate.apply(null));
- }
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/87335648/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapTest.java
deleted file mode 100644
index f5a84e3..0000000
--- a/core/src/test/java/org/apache/brooklyn/core/entity/internal/ConfigMapTest.java
+++ /dev/null
@@ -1,257 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.core.entity.internal;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-
-import java.util.concurrent.Callable;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import org.apache.brooklyn.api.entity.Entity;
-import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.api.entity.ImplementedBy;
-import org.apache.brooklyn.api.mgmt.ExecutionManager;
-import org.apache.brooklyn.api.mgmt.Task;
-import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.config.ConfigMap;
-import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.config.ConfigPredicates;
-import org.apache.brooklyn.core.entity.AbstractEntity;
-import org.apache.brooklyn.core.entity.EntityInternal;
-import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey.IntegerAttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
-import org.apache.brooklyn.util.core.task.BasicTask;
-import org.apache.brooklyn.util.core.task.DeferredSupplier;
-import org.testng.Assert;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.util.concurrent.MoreExecutors;
-
-import groovy.lang.Closure;
-
-public class ConfigMapTest extends BrooklynAppUnitTestSupport {
-
- private static final int TIMEOUT_MS = 10*1000;
-
- private MySubEntity entity;
- private ExecutorService executor;
- private ExecutionManager executionManager;
-
- @BeforeMethod(alwaysRun=true)
- @Override
- public void setUp() throws Exception {
- super.setUp();
- entity = app.addChild(EntitySpec.create(MySubEntity.class));
- executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
- executionManager = mgmt.getExecutionManager();
- }
-
- @AfterMethod(alwaysRun=true)
- @Override
- public void tearDown() throws Exception {
- if (executor != null) executor.shutdownNow();
- super.tearDown();
- }
-
- @Test
- public void testGetConfigKeysReturnsFromSuperAndInterfacesAndSubClass() throws Exception {
- assertEquals(entity.getEntityType().getConfigKeys(), ImmutableSet.of(
- MySubEntity.SUPER_KEY_1, MySubEntity.SUPER_KEY_2, MySubEntity.SUB_KEY_2, MySubEntity.INTERFACE_KEY_1));
- }
-
- @Test
- public void testConfigKeyDefaultUsesValueInSubClass() throws Exception {
- assertEquals(entity.getConfig(MyBaseEntity.SUPER_KEY_1), "overridden superKey1 default");
- }
-
- @Test
- public void testConfigureFromKey() throws Exception {
- MySubEntity entity2 = app.addChild(EntitySpec.create(MySubEntity.class)
- .configure(MySubEntity.SUPER_KEY_1, "changed"));
- assertEquals(entity2.getConfig(MySubEntity.SUPER_KEY_1), "changed");
- }
-
- @Test
- public void testConfigureFromSuperKey() throws Exception {
- MySubEntity entity2 = app.addChild(EntitySpec.create(MySubEntity.class)
- .configure(MyBaseEntity.SUPER_KEY_1, "changed"));
- assertEquals(entity2.getConfig(MySubEntity.SUPER_KEY_1), "changed");
- }
-
- @Test
- public void testConfigSubMap() throws Exception {
- entity.config().set(MyBaseEntity.SUPER_KEY_1, "s1");
- entity.config().set(MySubEntity.SUB_KEY_2, "s2");
- ConfigMap sub = entity.getConfigMap().submap(ConfigPredicates.nameMatchesGlob("sup*"));
- Assert.assertEquals(sub.getConfigRaw(MyBaseEntity.SUPER_KEY_1, true).get(), "s1");
- Assert.assertFalse(sub.getConfigRaw(MySubEntity.SUB_KEY_2, true).isPresent());
- }
-
- @Test(expectedExceptions=IllegalArgumentException.class)
- public void testFailFastOnInvalidConfigKeyCoercion() throws Exception {
- MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
- ConfigKey<Integer> key = MyOtherEntity.INT_KEY;
- entity2.config().set((ConfigKey)key, "thisisnotanint");
- }
-
- @Test
- public void testGetConfigWithDeferredSupplierReturnsSupplied() throws Exception {
- DeferredSupplier<Integer> supplier = new DeferredSupplier<Integer>() {
- volatile int next = 0;
- public Integer get() {
- return next++;
- }
- };
-
- MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
- entity2.config().set((ConfigKey)MyOtherEntity.INT_KEY, supplier);
-
- assertEquals(entity2.getConfig(MyOtherEntity.INT_KEY), Integer.valueOf(0));
- assertEquals(entity2.getConfig(MyOtherEntity.INT_KEY), Integer.valueOf(1));
- }
-
- @Test
- public void testGetConfigWithFutureWaitsForResult() throws Exception {
- LatchingCallable<String> work = new LatchingCallable<String>("abc");
- Future<String> future = executor.submit(work);
-
- final MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class));
- entity2.config().set((ConfigKey)MyOtherEntity.STRING_KEY, future);
-
- Future<String> getConfigFuture = executor.submit(new Callable<String>() {
- public String call() {
- return entity2.getConfig(MyOtherEntity.STRING_KEY);
- }});
-
- assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
- assertFalse(getConfigFuture.isDone());
-
- work.latchContinued.countDown();
- assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
- }
-
- @Test
- public void testGetConfigWithExecutedTaskWaitsForResult() throws Exception {
- LatchingCallable<String> work = new LatchingCallable<String>("abc");
- Task<String> task = executionManager.submit(work);
-
- final MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class)
- .configure(MyOtherEntity.STRING_KEY, task));
-
- Future<String> getConfigFuture = executor.submit(new Callable<String>() {
- public String call() {
- return entity2.getConfig(MyOtherEntity.STRING_KEY);
- }});
-
- assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
- assertFalse(getConfigFuture.isDone());
-
- work.latchContinued.countDown();
- assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
- assertEquals(work.callCount.get(), 1);
- }
-
- @Test
- public void testGetConfigWithUnexecutedTaskIsExecutedAndWaitsForResult() throws Exception {
- LatchingCallable<String> work = new LatchingCallable<String>("abc");
- Task<String> task = new BasicTask<String>(work);
-
- final MyOtherEntity entity2 = app.addChild(EntitySpec.create(MyOtherEntity.class)
- .configure(MyOtherEntity.STRING_KEY, task));
-
- Future<String> getConfigFuture = executor.submit(new Callable<String>() {
- public String call() {
- return entity2.getConfig(MyOtherEntity.STRING_KEY);
- }});
-
- assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
- assertFalse(getConfigFuture.isDone());
-
- work.latchContinued.countDown();
- assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc");
- assertEquals(work.callCount.get(), 1);
- }
-
- @ImplementedBy(MyBaseEntityImpl.class)
- public interface MyBaseEntity extends EntityInternal {
- public static final ConfigKey<String> SUPER_KEY_1 = ConfigKeys.newStringConfigKey("superKey1", "superKey1 key", "superKey1 default");
- public static final ConfigKey<String> SUPER_KEY_2 = ConfigKeys.newStringConfigKey("superKey2", "superKey2 key", "superKey2 default");
- }
-
- public static class MyBaseEntityImpl extends AbstractEntity implements MyBaseEntity {
- }
-
- @ImplementedBy(MySubEntityImpl.class)
- public interface MySubEntity extends MyBaseEntity, MyInterface {
- public static final ConfigKey<String> SUPER_KEY_1 = ConfigKeys.newConfigKeyWithDefault(MyBaseEntity.SUPER_KEY_1, "overridden superKey1 default");
- public static final ConfigKey<String> SUB_KEY_2 = ConfigKeys.newStringConfigKey("subKey2", "subKey2 key", "subKey2 default");
- }
-
- public static class MySubEntityImpl extends MyBaseEntityImpl implements MySubEntity {
- }
-
- public interface MyInterface {
- public static final ConfigKey<String> INTERFACE_KEY_1 = ConfigKeys.newStringConfigKey("interfaceKey1", "interface key 1", "interfaceKey1 default");
- }
-
- @ImplementedBy(MyOtherEntityImpl.class)
- public interface MyOtherEntity extends Entity {
- public static final ConfigKey<Integer> INT_KEY = ConfigKeys.newIntegerConfigKey("intKey", "int key", 1);
- public static final ConfigKey<String> STRING_KEY = ConfigKeys.newStringConfigKey("stringKey", "string key", null);
- public static final ConfigKey<Object> OBJECT_KEY = ConfigKeys.newConfigKey(Object.class, "objectKey", "object key", null);
- public static final ConfigKey<Closure> CLOSURE_KEY = ConfigKeys.newConfigKey(Closure.class, "closureKey", "closure key", null);
- public static final ConfigKey<Future> FUTURE_KEY = ConfigKeys.newConfigKey(Future.class, "futureKey", "future key", null);
- public static final ConfigKey<Task> TASK_KEY = ConfigKeys.newConfigKey(Task.class, "taskKey", "task key", null);
- public static final ConfigKey<Predicate> PREDICATE_KEY = ConfigKeys.newConfigKey(Predicate.class, "predicateKey", "predicate key", null);
- public static final IntegerAttributeSensorAndConfigKey SENSOR_AND_CONFIG_KEY = new IntegerAttributeSensorAndConfigKey("sensorConfigKey", "sensor+config key", 1);
- }
-
- public static class MyOtherEntityImpl extends AbstractEntity implements MyOtherEntity {
- }
-
- static class LatchingCallable<T> implements Callable<T> {
- final CountDownLatch latchCalled = new CountDownLatch(1);
- final CountDownLatch latchContinued = new CountDownLatch(1);
- final AtomicInteger callCount = new AtomicInteger(0);
- final T result;
-
- public LatchingCallable(T result) {
- this.result = result;
- }
-
- @Override
- public T call() throws Exception {
- callCount.incrementAndGet();
- latchCalled.countDown();
- latchContinued.await();
- return result;
- }
- }
-}