http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/OwnedChildrenTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/OwnedChildrenTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/OwnedChildrenTest.java deleted file mode 100644 index 9d8feb7..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/OwnedChildrenTest.java +++ /dev/null @@ -1,213 +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.entity.core; - -import static org.apache.brooklyn.test.Asserts.assertEqualsIgnoringOrder; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import org.apache.brooklyn.api.entity.Application; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.entity.core.AbstractApplication; -import org.apache.brooklyn.entity.core.AbstractEntity; -import org.apache.brooklyn.entity.core.Entities; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; - -public class OwnedChildrenTest { - - private Application app; - - @BeforeMethod(alwaysRun=true) - public void setUp() { - app = new AbstractApplication() {}; - } - - @AfterMethod(alwaysRun = true) - public void tearDown(){ - if (app != null) Entities.destroyAll(app.getManagementContext()); - } - - // Tests that the deprecated "owner" still works - @Test - public void testSetOwnerInConstructorMap() { - Entity e = new AbstractEntity(app) {}; - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEquals(e.getApplication(), app); - } - - @Test - public void testSetParentInConstructorMap() { - Entity e = new AbstractEntity(app) {}; - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEquals(e.getApplication(), app); - } - - @Test - public void testSetParentInConstructorArgument() { - Entity e = new AbstractEntity(app) {}; - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEquals(e.getApplication(), app); - } - - @Test - public void testSetParentInSetterMethod() { - Entity e = new AbstractEntity() {}; - e.setParent(app); - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEquals(e.getApplication(), app); - } - - @Test - public void testAddChild() { - Entity e = new AbstractEntity() {}; - app.addChild(e); - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEquals(e.getApplication(), app); - } - - @Test - public void testSetParentWhenMatchesParentSetInConstructor() { - Entity e = new AbstractEntity(app) {}; - e.setParent(app); - - assertEquals(e.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - } - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void testSetParentWhenDiffersFromParentSetInConstructor() { - Entity e = new AbstractEntity(app) {}; - Entity e2 = new AbstractEntity() {}; - e.setParent(e2); - fail(); - } - - @Test - public void testParentCanHaveMultipleChildren() { - Entity e = new AbstractEntity(app) {}; - Entity e2 = new AbstractEntity(app) {}; - - assertEquals(e.getParent(), app); - assertEquals(e2.getParent(), app); - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e,e2)); - } - - @Test - public void testHierarchyOfOwners() { - Entity e = new AbstractEntity(app) {}; - Entity e2 = new AbstractEntity(e) {}; - Entity e3 = new AbstractEntity(e2) {}; - - assertEquals(app.getParent(), null); - assertEquals(e.getParent(), app); - assertEquals(e2.getParent(), e); - assertEquals(e3.getParent(), e2); - - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of(e)); - assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2)); - assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of(e3)); - assertEqualsIgnoringOrder(e3.getChildren(), ImmutableList.of()); - } - - @Test(enabled = false) // FIXME fails currently - public void testRemoveChild() { - Entity e = new AbstractEntity(app) {}; - app.removeChild(e); - - assertEqualsIgnoringOrder(app.getChildren(), ImmutableList.of()); - assertEquals(e.getParent(), null); - } - - @Test - public void testParentalLoopForbiddenViaAddChild() { - Entity e = new AbstractEntity() {}; - Entity e2 = new AbstractEntity(e) {}; - try { - e2.addChild(e); - fail(); - } catch (IllegalStateException ex) { - // success - } - - assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2)); - assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of()); - assertEquals(e.getParent(), null); - assertEquals(e2.getParent(), e); - } - - @Test - public void testParentalLoopForbiddenViaSetParent() { - Entity e = new AbstractEntity() {}; - Entity e2 = new AbstractEntity(e) {}; - try { - e.setParent(e2); - fail(); - } catch (IllegalStateException ex) { - ex.printStackTrace(); - // success - } - assertEqualsIgnoringOrder(e.getChildren(), ImmutableList.of(e2)); - assertEqualsIgnoringOrder(e2.getChildren(), ImmutableList.of()); - assertEquals(e.getParent(), null); - assertEquals(e2.getParent(), e); - } - - @Test(expectedExceptions = IllegalStateException.class) - public void testParentingOneselfForbidden() { - AbstractEntity e = new AbstractEntity() {}; - e.addChild(e); - fail(); - } - - @Test - public void testIsAncestor() { - AbstractEntity e = new AbstractEntity(app) {}; - AbstractEntity e2 = new AbstractEntity(e) {}; - - assertTrue(Entities.isAncestor(e2, app)); - assertTrue(Entities.isAncestor(e2, e)); - assertFalse(Entities.isAncestor(e2, e2)); - } - - @Test - public void testIsDescendant() { - AbstractEntity e = new AbstractEntity(app) {}; - AbstractEntity e2 = new AbstractEntity(e) {}; - - assertTrue(Entities.isDescendant(app, e)); - assertTrue(Entities.isDescendant(app, e2)); - assertFalse(Entities.isDescendant(e2, e)); - } -}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/PolicyRegistrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/PolicyRegistrationTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/PolicyRegistrationTest.java deleted file mode 100644 index ee3a0fc..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/PolicyRegistrationTest.java +++ /dev/null @@ -1,152 +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.entity.core; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.fail; - -import java.util.Collection; -import java.util.List; - -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.policy.Policy; -import org.apache.brooklyn.api.policy.PolicySpec; -import org.apache.brooklyn.api.sensor.EnricherSpec; -import org.apache.brooklyn.api.sensor.SensorEvent; -import org.apache.brooklyn.api.sensor.SensorEventListener; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.core.test.entity.TestEntityNoEnrichersImpl; -import org.apache.brooklyn.entity.core.AbstractEntity; -import org.apache.brooklyn.entity.lifecycle.PolicyDescriptor; -import org.apache.brooklyn.policy.core.AbstractPolicy; -import org.apache.brooklyn.test.TestUtils; -import org.apache.brooklyn.util.collections.MutableMap; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - -public class PolicyRegistrationTest extends BrooklynAppUnitTestSupport { - - private static final int TIMEOUT_MS = 10*1000; - - private TestEntity entity; - private Policy policy1; - private Policy policy2; - - private List<PolicyDescriptor> added; - private List<PolicyDescriptor> removed; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - policy1 = new AbstractPolicy() {}; - policy2 = new AbstractPolicy() {}; - - added = Lists.newCopyOnWriteArrayList(); - removed = Lists.newCopyOnWriteArrayList(); - - app.subscribe(entity, AbstractEntity.POLICY_ADDED, new SensorEventListener<PolicyDescriptor>() { - @Override public void onEvent(SensorEvent<PolicyDescriptor> event) { - added.add(event.getValue()); - }}); - app.subscribe(entity, AbstractEntity.POLICY_REMOVED, new SensorEventListener<PolicyDescriptor>() { - @Override public void onEvent(SensorEvent<PolicyDescriptor> event) { - removed.add(event.getValue()); - }}); - } - - @Test - public void testGetPoliciesIsInitiallyEmpty() { - assertEquals(entity.getPolicies(), ImmutableList.of()); - } - - @Test(expectedExceptions = { UnsupportedOperationException.class }) - public void testGetPoliciesReturnsImmutableCollection() { - entity.getPolicies().add(policy1); - fail(); - } - - @Test - public void testAddAndRemovePolicies() { - entity.addPolicy(policy1); - assertEquals(entity.getPolicies(), ImmutableList.of(policy1)); - assertEqualsEventually(added, ImmutableList.of(new PolicyDescriptor(policy1))); - - entity.addPolicy(policy2); - assertEquals(entity.getPolicies(), ImmutableList.of(policy1, policy2)); - assertEqualsEventually(added, ImmutableList.of(new PolicyDescriptor(policy1), new PolicyDescriptor(policy2))); - - entity.removePolicy(policy1); - assertEquals(entity.getPolicies(), ImmutableList.of(policy2)); - assertEqualsEventually(removed, ImmutableList.of(new PolicyDescriptor(policy1))); - - entity.removePolicy(policy2); - assertEquals(entity.getPolicies(), ImmutableList.of()); - assertEqualsEventually(removed, ImmutableList.of(new PolicyDescriptor(policy1), new PolicyDescriptor(policy2))); - } - - @Test - public void testAddPolicySpec() { - EntitySpecTest.MyPolicy policy = entity.addPolicy(PolicySpec.create(EntitySpecTest.MyPolicy.class)); - assertNotNull(policy); - assertEquals(entity.getPolicies(), ImmutableList.of(policy)); - assertEqualsEventually(added, ImmutableList.of(new PolicyDescriptor(policy))); - } - - @Test - public void testAddEnricherSpec() { - TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class, TestEntityNoEnrichersImpl.class)); - EntitySpecTest.MyEnricher enricher = entity2.addEnricher(EnricherSpec.create(EntitySpecTest.MyEnricher.class)); - assertNotNull(enricher); - assertEquals(entity2.getEnrichers(), ImmutableList.of(enricher)); - } - - @Test - public void testRemoveAllPolicies() { - entity.addPolicy(policy1); - entity.addPolicy(policy2); - entity.removeAllPolicies(); - - assertEquals(entity.getPolicies(), ImmutableList.of()); - assertCollectionEqualsEventually(removed, ImmutableSet.of(new PolicyDescriptor(policy1), new PolicyDescriptor(policy2))); - } - - private <T> void assertEqualsEventually(final T actual, final T expected) { - TestUtils.assertEventually(MutableMap.of("timeout", TIMEOUT_MS), new Runnable() { - @Override public void run() { - assertEquals(actual, expected, "actual="+actual); - }}); - } - - // Ignores order of vals in collection, but asserts each same size and same elements - private <T> void assertCollectionEqualsEventually(final Collection<? extends T> actual, final Collection<? extends T> expected) { - TestUtils.assertEventually(MutableMap.of("timeout", TIMEOUT_MS), new Runnable() { - @Override public void run() { - assertEquals(ImmutableSet.copyOf(actual), ImmutableSet.copyOf(expected), "actual="+actual); - assertEquals(actual.size(), expected.size(), "actual="+actual); - }}); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/RecordingSensorEventListener.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/RecordingSensorEventListener.java b/core/src/test/java/org/apache/brooklyn/entity/core/RecordingSensorEventListener.java deleted file mode 100644 index f4b2d6f..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/RecordingSensorEventListener.java +++ /dev/null @@ -1,115 +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.entity.core; - -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; - -import org.apache.brooklyn.api.sensor.SensorEvent; -import org.apache.brooklyn.api.sensor.SensorEventListener; - -import com.google.common.base.Function; -import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.common.primitives.Longs; - -/** - * An event listener that records each event and allows callers to access all values and - * all values sorted by event timestamp. - */ -public class RecordingSensorEventListener<T> implements SensorEventListener<T>, Iterable<SensorEvent<T>> { - - private final List<SensorEvent<T>> events = Lists.newCopyOnWriteArrayList(); - private final boolean suppressDuplicates; - private T lastValue; - - public RecordingSensorEventListener() { - this(false); - } - - public RecordingSensorEventListener(boolean suppressDuplicates) { - this.suppressDuplicates = suppressDuplicates; - } - - @Override - public void onEvent(SensorEvent<T> event) { - if (!suppressDuplicates || events.isEmpty() || !Objects.equals(lastValue, event.getValue())) { - events.add(event); - lastValue = event.getValue(); - } - } - - /** - * @return An immutable iterable of the recorded events. - */ - public List<SensorEvent<T>> getEvents() { - return ImmutableList.copyOf(events); - } - - /** - * @return A live read-only view of recorded events. - */ - public Iterable<T> getEventValues() { - return FluentIterable.from(events) - .transform(new GetValueFunction<T>()); - } - - /** - * @return A static read-only view of event values sorted by the time at which they occurred. - */ - public Iterable<T> getEventValuesSortedByTimestamp() { - List<SensorEvent<T>> copy = Lists.newArrayList(events); - Collections.sort(copy, new EventTimestampComparator()); - return FluentIterable.from(copy) - .transform(new GetValueFunction<T>()); - } - - /** - * Clears all events recorded by the listener. - */ - public void clearEvents() { - this.events.clear(); - lastValue = null; - } - - @Override - public Iterator<SensorEvent<T>> iterator() { - return getEvents().iterator(); - } - - private static class GetValueFunction<T> implements Function<SensorEvent<T>, T> { - @Override - public T apply(SensorEvent<T> input) { - return input.getValue(); - } - } - - private static class EventTimestampComparator implements Comparator<SensorEvent<?>> { - @Override - public int compare(SensorEvent<?> o1, SensorEvent<?> o2) { - return Longs.compare(o1.getTimestamp(), o2.getTimestamp()); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/SanitizerTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/SanitizerTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/SanitizerTest.java deleted file mode 100644 index 66762df..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/SanitizerTest.java +++ /dev/null @@ -1,38 +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.entity.core; - -import static org.testng.Assert.assertEquals; - -import java.util.Map; - -import org.apache.brooklyn.core.config.Sanitizer; -import org.apache.brooklyn.util.core.config.ConfigBag; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableMap; - -public class SanitizerTest { - - @Test - public void testSanitize() throws Exception { - Map<String, Object> sanitized = Sanitizer.sanitize(ConfigBag.newInstance(ImmutableMap.of("password", "pa55w0rd", "mykey", "myval"))); - assertEquals(sanitized, ImmutableMap.of("password", "xxxxxxxx", "mykey", "myval")); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapGroovyTest.groovy ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapGroovyTest.groovy b/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapGroovyTest.groovy deleted file mode 100644 index 8e95b0d..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapGroovyTest.groovy +++ /dev/null @@ -1,61 +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.entity.core.internal; - -import static org.testng.Assert.assertEquals -import static org.testng.Assert.assertTrue - -import org.apache.brooklyn.core.test.entity.TestApplication -import org.apache.brooklyn.entity.core.Entities -import org.apache.brooklyn.entity.core.internal.ConfigMapTest.MyOtherEntity -import org.apache.brooklyn.entity.core.internal.ConfigMapTest.MySubEntity -import org.testng.annotations.AfterMethod -import org.testng.annotations.BeforeMethod -import org.testng.annotations.Test - -public class ConfigMapGroovyTest { - - private TestApplication app; - private MySubEntity entity; - - @BeforeMethod(alwaysRun=true) - public void setUp() { - app = TestApplication.Factory.newManagedInstanceForTests(); - entity = new MySubEntity(app); - Entities.manage(entity); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); - } - - @Test - public void testGetConfigOfTypeClosureReturnsClosure() throws Exception { - MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig(MyOtherEntity.CLOSURE_KEY, { return "abc" } ); - Entities.manage(entity2); - - Closure configVal = entity2.getConfig(MyOtherEntity.CLOSURE_KEY); - assertTrue(configVal instanceof Closure, "configVal="+configVal); - assertEquals(configVal.call(), "abc"); - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapTest.java deleted file mode 100644 index ef23a8b..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/internal/ConfigMapTest.java +++ /dev/null @@ -1,298 +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.entity.core.internal; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import groovy.lang.Closure; - -import java.util.Map; -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.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.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.entity.core.AbstractEntity; -import org.apache.brooklyn.entity.core.Entities; -import org.apache.brooklyn.sensor.core.BasicAttributeSensorAndConfigKey.IntegerAttributeSensorAndConfigKey; -import org.apache.brooklyn.util.collections.MutableMap; -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.base.Predicates; -import com.google.common.collect.ImmutableSet; -import com.google.common.util.concurrent.MoreExecutors; - -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 = new MySubEntity(app); - Entities.manage(entity); - 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 = new MySubEntity(MutableMap.of(MySubEntity.SUPER_KEY_1, "changed"), app); - Entities.manage(entity2); - assertEquals(entity2.getConfig(MySubEntity.SUPER_KEY_1), "changed"); - } - - @Test - public void testConfigureFromSuperKey() throws Exception { - MySubEntity entity2 = new MySubEntity(MutableMap.of(MyBaseEntity.SUPER_KEY_1, "changed"), app); - Entities.manage(entity2); - assertEquals(entity2.getConfig(MySubEntity.SUPER_KEY_1), "changed"); - } - - @Test - public void testConfigSubMap() throws Exception { - entity.setConfig(MyBaseEntity.SUPER_KEY_1, "s1"); - entity.setConfig(MySubEntity.SUB_KEY_2, "s2"); - ConfigMap sub = entity.getConfigMap().submap(ConfigPredicates.matchingGlob("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 = new MyOtherEntity(app); - ConfigKey<Integer> key = MyOtherEntity.INT_KEY; - entity2.setConfig((ConfigKey)key, "thisisnotanint"); - } - - @Test - public void testGetConfigOfPredicateTaskReturnsCoercedClosure() throws Exception { - MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig(MyOtherEntity.PREDICATE_KEY, Predicates.notNull()); - Entities.manage(entity2); - - Predicate predicate = entity2.getConfig(MyOtherEntity.PREDICATE_KEY); - assertTrue(predicate instanceof Predicate, "predicate="+predicate); - assertTrue(predicate.apply(1)); - assertFalse(predicate.apply(null)); - } - - @Test - public void testGetConfigWithDeferredSupplierReturnsSupplied() throws Exception { - DeferredSupplier<Integer> supplier = new DeferredSupplier<Integer>() { - volatile int next = 0; - public Integer get() { - return next++; - } - }; - - MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig(MyOtherEntity.INT_KEY, supplier); - Entities.manage(entity2); - - 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 work = new LatchingCallable("abc"); - Future<String> future = executor.submit(work); - - final MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig((ConfigKey)MyOtherEntity.STRING_KEY, future); - Entities.manage(entity2); - - 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 work = new LatchingCallable("abc"); - Task<String> task = executionManager.submit(work); - - final MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig(MyOtherEntity.STRING_KEY, task); - Entities.manage(entity2); - - 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 work = new LatchingCallable("abc"); - Task<String> task = new BasicTask<String>(work); - - final MyOtherEntity entity2 = new MyOtherEntity(app); - entity2.setConfig(MyOtherEntity.STRING_KEY, task); - Entities.manage(entity2); - - 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); - } - - public static class MyBaseEntity extends AbstractEntity { - 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 MyBaseEntity() { - } - public MyBaseEntity(Map flags) { - super(flags); - } - public MyBaseEntity(Map flags, Entity parent) { - super(flags, parent); - } - public MyBaseEntity(Entity parent) { - super(parent); - } - } - - public static class MySubEntity extends MyBaseEntity implements 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"); - - MySubEntity() { - } - MySubEntity(Map flags) { - super(flags); - } - MySubEntity(Map flags, Entity parent) { - super(flags, parent); - } - MySubEntity(Entity parent) { - super(parent); - } - } - - public interface MyInterface { - public static final ConfigKey<String> INTERFACE_KEY_1 = ConfigKeys.newStringConfigKey("interfaceKey1", "interface key 1", "interfaceKey1 default"); - } - - public static class MyOtherEntity extends AbstractEntity { - 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 MyOtherEntity() { - } - public MyOtherEntity(Map flags) { - super(flags); - } - public MyOtherEntity(Map flags, Entity parent) { - super(flags, parent); - } - public MyOtherEntity(Entity parent) { - super(parent); - } - } - - 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; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageLegacyTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageLegacyTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageLegacyTest.java deleted file mode 100644 index 642e8c5..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageLegacyTest.java +++ /dev/null @@ -1,292 +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.entity.core.internal; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; - -import org.apache.brooklyn.sensor.core.DependentConfiguration; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.time.Time; -import org.testng.annotations.Test; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.config.ConfigKeys; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.core.test.entity.TestEntityImpl; -import org.apache.brooklyn.entity.core.Entities; -import org.apache.brooklyn.location.core.SimulatedLocation; - -import com.google.common.base.Function; -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableList; -import com.google.common.util.concurrent.Callables; - -/** - * Test that configuration properties are usable and inherited correctly. - * - * Uses legacy mechanism of calling entity constructors. - */ -public class EntityConfigMapUsageLegacyTest extends BrooklynAppUnitTestSupport { - private ConfigKey<Integer> intKey = ConfigKeys.newIntegerConfigKey("bkey", "b key"); - private ConfigKey<String> strKey = ConfigKeys.newStringConfigKey("akey", "a key"); - private ConfigKey<Integer> intKeyWithDefault = ConfigKeys.newIntegerConfigKey("ckey", "c key", 1); - private ConfigKey<String> strKeyWithDefault = ConfigKeys.newStringConfigKey("strKey", "str key", "str key default"); - - @Test - public void testConfigPassedInAtConstructorIsAvailable() throws Exception { - TestEntity entity = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "aval", intKey, 2)), app); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(entity.getConfig(intKey), Integer.valueOf(2)); - } - - @Test - public void testConfigSetToGroovyTruthFalseIsAvailable() throws Exception { - TestEntity entity = new TestEntityImpl(MutableMap.of("config", MutableMap.of(intKeyWithDefault, 0)), app); - Entities.manage(entity); - - assertEquals(entity.getConfig(intKeyWithDefault), (Integer)0); - } - - @Test - public void testInheritedConfigSetToGroovyTruthFalseIsAvailable() throws Exception { - TestEntity parent = new TestEntityImpl(MutableMap.of("config", MutableMap.of(intKeyWithDefault, 0)), app); - TestEntity entity = new TestEntityImpl(parent); - Entities.manage(parent); - - assertEquals(entity.getConfig(intKeyWithDefault), (Integer)0); - } - - @Test - public void testConfigSetToNullIsAvailable() throws Exception { - TestEntity entity = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKeyWithDefault, null)), app); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKeyWithDefault), null); - } - - @Test - public void testInheritedConfigSetToNullIsAvailable() throws Exception { - TestEntity parent = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKeyWithDefault, null)), app); - TestEntity entity = new TestEntityImpl(parent); - Entities.manage(parent); - - assertEquals(entity.getConfig(strKeyWithDefault), null); - } - - @Test - public void testConfigCanBeSetOnEntity() throws Exception { - TestEntity entity = new TestEntityImpl(app); - entity.setConfig(strKey, "aval"); - entity.setConfig(intKey, 2); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(entity.getConfig(intKey), (Integer)2); - } - - @Test - public void testConfigInheritedFromParent() throws Exception { - TestEntity parent = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "aval")), app); - parent.setConfig(intKey, 2); - TestEntity entity = new TestEntityImpl(parent); - Entities.manage(parent); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(entity.getConfig(intKey), (Integer)2); - } - - @Test - public void testConfigInConstructorOverridesParentValue() throws Exception { - TestEntity parent = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "aval")), app); - TestEntity entity = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "diffval")), parent); - Entities.manage(parent); - - assertEquals("diffval", entity.getConfig(strKey)); - } - - @Test - public void testConfigSetterOverridesParentValue() throws Exception { - TestEntity parent = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "aval")), app); - TestEntity entity = new TestEntityImpl(parent); - entity.setConfig(strKey, "diffval"); - Entities.manage(parent); - - assertEquals("diffval", entity.getConfig(strKey)); - } - - @Test - public void testConfigSetterOverridesConstructorValue() throws Exception { - TestEntity entity = new TestEntityImpl(MutableMap.of("config", MutableMap.of(strKey, "aval")), app); - entity.setConfig(strKey, "diffval"); - Entities.manage(entity); - - assertEquals("diffval", entity.getConfig(strKey)); - } - - @Test - public void testConfigSetOnParentInheritedByExistingChildrenBeforeStarted() throws Exception { - TestEntity entity = new TestEntityImpl(app); - app.setConfig(strKey,"aval"); - Entities.manage(entity); - - assertEquals("aval", entity.getConfig(strKey)); - } - - @Test - public void testConfigInheritedThroughManyGenerations() throws Exception { - TestEntity e = new TestEntityImpl(app); - TestEntity e2 = new TestEntityImpl(e); - app.setConfig(strKey,"aval"); - Entities.manage(e); - - assertEquals("aval", app.getConfig(strKey)); - assertEquals("aval", e.getConfig(strKey)); - assertEquals("aval", e2.getConfig(strKey)); - } - - @Test(enabled=false) - public void testConfigCannotBeSetAfterApplicationIsStarted() throws Exception { - TestEntity entity = new TestEntityImpl(app); - Entities.manage(entity); - app.start(ImmutableList.of(new SimulatedLocation())); - - try { - app.setConfig(strKey,"aval"); - fail(); - } catch (IllegalStateException e) { - // success - } - - assertEquals(null, entity.getConfig(strKey)); - } - - @Test - public void testConfigReturnsDefaultValueIfNotSet() throws Exception { - TestEntity entity = new TestEntityImpl(app); - Entities.manage(entity); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "defaultval"); - } - - @Test - public void testGetFutureConfigWhenReady() throws Exception { - TestEntity entity = new TestEntityImpl(app); - entity.setConfig(TestEntity.CONF_NAME, DependentConfiguration.whenDone(Callables.returning("aval"))); - Entities.manage(entity); - app.start(ImmutableList.of(new SimulatedLocation())); - - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - } - - @Test - public void testGetFutureConfigBlocksUntilReady() throws Exception { - TestEntity entity = new TestEntityImpl(app); - final CountDownLatch latch = new CountDownLatch(1); - entity.setConfig(TestEntity.CONF_NAME, DependentConfiguration.whenDone(new Callable<String>() { - @Override public String call() throws Exception { - latch.await(); - return "aval"; - }})); - Entities.manage(entity); - app.start(ImmutableList.of(new SimulatedLocation())); - - Thread t = new Thread(new Runnable() { - public void run() { - Time.sleep(10); - latch.countDown(); - }}); - try { - long starttime = System.currentTimeMillis(); - t.start(); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - long endtime = System.currentTimeMillis(); - - assertTrue((endtime - starttime) >= 10, "starttime="+starttime+"; endtime="+endtime); - - } finally { - t.interrupt(); - } - } - - @Test - public void testGetAttributeWhenReadyConfigReturnsWhenSet() throws Exception { - TestEntity entity = new TestEntityImpl(app); - TestEntity entity2 = new TestEntityImpl(app); - entity.setConfig(TestEntity.CONF_NAME, DependentConfiguration.attributeWhenReady(entity2, TestEntity.NAME)); - Entities.manage(entity); - Entities.manage(entity2); - app.start(ImmutableList.of(new SimulatedLocation())); - - entity2.setAttribute(TestEntity.NAME, "aval"); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - } - - @Test - public void testGetAttributeWhenReadyWithPostProcessingConfigReturnsWhenSet() throws Exception { - TestEntity entity = new TestEntityImpl(app); - TestEntity entity2 = new TestEntityImpl(app); - entity.setConfig(TestEntity.CONF_NAME, DependentConfiguration.attributePostProcessedWhenReady(entity2, TestEntity.NAME, Predicates.notNull(), new Function<String,String>() { - @Override public String apply(String input) { - return (input == null) ? null : input+"mysuffix"; - }})); - Entities.manage(entity); - Entities.manage(entity2); - app.start(ImmutableList.of(new SimulatedLocation())); - - entity2.setAttribute(TestEntity.NAME, "aval"); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "avalmysuffix"); - } - - @Test - public void testGetAttributeWhenReadyConfigBlocksUntilSet() throws Exception { - TestEntity entity = new TestEntityImpl(app); - final TestEntity entity2 = new TestEntityImpl(app); - entity.setConfig(TestEntity.CONF_NAME, DependentConfiguration.attributeWhenReady(entity2, TestEntity.NAME)); - Entities.manage(entity); - Entities.manage(entity2); - app.start(ImmutableList.of(new SimulatedLocation())); - - // previously was just sleep 10, and (endtime-starttime > 10); failed with exactly 10ms - final long sleepTime = 20; - final long earlyReturnGrace = 5; - Thread t = new Thread(new Runnable() { - @Override public void run() { - Time.sleep(sleepTime); - entity2.setAttribute(TestEntity.NAME, "aval"); - }}); - try { - long starttime = System.currentTimeMillis(); - t.start(); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - long endtime = System.currentTimeMillis(); - - assertTrue((endtime - starttime) >= (sleepTime - earlyReturnGrace), "starttime=$starttime; endtime=$endtime"); - - } finally { - t.interrupt(); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageTest.java b/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageTest.java deleted file mode 100644 index bececeb..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/core/internal/EntityConfigMapUsageTest.java +++ /dev/null @@ -1,318 +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.entity.core.internal; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; - -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.config.BasicConfigKey; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.entity.core.Entities; -import org.apache.brooklyn.sensor.core.DependentConfiguration; -import org.apache.brooklyn.util.exceptions.Exceptions; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.apache.brooklyn.location.core.SimulatedLocation; - -import com.google.common.base.Function; -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableList; -import com.google.common.util.concurrent.Callables; - -/** - * Test that configuration properties are usable and inherited correctly. - */ -public class EntityConfigMapUsageTest extends BrooklynAppUnitTestSupport { - private static final int EARLY_RETURN_GRACE = 10; - - private BasicConfigKey<Integer> intKey = new BasicConfigKey<Integer>(Integer.class, "bkey", "b key"); - private ConfigKey<String> strKey = new BasicConfigKey<String>(String.class, "akey", "a key"); - private ConfigKey<Integer> intKeyWithDefault = new BasicConfigKey<Integer>(Integer.class, "ckey", "c key", 1); - private ConfigKey<String> strKeyWithDefault = new BasicConfigKey<String>(String.class, "strKey", "str key", "str key default"); - - private List<SimulatedLocation> locs; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - locs = ImmutableList.of(new SimulatedLocation()); - } - - @Test - public void testConfigPassedInAtConstructionIsAvailable() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval") - .configure(intKey, 2)); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(entity.getConfig(intKey), (Integer)2); - } - - @Test - public void testConfigSetToGroovyTruthFalseIsAvailable() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(intKeyWithDefault, 0)); - - assertEquals(entity.getConfig(intKeyWithDefault), (Integer)0); - } - - @Test - public void testInheritedConfigSetToGroovyTruthFalseIsAvailable() throws Exception { - TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(intKeyWithDefault, 0)); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class)); - - assertEquals(entity.getConfig(intKeyWithDefault), (Integer)0); - } - - @Test - public void testConfigSetToNullIsAvailable() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKeyWithDefault, (String)null)); - - assertEquals(entity.getConfig(strKeyWithDefault), null); - } - - @Test - public void testInheritedConfigSetToNullIsAvailable() throws Exception { - TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKeyWithDefault, (String)null)); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class)); - - assertEquals(entity.getConfig(strKeyWithDefault), null); - } - - @Test - public void testInheritedConfigAvailableDeepInHierarchy() throws Exception { - TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKeyWithDefault, "customval")); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity entity2 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity entity3 = entity2.createAndManageChild(EntitySpec.create(TestEntity.class)); - - assertEquals(entity.getConfig(strKeyWithDefault), "customval"); - assertEquals(entity2.getConfig(strKeyWithDefault), "customval"); - assertEquals(entity3.getConfig(strKeyWithDefault), "customval"); - } - - @Test - public void testConfigCanBeSetOnEntity() throws Exception { - TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class)); - ((EntityLocal)entity).setConfig(strKey, "aval"); - ((EntityLocal)entity).setConfig(intKey, 2); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(entity.getConfig(intKey), (Integer)2); - } - - @Test - public void testConfigInheritedFromParent() throws Exception { - TestEntity parent = app.addChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval")); - ((EntityLocal)parent).setConfig(intKey, 2); - Entities.manage(parent); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class)); - - assertEquals(entity.getConfig(strKey), "aval"); - assertEquals(2, entity.getConfig(intKey), (Integer)2); - } - - @Test - public void testConfigAtConstructionOverridesParentValue() throws Exception { - TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval")); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "diffval")); - - assertEquals(entity.getConfig(strKey), "diffval"); - } - - @Test - public void testConfigSetterOverridesParentValue() throws Exception { - TestEntity parent = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval")); - TestEntity entity = parent.createAndManageChild(EntitySpec.create(TestEntity.class)); - ((EntityLocal)entity).setConfig(strKey, "diffval"); - - assertEquals(entity.getConfig(strKey), "diffval"); - } - - @Test - public void testConfigSetterOverridesConstructorValue() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval")); - ((EntityLocal)entity).setConfig(strKey, "diffval"); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKey), "diffval"); - } - - @Test - public void testConfigSetOnParentInheritedByExistingChildrenBeforeStarted() throws Exception { - TestEntity parent = app.addChild(EntitySpec.create(TestEntity.class)); - TestEntity entity = parent.createChild(EntitySpec.create(TestEntity.class)); - ((EntityLocal)parent).setConfig(strKey,"aval"); - Entities.manage(entity); - - assertEquals(entity.getConfig(strKey), "aval"); - } - - @Test - public void testConfigInheritedThroughManyGenerations() throws Exception { - TestEntity e = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(strKey, "aval")); - TestEntity e2 = e.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity e3 = e2.createAndManageChild(EntitySpec.create(TestEntity.class)); - - assertEquals(e.getConfig(strKey), "aval"); - assertEquals(e2.getConfig(strKey), "aval"); - assertEquals(e3.getConfig(strKey), "aval"); - } - - // This has been relaxed to a warning, with a message saying "may not be supported in future versions" - @Test(enabled=false) - public void testConfigCannotBeSetAfterApplicationIsStarted() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - app.start(locs); - - try { - ((EntityLocal)app).setConfig(strKey,"aval"); - fail(); - } catch (IllegalStateException e) { - // success - } - - assertEquals(entity.getConfig(strKey), null); - } - - @Test - public void testConfigReturnsDefaultValueIfNotSet() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "defaultval"); - } - - @Test - public void testGetFutureConfigWhenReady() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(TestEntity.CONF_NAME, DependentConfiguration.whenDone(Callables.returning("aval")))); - app.start(locs); - - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - } - - @Test - public void testGetFutureConfigBlocksUntilReady() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(TestEntity.CONF_NAME, DependentConfiguration.whenDone(new Callable<String>() { - public String call() { - try { - latch.await(); return "aval"; - } catch (InterruptedException e) { - throw Exceptions.propagate(e); - } - }}))); - app.start(locs); - - Thread t = new Thread(new Runnable() { - public void run() { - try { - Thread.sleep(10+EARLY_RETURN_GRACE); latch.countDown(); - } catch (InterruptedException e) { - throw Exceptions.propagate(e); - } - }}); - try { - long starttime = System.currentTimeMillis(); - t.start(); - assertEquals(entity.getConfig(TestEntity.CONF_NAME), "aval"); - long endtime = System.currentTimeMillis(); - - assertTrue((endtime - starttime) >= 10, "starttime="+starttime+"; endtime="+endtime); - - } finally { - t.interrupt(); - } - } - - @Test - public void testGetAttributeWhenReadyConfigReturnsWhenSet() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(TestEntity.CONF_NAME, DependentConfiguration.attributeWhenReady(entity, TestEntity.NAME))); - app.start(locs); - - ((EntityLocal)entity).setAttribute(TestEntity.NAME, "aval"); - assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "aval"); - } - - @Test - public void testGetAttributeWhenReadyWithPostProcessingConfigReturnsWhenSet() throws Exception { - TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(TestEntity.CONF_NAME, DependentConfiguration.attributePostProcessedWhenReady(entity, TestEntity.NAME, Predicates.notNull(), new Function<String,String>() { - public String apply(String input) { - return input+"mysuffix"; - }}))); - app.start(locs); - - ((EntityLocal)entity).setAttribute(TestEntity.NAME, "aval"); - assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "avalmysuffix"); - } - - @Test - public void testGetAttributeWhenReadyConfigBlocksUntilSet() throws Exception { - final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .configure(TestEntity.CONF_NAME, DependentConfiguration.attributeWhenReady(entity, TestEntity.NAME))); - app.start(locs); - - Thread t = new Thread(new Runnable() { - public void run() { - try { - Thread.sleep(10+EARLY_RETURN_GRACE); - ((EntityLocal)entity).setAttribute(TestEntity.NAME, "aval"); - } catch (InterruptedException e) { - throw Exceptions.propagate(e); - } - }}); - try { - long starttime = System.currentTimeMillis(); - t.start(); - assertEquals(entity2.getConfig(TestEntity.CONF_NAME), "aval"); - long endtime = System.currentTimeMillis(); - - assertTrue((endtime - starttime) > 10, "starttime="+starttime+"; endtime="+endtime); - - } finally { - t.interrupt(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/drivers/BasicEntityDriverManagerTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/drivers/BasicEntityDriverManagerTest.java b/core/src/test/java/org/apache/brooklyn/entity/drivers/BasicEntityDriverManagerTest.java deleted file mode 100644 index b4928aa..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/drivers/BasicEntityDriverManagerTest.java +++ /dev/null @@ -1,74 +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.entity.drivers; - -import static org.testng.Assert.assertTrue; - -import org.apache.brooklyn.api.entity.drivers.DriverDependentEntity; -import org.apache.brooklyn.entity.drivers.BasicEntityDriverManager; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriver; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriverDependentEntity; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MySshDriver; -import org.apache.brooklyn.entity.drivers.RegistryEntityDriverFactoryTest.MyOtherSshDriver; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.apache.brooklyn.location.core.SimulatedLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.collections.MutableMap; - -public class BasicEntityDriverManagerTest { - - private BasicEntityDriverManager manager; - private SshMachineLocation sshLocation; - private SimulatedLocation simulatedLocation; - - @BeforeMethod - public void setUp() throws Exception { - manager = new BasicEntityDriverManager(); - sshLocation = new SshMachineLocation(MutableMap.of("address", "localhost")); - simulatedLocation = new SimulatedLocation(); - } - - @AfterMethod - public void tearDown(){ - // nothing to tear down; no management context created - } - - @Test - public void testPrefersRegisteredDriver() throws Exception { - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - manager.registerDriver(MyDriver.class, SshMachineLocation.class, MyOtherSshDriver.class); - assertTrue(manager.build(entity, sshLocation) instanceof MyOtherSshDriver); - } - - @Test - public void testFallsBackToReflectiveDriver() throws Exception { - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - assertTrue(manager.build(entity, sshLocation) instanceof MySshDriver); - } - - @Test - public void testRespectsLocationWhenDecidingOnDriver() throws Exception { - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - manager.registerDriver(MyDriver.class, SimulatedLocation.class, MyOtherSshDriver.class); - assertTrue(manager.build(entity, simulatedLocation) instanceof MyOtherSshDriver); - assertTrue(manager.build(entity, sshLocation) instanceof MySshDriver); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/drivers/EntityDriverRegistryTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/drivers/EntityDriverRegistryTest.java b/core/src/test/java/org/apache/brooklyn/entity/drivers/EntityDriverRegistryTest.java deleted file mode 100644 index 765e40b..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/drivers/EntityDriverRegistryTest.java +++ /dev/null @@ -1,59 +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.entity.drivers; - -import static org.testng.Assert.assertTrue; - -import org.apache.brooklyn.api.entity.drivers.DriverDependentEntity; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.entity.core.Entities; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriver; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriverDependentEntity; -import org.apache.brooklyn.entity.drivers.RegistryEntityDriverFactoryTest.MyOtherSshDriver; -import org.apache.brooklyn.util.collections.MutableMap; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.apache.brooklyn.location.ssh.SshMachineLocation; - -public class EntityDriverRegistryTest { - - private ManagementContext managementContext; - private SshMachineLocation sshLocation; - - @BeforeMethod - public void setUp() throws Exception { - managementContext = new LocalManagementContextForTests(); - sshLocation = new SshMachineLocation(MutableMap.of("address", "localhost")); - } - - @AfterMethod - public void tearDown(){ - if (managementContext != null) Entities.destroyAll(managementContext); - } - - @Test - public void testInstantiatesRegisteredDriver() throws Exception { - managementContext.getEntityDriverManager().registerDriver(MyDriver.class, SshMachineLocation.class, MyOtherSshDriver.class); - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - MyDriver driver = managementContext.getEntityDriverManager().build(entity, sshLocation); - assertTrue(driver instanceof MyOtherSshDriver); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/drivers/ReflectiveEntityDriverFactoryTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/drivers/ReflectiveEntityDriverFactoryTest.java b/core/src/test/java/org/apache/brooklyn/entity/drivers/ReflectiveEntityDriverFactoryTest.java deleted file mode 100644 index d3b1460..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/drivers/ReflectiveEntityDriverFactoryTest.java +++ /dev/null @@ -1,169 +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.entity.drivers; - -import org.apache.brooklyn.location.paas.PaasLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.drivers.DriverDependentEntity; -import org.apache.brooklyn.api.entity.drivers.EntityDriver; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.core.test.location.TestPaasLocation; -import org.apache.brooklyn.entity.core.AbstractEntity; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertTrue; - -public class ReflectiveEntityDriverFactoryTest { - - private ReflectiveEntityDriverFactory factory; - private SshMachineLocation sshLocation; - private PaasLocation paasLocation; - private DriverDependentEntity<MyDriver> entity; - - @BeforeMethod - public void setUp() throws Exception { - factory = new ReflectiveEntityDriverFactory(); - sshLocation = new SshMachineLocation(MutableMap.of("address", "localhost")); - entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - - paasLocation = new TestPaasLocation(); - } - - @AfterMethod - public void tearDown() { - // nothing to tear down; no management context created - } - - protected void assertDriverIs(Class<?> clazz, Location location) { - MyDriver driver = factory.build(entity, location); - assertTrue(driver.getClass().equals(clazz), "driver="+driver+"; should be "+clazz); - } - - @Test - public void testInstantiatesSshDriver() throws Exception { - assertDriverIs(MySshDriver.class, sshLocation); - } - - @Test - public void testInstantiatesPaasDriver() throws Exception { - assertDriverIs(MyTestPaasDriver.class, paasLocation); - } - - @Test - public void testFullNameMapping() throws Exception { - factory.addClassFullNameMapping(MyDriver.class.getName(), MyCustomDriver.class.getName()); - assertDriverIs(MyCustomDriver.class, sshLocation); - } - - @Test - public void testFullNameMappingMulti() throws Exception { - factory.addClassFullNameMapping(MyDriver.class.getName(), "X"); - factory.addClassFullNameMapping(MyDriver.class.getName(), MyCustomDriver.class.getName()); - assertDriverIs(MyCustomDriver.class, sshLocation); - } - - - @Test - public void testFullNameMappingFailure1() throws Exception { - factory.addClassFullNameMapping(MyDriver.class.getName()+"X", MyCustomDriver.class.getName()); - assertDriverIs(MySshDriver.class, sshLocation); - } - - @Test - public void testFullNameMappingFailure2() throws Exception { - factory.addClassFullNameMapping(MyDriver.class.getName(), MyCustomDriver.class.getName()); - factory.addClassFullNameMapping(MyDriver.class.getName(), "X"); - assertDriverIs(MySshDriver.class, sshLocation); - } - - @Test - public void testSimpleNameMapping() throws Exception { - factory.addClassSimpleNameMapping(MyDriver.class.getSimpleName(), MyCustomDriver.class.getSimpleName()); - assertDriverIs(MyCustomDriver.class, sshLocation); - } - - @Test - public void testSimpleNameMappingFailure() throws Exception { - factory.addClassSimpleNameMapping(MyDriver.class.getSimpleName()+"X", MyCustomDriver.class.getSimpleName()); - assertDriverIs(MySshDriver.class, sshLocation); - } - - public static class MyDriverDependentEntity<D extends EntityDriver> extends AbstractEntity implements DriverDependentEntity<D> { - private final Class<D> clazz; - - public MyDriverDependentEntity(Class<D> clazz) { - this.clazz = clazz; - } - - @Override - public Class<D> getDriverInterface() { - return clazz; - } - - @Override - public D getDriver() { - throw new UnsupportedOperationException(); - } - } - - public static interface MyDriver extends EntityDriver { - } - - public static class MySshDriver implements MyDriver { - public MySshDriver(Entity entity, SshMachineLocation machine) { - } - - @Override - public Location getLocation() { - throw new UnsupportedOperationException(); - } - - @Override - public EntityLocal getEntity() { - throw new UnsupportedOperationException(); - } - } - - public static class MyCustomDriver extends MySshDriver { - public MyCustomDriver(Entity entity, SshMachineLocation machine) { - super(entity, machine); - } - } - - public static class MyTestPaasDriver implements MyDriver { - public MyTestPaasDriver(Entity entity, PaasLocation location) { - } - - @Override - public Location getLocation() { - throw new UnsupportedOperationException(); - } - - @Override - public EntityLocal getEntity() { - throw new UnsupportedOperationException(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c27cf1d0/core/src/test/java/org/apache/brooklyn/entity/drivers/RegistryEntityDriverFactoryTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/entity/drivers/RegistryEntityDriverFactoryTest.java b/core/src/test/java/org/apache/brooklyn/entity/drivers/RegistryEntityDriverFactoryTest.java deleted file mode 100644 index 41c96e1..0000000 --- a/core/src/test/java/org/apache/brooklyn/entity/drivers/RegistryEntityDriverFactoryTest.java +++ /dev/null @@ -1,86 +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.entity.drivers; - -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.drivers.DriverDependentEntity; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.entity.drivers.RegistryEntityDriverFactory; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriver; -import org.apache.brooklyn.entity.drivers.ReflectiveEntityDriverFactoryTest.MyDriverDependentEntity; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.apache.brooklyn.location.core.SimulatedLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.collections.MutableMap; - -public class RegistryEntityDriverFactoryTest { - - private RegistryEntityDriverFactory factory; - private SshMachineLocation sshLocation; - private SimulatedLocation simulatedLocation; - - @BeforeMethod - public void setUp() throws Exception { - factory = new RegistryEntityDriverFactory(); - sshLocation = new SshMachineLocation(MutableMap.of("address", "localhost")); - simulatedLocation = new SimulatedLocation(); - } - - @AfterMethod - public void tearDown(){ - // nothing to tear down; no management context created - } - - @Test - public void testHasDriver() throws Exception { - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - factory.registerDriver(MyDriver.class, SshMachineLocation.class, MyOtherSshDriver.class); - assertTrue(factory.hasDriver(entity, sshLocation)); - assertFalse(factory.hasDriver(entity, simulatedLocation)); - } - - @Test - public void testInstantiatesRegisteredDriver() throws Exception { - DriverDependentEntity<MyDriver> entity = new MyDriverDependentEntity<MyDriver>(MyDriver.class); - factory.registerDriver(MyDriver.class, SshMachineLocation.class, MyOtherSshDriver.class); - MyDriver driver = factory.build(entity, sshLocation); - assertTrue(driver instanceof MyOtherSshDriver); - } - - public static class MyOtherSshDriver implements MyDriver { - public MyOtherSshDriver(Entity entity, Location machine) { - } - - @Override - public EntityLocal getEntity() { - throw new UnsupportedOperationException(); - } - - @Override - public Location getLocation() { - throw new UnsupportedOperationException(); - } - } -}
