You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by gr...@apache.org on 2014/11/04 22:13:45 UTC
[4/7] git commit: Convert PolicySubscriptionTest from groovy to java
Convert PolicySubscriptionTest from groovy to java
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/c3b8b327
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/c3b8b327
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/c3b8b327
Branch: refs/heads/master
Commit: c3b8b32713e0bbdbb071055ae90149f9b97dbabc
Parents: cf76213
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 14:30:24 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:19 2014 +0000
----------------------------------------------------------------------
.../policy/basic/PolicySubscriptionTest.groovy | 138 -------------------
.../policy/basic/PolicySubscriptionTest.java | 134 ++++++++++++++++++
2 files changed, 134 insertions(+), 138 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c3b8b327/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.groovy b/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.groovy
deleted file mode 100644
index 2290c07..0000000
--- a/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.groovy
+++ /dev/null
@@ -1,138 +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 brooklyn.policy.basic
-
-import static brooklyn.test.TestUtils.*
-import static org.testng.Assert.*
-
-import java.util.concurrent.CopyOnWriteArrayList
-
-import org.testng.annotations.AfterMethod
-import org.testng.annotations.BeforeMethod
-import org.testng.annotations.Test
-
-import brooklyn.entity.basic.ApplicationBuilder
-import brooklyn.entity.basic.Entities
-import brooklyn.entity.proxying.EntitySpec
-import brooklyn.event.SensorEvent
-import brooklyn.event.SensorEventListener
-import brooklyn.event.basic.BasicSensorEvent
-import brooklyn.location.basic.SimulatedLocation
-import brooklyn.management.SubscriptionHandle
-import brooklyn.test.entity.TestApplication
-import brooklyn.test.entity.TestEntity
-
-public class PolicySubscriptionTest {
-
- // TODO Duplication between this and EntitySubscriptionTest
-
- private static final long TIMEOUT_MS = 5000;
- private static final long SHORT_WAIT_MS = 100;
-
- private SimulatedLocation loc;
- private TestApplication app;
- private TestEntity entity;
- private TestEntity entity2;
- private AbstractPolicy policy;
- private RecordingSensorEventListener listener;
-
- @BeforeMethod(alwaysRun=true)
- public void setUp() {
- loc = new SimulatedLocation();
- app = TestApplication.Factory.newManagedInstanceForTests();
- entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
- entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class));
- listener = new RecordingSensorEventListener();
- policy = new AbstractPolicy() {};
- entity.addPolicy(policy);
- app.start([loc])
- }
-
- @AfterMethod(alwaysRun=true)
- public void tearDown() throws Exception {
- if (app != null) Entities.destroyAll(app.getManagementContext());
- }
-
- @Test
- public void testSubscriptionReceivesEvents() {
- policy.subscribe(entity, TestEntity.SEQUENCE, listener);
- policy.subscribe(entity, TestEntity.NAME, listener);
- policy.subscribe(entity, TestEntity.MY_NOTIF, listener);
-
- entity2.setAttribute(TestEntity.SEQUENCE, 456);
- entity.setAttribute(TestEntity.SEQUENCE, 123);
- entity.setAttribute(TestEntity.NAME, "myname");
- entity.emit(TestEntity.MY_NOTIF, 789);
-
- executeUntilSucceeds(timeout:TIMEOUT_MS) {
- assertEquals(listener.events, [
- new BasicSensorEvent(TestEntity.SEQUENCE, entity, 123),
- new BasicSensorEvent(TestEntity.NAME, entity, "myname"),
- new BasicSensorEvent(TestEntity.MY_NOTIF, entity, 789)
- ])
- }
- }
-
- @Test
- public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() {
- policy.subscribe(entity, TestEntity.SEQUENCE, listener);
- policy.subscribe(entity, TestEntity.NAME, listener);
- policy.subscribe(entity, TestEntity.MY_NOTIF, listener);
- policy.subscribe(entity2, TestEntity.SEQUENCE, listener);
- policy.unsubscribe(entity);
-
- entity.setAttribute(TestEntity.SEQUENCE, 123);
- entity.setAttribute(TestEntity.NAME, "myname");
- entity.emit(TestEntity.MY_NOTIF, 456);
- entity2.setAttribute(TestEntity.SEQUENCE, 789);
-
- Thread.sleep(SHORT_WAIT_MS)
- assertEquals(listener.events, [
- new BasicSensorEvent(TestEntity.SEQUENCE, entity2, 789)
- ]);
- }
-
- @Test
- public void testUnsubscribeUsingHandleStopsEvents() {
- SubscriptionHandle handle1 = policy.subscribe(entity, TestEntity.SEQUENCE, listener);
- SubscriptionHandle handle2 = policy.subscribe(entity, TestEntity.NAME, listener);
- SubscriptionHandle handle3 = policy.subscribe(entity2, TestEntity.SEQUENCE, listener);
-
- policy.unsubscribe(entity, handle2)
-
- entity.setAttribute(TestEntity.SEQUENCE, 123);
- entity.setAttribute(TestEntity.NAME, "myname");
- entity2.setAttribute(TestEntity.SEQUENCE, 456);
-
- executeUntilSucceeds(timeout:TIMEOUT_MS) {
- assertEquals(listener.events, [
- new BasicSensorEvent(TestEntity.SEQUENCE, entity, 123),
- new BasicSensorEvent(TestEntity.SEQUENCE, entity2, 456)
- ])
- }
- }
-
- private static class RecordingSensorEventListener implements SensorEventListener<Object> {
- final List<SensorEvent<?>> events = new CopyOnWriteArrayList<SensorEvent<?>>();
-
- @Override public void onEvent(SensorEvent<Object> event) {
- events.add(event);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/c3b8b327/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java b/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
new file mode 100644
index 0000000..41a0736
--- /dev/null
+++ b/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
@@ -0,0 +1,134 @@
+/*
+ * 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 brooklyn.policy.basic;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.BrooklynAppUnitTestSupport;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.event.SensorEvent;
+import brooklyn.event.SensorEventListener;
+import brooklyn.event.basic.BasicSensorEvent;
+import brooklyn.location.basic.SimulatedLocation;
+import brooklyn.management.SubscriptionHandle;
+import brooklyn.test.Asserts;
+import brooklyn.test.entity.TestEntity;
+
+import com.google.common.collect.ImmutableList;
+
+public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
+
+ // TODO Duplication between this and EntitySubscriptionTest
+
+ private static final long SHORT_WAIT_MS = 100;
+
+ private SimulatedLocation loc;
+ private TestEntity entity;
+ private TestEntity otherEntity;
+ private AbstractPolicy policy;
+ private RecordingSensorEventListener listener;
+
+ @BeforeMethod(alwaysRun=true)
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ loc = app.newSimulatedLocation();
+ entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+ otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+ listener = new RecordingSensorEventListener();
+ policy = new AbstractPolicy() {};
+ entity.addPolicy(policy);
+ app.start(ImmutableList.of(loc));
+ }
+
+ @Test
+ public void testSubscriptionReceivesEvents() throws Exception {
+ policy.subscribe(entity, TestEntity.SEQUENCE, listener);
+ policy.subscribe(entity, TestEntity.NAME, listener);
+ policy.subscribe(entity, TestEntity.MY_NOTIF, listener);
+
+ otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
+ entity.setAttribute(TestEntity.SEQUENCE, 123);
+ entity.setAttribute(TestEntity.NAME, "myname");
+ entity.emit(TestEntity.MY_NOTIF, 789);
+
+ Asserts.succeedsEventually(new Runnable() {
+ @Override public void run() {
+ assertEquals(listener.events, ImmutableList.of(
+ new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
+ new BasicSensorEvent<String>(TestEntity.NAME, entity, "myname"),
+ new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, entity, 789)));
+ }});
+ }
+
+ @Test
+ public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() throws Exception {
+ policy.subscribe(entity, TestEntity.SEQUENCE, listener);
+ policy.subscribe(entity, TestEntity.NAME, listener);
+ policy.subscribe(entity, TestEntity.MY_NOTIF, listener);
+ policy.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
+ policy.unsubscribe(entity);
+
+ entity.setAttribute(TestEntity.SEQUENCE, 123);
+ entity.setAttribute(TestEntity.NAME, "myname");
+ entity.emit(TestEntity.MY_NOTIF, 456);
+ otherEntity.setAttribute(TestEntity.SEQUENCE, 789);
+
+ Thread.sleep(SHORT_WAIT_MS);
+ Asserts.succeedsEventually(new Runnable() {
+ @Override public void run() {
+ assertEquals(listener.events, ImmutableList.of(
+ new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 789)));
+ }});
+ }
+
+ @Test
+ public void testUnsubscribeUsingHandleStopsEvents() throws Exception {
+ SubscriptionHandle handle1 = policy.subscribe(entity, TestEntity.SEQUENCE, listener);
+ SubscriptionHandle handle2 = policy.subscribe(entity, TestEntity.NAME, listener);
+ SubscriptionHandle handle3 = policy.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
+
+ policy.unsubscribe(entity, handle2);
+
+ entity.setAttribute(TestEntity.SEQUENCE, 123);
+ entity.setAttribute(TestEntity.NAME, "myname");
+ otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
+
+ Asserts.succeedsEventually(new Runnable() {
+ @Override public void run() {
+ assertEquals(listener.events, ImmutableList.of(
+ new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
+ new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
+ }});
+ }
+
+ private static class RecordingSensorEventListener implements SensorEventListener<Object> {
+ final List<SensorEvent<?>> events = new CopyOnWriteArrayList<SensorEvent<?>>();
+
+ @Override public void onEvent(SensorEvent<Object> event) {
+ events.add(event);
+ }
+ }
+}