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:42 UTC

[1/7] git commit: Convert EntitySubscriptionTest from groovy to java

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 7cfeacc3e -> 0e7cc9c0f


Convert EntitySubscriptionTest 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/3c9a9e67
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/3c9a9e67
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/3c9a9e67

Branch: refs/heads/master
Commit: 3c9a9e67c35918da4ace74889e80490abb30dad5
Parents: b18b189
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 12:39:25 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:17 2014 +0000

----------------------------------------------------------------------
 .../entity/basic/EntitySubscriptionTest.groovy  | 248 ------------------
 .../entity/basic/EntitySubscriptionTest.java    | 251 +++++++++++++++++++
 2 files changed, 251 insertions(+), 248 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3c9a9e67/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.groovy b/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.groovy
deleted file mode 100644
index 1317c12..0000000
--- a/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.groovy
+++ /dev/null
@@ -1,248 +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.entity.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.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 EntitySubscriptionTest {
-
-    // TODO Duplication between this and PolicySubscriptionTest
-    
-    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 observedEntity;
-    private BasicGroup observedGroup;
-    private TestEntity observedChildEntity;
-    private TestEntity observedMemberEntity;
-    private TestEntity otherEntity;
-    private RecordingSensorEventListener listener;
-    
-    @BeforeMethod(alwaysRun=true)
-    public void setUp() {
-        loc = new SimulatedLocation();
-        app = TestApplication.Factory.newManagedInstanceForTests();
-        entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        observedEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        observedChildEntity = observedEntity.createAndManageChild(EntitySpec.create(TestEntity.class));
-
-        observedGroup = app.createAndManageChild(EntitySpec.create(BasicGroup.class));
-        observedMemberEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        observedGroup.addMember(observedMemberEntity);
-        
-        otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        
-        listener = new RecordingSensorEventListener();
-        
-        app.start([loc])
-    }
-    
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() {
-        if (app != null) Entities.destroyAll(app.getManagementContext());
-    }
-    
-    @Test
-    public void testSubscriptionReceivesEvents() {
-        entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
-        entity.subscribe(observedEntity, TestEntity.NAME, listener);
-        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
-        
-        otherEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedEntity.setAttribute(TestEntity.NAME, "myname");
-        observedEntity.emit(TestEntity.MY_NOTIF, 456);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedEntity, 123),
-                new BasicSensorEvent(TestEntity.NAME, observedEntity, "myname"),
-                new BasicSensorEvent(TestEntity.MY_NOTIF, observedEntity, 456)
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscriptionToAllReceivesEvents() {
-        entity.subscribe(null, TestEntity.SEQUENCE, listener);
-        
-        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedEntity, 123),
-                new BasicSensorEvent(TestEntity.SEQUENCE, otherEntity, 456),
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscribeToChildrenReceivesEvents() {
-        entity.subscribeToChildren(observedEntity, TestEntity.SEQUENCE, listener);
-        
-        observedChildEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedEntity.setAttribute(TestEntity.SEQUENCE, 456);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedChildEntity, 123)
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscribeToChildrenReceivesEventsForDynamicallyAddedChildren() {
-        entity.subscribeToChildren(observedEntity, TestEntity.SEQUENCE, listener);
-        
-        TestEntity observedChildEntity2 = observedEntity.createAndManageChild(EntitySpec.create(TestEntity.class));
-        observedChildEntity2.setAttribute(TestEntity.SEQUENCE, 123);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedChildEntity2, 123)
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscribeToMembersReceivesEvents() {
-        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
-        
-        observedMemberEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedGroup.setAttribute(TestEntity.SEQUENCE, 456);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedMemberEntity, 123)
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscribeToMembersReceivesEventsForDynamicallyAddedMembers() {
-        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
-        
-        TestEntity observedMemberEntity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        observedGroup.addMember(observedMemberEntity2);
-        observedMemberEntity2.setAttribute(TestEntity.SEQUENCE, 123);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedMemberEntity2, 123)
-            ])
-        }
-    }
-    
-    @Test(groups="Integration")
-    public void testSubscribeToMembersIgnoresEventsForDynamicallyRemovedMembers() {
-        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
-        
-        observedGroup.removeMember(observedMemberEntity);
-        
-        observedMemberEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        
-        assertSucceedsContinually {
-            assertEquals(listener.events, []);
-        }
-    }
-    
-    @Test
-    public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() {
-        entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
-        entity.subscribe(observedEntity, TestEntity.NAME, listener);
-        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
-        entity.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
-        entity.unsubscribe(observedEntity);
-        
-        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedEntity.setAttribute(TestEntity.NAME, "myname");
-        observedEntity.emit(TestEntity.MY_NOTIF, 123);
-        otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
-        
-        Thread.sleep(SHORT_WAIT_MS)
-        assertEquals(listener.events, [
-            new BasicSensorEvent(TestEntity.SEQUENCE, otherEntity, 456)
-        ]);
-    }
-    
-    @Test
-    public void testUnsubscribeUsingHandleStopsEvents() {
-        SubscriptionHandle handle1 = entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
-        SubscriptionHandle handle2 = entity.subscribe(observedEntity, TestEntity.NAME, listener);
-        SubscriptionHandle handle3 = entity.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
-        
-        entity.unsubscribe(observedEntity, handle2)
-        
-        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
-        observedEntity.setAttribute(TestEntity.NAME, "myname");
-        otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events, [
-                new BasicSensorEvent(TestEntity.SEQUENCE, observedEntity, 123),
-                new BasicSensorEvent(TestEntity.SEQUENCE, otherEntity, 456)
-            ])
-        }
-    }
-    
-    @Test
-    public void testSubscriptionReceivesEventsInOrder() {
-        final int NUM_EVENTS = 100
-        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
-
-        for (int i = 0; i < NUM_EVENTS; i++) {
-            observedEntity.emit(TestEntity.MY_NOTIF, i);
-        }
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(listener.events.size(), NUM_EVENTS)
-            for (int i = 0; i < NUM_EVENTS; i++) {
-                assertEquals(listener.events.get(i).getValue(), i)
-            }
-        }
-    }
-
-    public static class RecordingSensorEventListener implements SensorEventListener<Object> {
-        public 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/3c9a9e67/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java b/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
new file mode 100644
index 0000000..fd648d4
--- /dev/null
+++ b/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
@@ -0,0 +1,251 @@
+/*
+ * 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.entity.basic;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+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.TestApplication;
+import brooklyn.test.entity.TestEntity;
+
+import com.google.common.collect.ImmutableList;
+
+public class EntitySubscriptionTest {
+
+    // TODO Duplication between this and PolicySubscriptionTest
+    
+    private SimulatedLocation loc;
+    private TestApplication app;
+    private TestEntity entity;
+    private TestEntity observedEntity;
+    private BasicGroup observedGroup;
+    private TestEntity observedChildEntity;
+    private TestEntity observedMemberEntity;
+    private TestEntity otherEntity;
+    private RecordingSensorEventListener listener;
+    
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() {
+        app = TestApplication.Factory.newManagedInstanceForTests();
+        loc = app.newSimulatedLocation();
+        entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        observedEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        observedChildEntity = observedEntity.createAndManageChild(EntitySpec.create(TestEntity.class));
+
+        observedGroup = app.createAndManageChild(EntitySpec.create(BasicGroup.class));
+        observedMemberEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        observedGroup.addMember(observedMemberEntity);
+        
+        otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        
+        listener = new RecordingSensorEventListener();
+        
+        app.start(ImmutableList.of(loc));
+    }
+    
+    @AfterMethod(alwaysRun=true)
+    public void tearDown() {
+        if (app != null) Entities.destroyAll(app.getManagementContext());
+    }
+    
+    @Test
+    public void testSubscriptionReceivesEvents() {
+        entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
+        entity.subscribe(observedEntity, TestEntity.NAME, listener);
+        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
+        
+        otherEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        observedEntity.setAttribute(TestEntity.NAME, "myname");
+        observedEntity.emit(TestEntity.MY_NOTIF, 456);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123),
+                        new BasicSensorEvent<String>(TestEntity.NAME, observedEntity, "myname"),
+                        new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, observedEntity, 456)));
+            }});
+    }
+    
+    @Test
+    public void testSubscriptionToAllReceivesEvents() {
+        entity.subscribe(null, TestEntity.SEQUENCE, listener);
+        
+        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123),
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
+            }});
+    }
+    
+    @Test
+    public void testSubscribeToChildrenReceivesEvents() {
+        entity.subscribeToChildren(observedEntity, TestEntity.SEQUENCE, listener);
+        
+        observedChildEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        observedEntity.setAttribute(TestEntity.SEQUENCE, 456);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedChildEntity, 123)));
+            }});
+    }
+    
+    @Test
+    public void testSubscribeToChildrenReceivesEventsForDynamicallyAddedChildren() {
+        entity.subscribeToChildren(observedEntity, TestEntity.SEQUENCE, listener);
+        
+        final TestEntity observedChildEntity2 = observedEntity.createAndManageChild(EntitySpec.create(TestEntity.class));
+        observedChildEntity2.setAttribute(TestEntity.SEQUENCE, 123);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedChildEntity2, 123)));
+            }});
+    }
+    
+    @Test
+    public void testSubscribeToMembersReceivesEvents() {
+        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
+        
+        observedMemberEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        ((EntityLocal)observedGroup).setAttribute(TestEntity.SEQUENCE, 456);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedMemberEntity, 123)));
+            }});
+    }
+    
+    @Test
+    public void testSubscribeToMembersReceivesEventsForDynamicallyAddedMembers() {
+        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
+        
+        final TestEntity observedMemberEntity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        observedGroup.addMember(observedMemberEntity2);
+        observedMemberEntity2.setAttribute(TestEntity.SEQUENCE, 123);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedMemberEntity2, 123)));
+            }});
+    }
+    
+    @Test(groups="Integration")
+    public void testSubscribeToMembersIgnoresEventsForDynamicallyRemovedMembers() {
+        entity.subscribeToMembers(observedGroup, TestEntity.SEQUENCE, listener);
+        
+        observedGroup.removeMember(observedMemberEntity);
+        
+        observedMemberEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of());
+            }});
+    }
+    
+    @Test
+    public void testUnsubscribeRemovesAllSubscriptionsForThatEntity() {
+        entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
+        entity.subscribe(observedEntity, TestEntity.NAME, listener);
+        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
+        entity.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
+        entity.unsubscribe(observedEntity);
+        
+        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        observedEntity.setAttribute(TestEntity.NAME, "myname");
+        observedEntity.emit(TestEntity.MY_NOTIF, 123);
+        otherEntity.setAttribute(TestEntity.SEQUENCE, 456);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events, ImmutableList.of(
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
+            }});
+    }
+    
+    @Test
+    public void testUnsubscribeUsingHandleStopsEvents() {
+        SubscriptionHandle handle1 = entity.subscribe(observedEntity, TestEntity.SEQUENCE, listener);
+        SubscriptionHandle handle2 = entity.subscribe(observedEntity, TestEntity.NAME, listener);
+        SubscriptionHandle handle3 = entity.subscribe(otherEntity, TestEntity.SEQUENCE, listener);
+        
+        entity.unsubscribe(observedEntity, handle2);
+        
+        observedEntity.setAttribute(TestEntity.SEQUENCE, 123);
+        observedEntity.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, observedEntity, 123),
+                        new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
+            }});
+    }
+    
+    @Test
+    public void testSubscriptionReceivesEventsInOrder() {
+        final int NUM_EVENTS = 100;
+        entity.subscribe(observedEntity, TestEntity.MY_NOTIF, listener);
+
+        for (int i = 0; i < NUM_EVENTS; i++) {
+            observedEntity.emit(TestEntity.MY_NOTIF, i);
+        }
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(listener.events.size(), NUM_EVENTS);
+                for (int i = 0; i < NUM_EVENTS; i++) {
+                    assertEquals(listener.events.get(i).getValue(), i);
+                }
+            }});
+    }
+
+    public static class RecordingSensorEventListener implements SensorEventListener<Object> {
+        public final List<SensorEvent<?>> events = new CopyOnWriteArrayList<SensorEvent<?>>();
+        
+        @Override public void onEvent(SensorEvent<Object> event) {
+            events.add(event);
+        }
+    }
+}


[2/7] git commit: Convert KarafContainerTest from groovy to java

Posted by gr...@apache.org.
Convert KarafContainerTest 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/0826dfcc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/0826dfcc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/0826dfcc

Branch: refs/heads/master
Commit: 0826dfcceb9543c094fc0aa6def1ddedcebc5452
Parents: 374a087
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 13:55:53 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:18 2014 +0000

----------------------------------------------------------------------
 .../entity/osgi/karaf/KarafContainerTest.groovy | 157 -------------------
 .../entity/osgi/karaf/KarafContainerTest.java   | 143 +++++++++++++++++
 2 files changed, 143 insertions(+), 157 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0826dfcc/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.groovy
----------------------------------------------------------------------
diff --git a/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.groovy b/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.groovy
deleted file mode 100644
index f2c5585..0000000
--- a/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.groovy
+++ /dev/null
@@ -1,157 +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.entity.osgi.karaf
-
-import static brooklyn.test.TestUtils.*
-import static java.util.concurrent.TimeUnit.*
-import static org.testng.Assert.*
-
-import java.util.concurrent.TimeUnit
-
-import org.testng.Assert;
-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.entity.software.SshEffectorTasks;
-import brooklyn.entity.trait.Startable
-import brooklyn.location.MachineProvisioningLocation
-import brooklyn.location.basic.LocalhostMachineProvisioningLocation
-import brooklyn.test.entity.TestApplication
-import brooklyn.util.internal.TimeExtras
-import brooklyn.util.text.Identifiers
-
-public class KarafContainerTest {
-    static { TimeExtras.init() }
-
-    MachineProvisioningLocation localhost;
-    TestApplication app
-    KarafContainer karaf
-
-    @BeforeMethod(alwaysRun=true)
-    public void setup() {
-        app = ApplicationBuilder.newManagedApp(TestApplication.class);
-        localhost = app.newLocalhostProvisioningLocation();
-    }
-
-    @AfterMethod(alwaysRun=true)
-    public void shutdown() {
-        if (app != null) Entities.destroyAll(app.getManagementContext());
-        app = null;
-    }
-
-    // FIXME Test failing in jenkins; not sure why. The karaf log shows the mbeans never being
-    // registered so we are never able to connect to them over jmx.
-    @Test(groups = ["Integration", "WIP"])
-    public void canStartupAndShutdown() {
-        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
-                .configure("name", Identifiers.makeRandomId(8))
-                .configure("displayName", "Karaf Test"));
-        
-        app.start([ localhost ]);
-        executeUntilSucceeds(timeout:30 * SECONDS) {
-            assertNotNull karaf.getAttribute(Startable.SERVICE_UP)
-            assertTrue karaf.getAttribute(Startable.SERVICE_UP)
-        }
-        
-        Entities.dumpInfo(karaf);
-        int pid = karaf.getAttribute(KarafContainer.KARAF_PID);
-        Entities.submit(app, SshEffectorTasks.requirePidRunning(pid).machine(localhost.obtain())).get();
-        
-        karaf.stop();
-        executeUntilSucceeds(timeout:10 * SECONDS) {
-            assertFalse karaf.getAttribute(Startable.SERVICE_UP)
-        }
-        
-        Assert.assertFalse(Entities.submit(app, SshEffectorTasks.isPidRunning(pid).machine(localhost.obtain())).get());
-    }
-    
-    @Test(groups = ["Integration", "WIP"])
-    public void canStartupAndShutdownExplicitJmx() {
-        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
-                .configure("name", Identifiers.makeRandomId(8))
-                .configure("displayName", "Karaf Test")
-                .configure("rmiRegistryPort", "8099+")
-                .configure("jmxPort", "9099+"));
-        
-        app.start([ localhost ]);
-        executeUntilSucceeds(timeout:30 * SECONDS) {
-            assertNotNull karaf.getAttribute(Startable.SERVICE_UP)
-            assertTrue karaf.getAttribute(Startable.SERVICE_UP)
-        }
-        
-        karaf.stop();
-        executeUntilSucceeds(timeout:10 * SECONDS) {
-            assertFalse karaf.getAttribute(Startable.SERVICE_UP)
-        }
-    }
-    
-    @Test(groups = ["Integration", "WIP"])
-    public void canStartupAndShutdownLegacyJmx() {
-        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
-                .configure("name", Identifiers.makeRandomId(8))
-                .configure("displayName", "Karaf Test")
-                .configure("jmxPort", "8099+")
-                .configure("rmiRegistryPort", "9099+"));
-            // NB: now the above parameters have the opposite semantics to before
-        
-        app.start([ localhost ]);
-        executeUntilSucceeds(timeout:30 * SECONDS) {
-            assertNotNull karaf.getAttribute(Startable.SERVICE_UP)
-            assertTrue karaf.getAttribute(Startable.SERVICE_UP)
-        }
-        
-        karaf.stop();
-        executeUntilSucceeds(timeout:10 * SECONDS) {
-            assertFalse karaf.getAttribute(Startable.SERVICE_UP)
-        }
-    }
-    
-    // FIXME Test failing in jenkins; not sure why. The karaf log shows the mbeans never being
-    // registered so we are never able to connect to them over jmx.
-    @Test(groups = ["Integration", "WIP"])
-    public void testCanInstallAndUninstallBundle() {
-        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
-            .configure("name", Identifiers.makeRandomId(8))
-            .configure("displayName", "Karaf Test")
-            .configure("jmxPort", "8099+")
-            .configure("rmiRegistryPort", "9099+"));
-        
-        app.start([ localhost ]);
-        
-        URL jarUrl = getClass().getClassLoader().getResource("hello-world.jar");
-        assertNotNull(jarUrl);
-        
-        long bundleId = karaf.installBundle("wrap:"+jarUrl.toString());
-        
-        Map<Long, Map<String,?>> bundles = karaf.listBundles();
-        Map<String,?> bundle = bundles.get(bundleId);
-        assertNotNull(bundle, "expected="+bundleId+"; actual="+bundles.keySet());
-
-        // Undeploy: expect bundle to no longer be listed        
-        karaf.uninstallBundle(bundleId);
-        
-        Map<Long, Map<String,?>> bundles2 = karaf.listBundles();
-        Map<String,?> bundle2 = bundles2.get(bundleId);
-        assertNull(bundle2, "expectedAbsent="+bundleId+"; actual="+bundles2.keySet());
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0826dfcc/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.java
----------------------------------------------------------------------
diff --git a/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.java b/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.java
new file mode 100644
index 0000000..f43255d
--- /dev/null
+++ b/software/osgi/src/test/java/brooklyn/entity/osgi/karaf/KarafContainerTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.entity.osgi.karaf;
+
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import java.net.URL;
+import java.util.Map;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.BrooklynAppLiveTestSupport;
+import brooklyn.entity.basic.Attributes;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.software.SshEffectorTasks;
+import brooklyn.location.NoMachinesAvailableException;
+import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
+import brooklyn.test.Asserts;
+import brooklyn.test.EntityTestUtils;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.text.Identifiers;
+
+import com.google.common.collect.ImmutableList;
+
+public class KarafContainerTest extends BrooklynAppLiveTestSupport {
+
+    LocalhostMachineProvisioningLocation localhost;
+    KarafContainer karaf;
+
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        super.setUp();
+        localhost = app.newLocalhostProvisioningLocation();
+    }
+
+    // FIXME Test failing in jenkins; not sure why. The karaf log shows the mbeans never being
+    // registered so we are never able to connect to them over jmx.
+    @Test(groups = {"Integration", "WIP"})
+    public void canStartupAndShutdown() throws Exception {
+        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
+                .configure("name", Identifiers.makeRandomId(8))
+                .configure("displayName", "Karaf Test"));
+        
+        app.start(ImmutableList.of(localhost));
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, true);
+        
+        Entities.dumpInfo(karaf);
+        final int pid = karaf.getAttribute(KarafContainer.KARAF_PID);
+        Entities.submit(app, SshEffectorTasks.requirePidRunning(pid).machine(localhost.obtain())).get();
+        
+        karaf.stop();
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, false);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                try {
+                    Assert.assertFalse(Entities.submit(app, SshEffectorTasks.isPidRunning(pid).machine(localhost.obtain())).get());
+                } catch (NoMachinesAvailableException e) {
+                    throw Exceptions.propagate(e);
+                }
+            }});
+    }
+    
+    @Test(groups = {"Integration", "WIP"})
+    public void canStartupAndShutdownExplicitJmx() throws Exception {
+        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
+                .configure("name", Identifiers.makeRandomId(8))
+                .configure("displayName", "Karaf Test")
+                .configure("rmiRegistryPort", "8099+")
+                .configure("jmxPort", "9099+"));
+        
+        app.start(ImmutableList.of(localhost));
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, true);
+        
+        karaf.stop();
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, false);
+    }
+    
+    @Test(groups = {"Integration", "WIP"})
+    public void canStartupAndShutdownLegacyJmx() throws Exception {
+        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
+                .configure("name", Identifiers.makeRandomId(8))
+                .configure("displayName", "Karaf Test")
+                .configure("jmxPort", "8099+")
+                .configure("rmiRegistryPort", "9099+"));
+            // NB: now the above parameters have the opposite semantics to before
+        
+        app.start(ImmutableList.of(localhost));
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, true);
+        
+        karaf.stop();
+        EntityTestUtils.assertAttributeEqualsEventually(karaf, Attributes.SERVICE_UP, false);
+    }
+    
+    // FIXME Test failing in jenkins; not sure why. The karaf log shows the mbeans never being
+    // registered so we are never able to connect to them over jmx.
+    @Test(groups = {"Integration", "WIP"})
+    public void testCanInstallAndUninstallBundle() throws Exception {
+        karaf = app.createAndManageChild(EntitySpec.create(KarafContainer.class)
+            .configure("name", Identifiers.makeRandomId(8))
+            .configure("displayName", "Karaf Test")
+            .configure("jmxPort", "8099+")
+            .configure("rmiRegistryPort", "9099+"));
+        
+        app.start(ImmutableList.of(localhost));
+        
+        URL jarUrl = getClass().getClassLoader().getResource("hello-world.jar");
+        assertNotNull(jarUrl);
+        
+        long bundleId = karaf.installBundle("wrap:"+jarUrl.toString());
+        
+        Map<Long, Map<String,?>> bundles = karaf.listBundles();
+        Map<String,?> bundle = bundles.get(bundleId);
+        assertNotNull(bundle, "expected="+bundleId+"; actual="+bundles.keySet());
+
+        // Undeploy: expect bundle to no longer be listed        
+        karaf.uninstallBundle(bundleId);
+        
+        Map<Long, Map<String,?>> bundles2 = karaf.listBundles();
+        Map<String,?> bundle2 = bundles2.get(bundleId);
+        assertNull(bundle2, "expectedAbsent="+bundleId+"; actual="+bundles2.keySet());
+    }
+}


[7/7] git commit: This closes #297

Posted by gr...@apache.org.
This closes #297

* github/pr/297:
  Convert SetFromFlagTest from groovy to java
  Convert PolicySubscriptionTest from groovy to java
  Convert TestPortSupplierLocation from groovy to java
  Convert KarafContainerTest from groovy to java
  Convert OwnedChildrenTest from groovy to Java
  Convert EntitySubscriptionTest 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/0e7cc9c0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/0e7cc9c0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/0e7cc9c0

Branch: refs/heads/master
Commit: 0e7cc9c0fad843cb7331b50b828c5c46f7ddc32c
Parents: 7cfeacc aa6421b
Author: Andrew Kennedy <gr...@apache.org>
Authored: Tue Nov 4 21:13:35 2014 +0000
Committer: Andrew Kennedy <gr...@apache.org>
Committed: Tue Nov 4 21:13:35 2014 +0000

----------------------------------------------------------------------
 .../java/brooklyn/entity/SetFromFlagTest.groovy | 200 ---------------
 .../java/brooklyn/entity/SetFromFlagTest.java   | 213 ++++++++++++++++
 .../entity/basic/EntitySubscriptionTest.groovy  | 248 ------------------
 .../entity/basic/EntitySubscriptionTest.java    | 251 +++++++++++++++++++
 .../entity/basic/OwnedChildrenTest.groovy       | 209 ---------------
 .../entity/basic/OwnedChildrenTest.java         | 211 ++++++++++++++++
 .../basic/TestPortSupplierLocation.groovy       |  94 -------
 .../basic/TestPortSupplierLocation.java         |  90 +++++++
 .../policy/basic/PolicySubscriptionTest.groovy  | 138 ----------
 .../policy/basic/PolicySubscriptionTest.java    | 134 ++++++++++
 .../entity/osgi/karaf/KarafContainerTest.groovy | 157 ------------
 .../entity/osgi/karaf/KarafContainerTest.java   | 143 +++++++++++
 12 files changed, 1042 insertions(+), 1046 deletions(-)
----------------------------------------------------------------------



[6/7] git commit: Convert SetFromFlagTest from groovy to java

Posted by gr...@apache.org.
Convert SetFromFlagTest 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/aa6421bd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/aa6421bd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/aa6421bd

Branch: refs/heads/master
Commit: aa6421bd24c3d47c13e1c4472004c9ab85fe5c62
Parents: c3b8b32
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 14:36:46 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:19 2014 +0000

----------------------------------------------------------------------
 .../java/brooklyn/entity/SetFromFlagTest.groovy | 200 -----------------
 .../java/brooklyn/entity/SetFromFlagTest.java   | 213 +++++++++++++++++++
 2 files changed, 213 insertions(+), 200 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/aa6421bd/core/src/test/java/brooklyn/entity/SetFromFlagTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/SetFromFlagTest.groovy b/core/src/test/java/brooklyn/entity/SetFromFlagTest.groovy
deleted file mode 100644
index 9ac7cdf..0000000
--- a/core/src/test/java/brooklyn/entity/SetFromFlagTest.groovy
+++ /dev/null
@@ -1,200 +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.entity;
-
-import static org.testng.Assert.*
-
-import java.util.Map
-
-import org.testng.annotations.Test
-
-import brooklyn.entity.basic.AbstractEntity
-import brooklyn.location.PortRange
-import brooklyn.location.basic.PortRanges;
-import brooklyn.util.flags.SetFromFlag
-
-public class SetFromFlagTest {
-
-    @Test
-    public void testSetFromFlagUsingFieldName() {
-        MyEntity entity = new MyEntity(str1:"myval")
-        assertEquals(entity.str1, "myval")
-    }
-    
-    @Test
-    public void testSetFromFlagUsingOverridenName() {
-        MyEntity entity = new MyEntity(altStr2:"myval")
-        assertEquals(entity.str2, "myval")
-    }
-    
-    @Test
-    public void testSetFromFlagWhenNoDefaultIsNull() {
-        MyEntity entity = new MyEntity()
-        assertEquals(entity.str1, null)
-    }
-    
-    @Test
-    public void testSetFromFlagUsesDefault() {
-        MyEntity entity = new MyEntity()
-        assertEquals(entity.str3, "default str3")
-    }
-    
-    @Test
-    public void testSetFromFlagOverridingDefault() {
-        MyEntity entity = new MyEntity(str3:"overridden str3")
-        assertEquals(entity.str3, "overridden str3")
-    }
-
-    @Test
-    public void testSetFromFlagCastsPrimitives() {
-        MyEntity entity = new MyEntity(double1:1f)
-        assertEquals(entity.double1, 1d)
-    }
-
-    @Test
-    public void testSetFromFlagCastsDefault() {
-        MyEntity entity = new MyEntity()
-        assertEquals(entity.byte1, (byte)1)
-        assertEquals(entity.short1, (short)2)
-        assertEquals(entity.int1, 3)
-        assertEquals(entity.long1, 4l)
-        assertEquals(entity.float1, 5f)
-        assertEquals(entity.double1, 6d)
-         assertEquals(entity.char1, 'a' as char)
-        assertEquals(entity.bool1, true)
-        
-        assertEquals(entity.byte2, Byte.valueOf((byte)1))
-        assertEquals(entity.short2, Short.valueOf((short)2))
-        assertEquals(entity.int2, Integer.valueOf(3))
-        assertEquals(entity.long2, Long.valueOf(4l))
-        assertEquals(entity.float2, Float.valueOf(5f))
-        assertEquals(entity.double2, Double.valueOf(6d))
-        assertEquals(entity.char2, Character.valueOf('a' as char))
-        assertEquals(entity.bool2, Boolean.TRUE)
-    }
-    
-    @Test
-    public void testSetFromFlagCoercesDefaultToPortRange() {
-        MyEntity entity = new MyEntity()
-        assertEquals(entity.portRange1, PortRanges.fromInteger(1234))
-    }
-    
-    @Test
-    public void testSetFromFlagCoercesStringValueToPortRange() {
-        MyEntity entity = new MyEntity(portRange1:"1-3")
-        assertEquals(entity.portRange1, new PortRanges.LinearPortRange(1, 3))
-    }
-    
-    @Test
-    public void testSetFromFlagCoercesStringValueToInt() {
-        MyEntity entity = new MyEntity(int1:"123")
-        assertEquals(entity.int1, 123)
-    }
-
-    @Test
-    public void testSetIconUrl() {
-        MyEntity entity = new MyEntity(iconUrl:"/img/myicon.gif")
-        assertEquals(entity.getIconUrl(), "/img/myicon.gif")
-    }
-
-    @Test(expectedExceptions=IllegalArgumentException.class)
-    public void testFailsFastOnInvalidCoercion() {
-        MyEntity entity = new MyEntity(int1:"thisisnotanint")
-    }
-    
-    // Fails because configure being called from inside constructor; so field is set after configure called
-    @Test(enabled=false) 
-    public void testSetFromFlagWithFieldThatIsExplicitySet() {
-        MyEntity entity = new MyEntity(str4:"myval")
-        assertEquals(entity.str4, "myval")
-        
-        MyEntity entity2 = new MyEntity()
-        assertEquals(entity2.str4, "explicit str4")
-    }
-    
-    private static class MyEntity extends AbstractEntity {
-
-        @SetFromFlag(defaultVal="1234")
-        PortRange portRange1;
-
-        @SetFromFlag
-        String str1;
-        
-        @SetFromFlag("altStr2")
-        String str2;
-        
-        @SetFromFlag(defaultVal="default str3")
-        String str3;
-
-        @SetFromFlag
-        String str4 = "explicit str4";
-        
-        @SetFromFlag(defaultVal="1")
-        byte byte1;
-
-        @SetFromFlag(defaultVal="2")
-        short short1;
-
-        @SetFromFlag(defaultVal="3")
-        int int1;
-
-        @SetFromFlag(defaultVal="4")
-        long long1;
-
-        @SetFromFlag(defaultVal="5")
-        float float1;
-
-        @SetFromFlag(defaultVal="6")
-        double double1;
-
-        @SetFromFlag(defaultVal="a")
-        char char1;
-
-        @SetFromFlag(defaultVal="true")
-        boolean bool1;
-
-        @SetFromFlag(defaultVal="1")
-        Byte byte2;
-
-        @SetFromFlag(defaultVal="2")
-        Short short2;
-
-        @SetFromFlag(defaultVal="3")
-        Integer int2;
-
-        @SetFromFlag(defaultVal="4")
-        Long long2;
-
-        @SetFromFlag(defaultVal="5")
-        Float float2;
-
-        @SetFromFlag(defaultVal="6")
-        Double double2;
-
-        @SetFromFlag(defaultVal="a")
-        Character char2;
-
-        @SetFromFlag(defaultVal="true")
-        Boolean bool2;
-
-        MyEntity(Map flags=[:], Entity parent=null) {
-            super(flags, parent)
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/aa6421bd/core/src/test/java/brooklyn/entity/SetFromFlagTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/SetFromFlagTest.java b/core/src/test/java/brooklyn/entity/SetFromFlagTest.java
new file mode 100644
index 0000000..b02aff5
--- /dev/null
+++ b/core/src/test/java/brooklyn/entity/SetFromFlagTest.java
@@ -0,0 +1,213 @@
+/*
+ * 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.entity;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.AbstractEntity;
+import brooklyn.location.PortRange;
+import brooklyn.location.basic.PortRanges;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.flags.SetFromFlag;
+
+public class SetFromFlagTest {
+
+    @Test
+    public void testSetFromFlagUsingFieldName() {
+        MyEntity entity = new MyEntity(MutableMap.of("str1", "myval"));
+        assertEquals(entity.str1, "myval");
+    }
+    
+    @Test
+    public void testSetFromFlagUsingOverridenName() {
+        MyEntity entity = new MyEntity(MutableMap.of("altStr2", "myval"));
+        assertEquals(entity.str2, "myval");
+    }
+    
+    @Test
+    public void testSetFromFlagWhenNoDefaultIsNull() {
+        MyEntity entity = new MyEntity();
+        assertEquals(entity.str1, null);
+    }
+    
+    @Test
+    public void testSetFromFlagUsesDefault() {
+        MyEntity entity = new MyEntity();
+        assertEquals(entity.str3, "default str3");
+    }
+    
+    @Test
+    public void testSetFromFlagOverridingDefault() {
+        MyEntity entity = new MyEntity(MutableMap.of("str3", "overridden str3"));
+        assertEquals(entity.str3, "overridden str3");
+    }
+
+    @Test
+    public void testSetFromFlagCastsPrimitives() {
+        MyEntity entity = new MyEntity(MutableMap.of("double1", 1f));
+        assertEquals(entity.double1, 1d);
+    }
+
+    @Test
+    public void testSetFromFlagCastsDefault() {
+        MyEntity entity = new MyEntity();
+        assertEquals(entity.byte1, (byte)1);
+        assertEquals(entity.short1, (short)2);
+        assertEquals(entity.int1, 3);
+        assertEquals(entity.long1, 4l);
+        assertEquals(entity.float1, 5f);
+        assertEquals(entity.double1, 6d);
+         assertEquals(entity.char1, 'a');
+        assertEquals(entity.bool1, true);
+        
+        assertEquals(entity.byte2, Byte.valueOf((byte)1));
+        assertEquals(entity.short2, Short.valueOf((short)2));
+        assertEquals(entity.int2, Integer.valueOf(3));
+        assertEquals(entity.long2, Long.valueOf(4l));
+        assertEquals(entity.float2, Float.valueOf(5f));
+        assertEquals(entity.double2, Double.valueOf(6d));
+        assertEquals(entity.char2, Character.valueOf('a'));
+        assertEquals(entity.bool2, Boolean.TRUE);
+    }
+    
+    @Test
+    public void testSetFromFlagCoercesDefaultToPortRange() {
+        MyEntity entity = new MyEntity();
+        assertEquals(entity.portRange1, PortRanges.fromInteger(1234));
+    }
+    
+    @Test
+    public void testSetFromFlagCoercesStringValueToPortRange() {
+        MyEntity entity = new MyEntity(MutableMap.of("portRange1", "1-3"));
+        assertEquals(entity.portRange1, new PortRanges.LinearPortRange(1, 3));
+    }
+    
+    @Test
+    public void testSetFromFlagCoercesStringValueToInt() {
+        MyEntity entity = new MyEntity(MutableMap.of("int1", "123"));
+        assertEquals(entity.int1, 123);
+    }
+
+    @Test
+    public void testSetIconUrl() {
+        MyEntity entity = new MyEntity(MutableMap.of("iconUrl", "/img/myicon.gif"));
+        assertEquals(entity.getIconUrl(), "/img/myicon.gif");
+    }
+
+    @Test(expectedExceptions=IllegalArgumentException.class)
+    public void testFailsFastOnInvalidCoercion() {;
+        new MyEntity(MutableMap.of("int1", "thisisnotanint"));
+    }
+    
+    // Fails because configure being called from inside constructor; so field is set after configure called
+    @Test(enabled=false) 
+    public void testSetFromFlagWithFieldThatIsExplicitySet() {
+        MyEntity entity = new MyEntity(MutableMap.of("str4", "myval"));
+        assertEquals(entity.str4, "myval");
+        
+        MyEntity entity2 = new MyEntity();
+        assertEquals(entity2.str4, "explicit str4");
+    }
+    
+    private static class MyEntity extends AbstractEntity {
+
+        @SetFromFlag(defaultVal="1234")
+        PortRange portRange1;
+
+        @SetFromFlag
+        String str1;
+        
+        @SetFromFlag("altStr2")
+        String str2;
+        
+        @SetFromFlag(defaultVal="default str3")
+        String str3;
+
+        @SetFromFlag
+        String str4 = "explicit str4";
+        
+        @SetFromFlag(defaultVal="1")
+        byte byte1;
+
+        @SetFromFlag(defaultVal="2")
+        short short1;
+
+        @SetFromFlag(defaultVal="3")
+        int int1;
+
+        @SetFromFlag(defaultVal="4")
+        long long1;
+
+        @SetFromFlag(defaultVal="5")
+        float float1;
+
+        @SetFromFlag(defaultVal="6")
+        double double1;
+
+        @SetFromFlag(defaultVal="a")
+        char char1;
+
+        @SetFromFlag(defaultVal="true")
+        boolean bool1;
+
+        @SetFromFlag(defaultVal="1")
+        Byte byte2;
+
+        @SetFromFlag(defaultVal="2")
+        Short short2;
+
+        @SetFromFlag(defaultVal="3")
+        Integer int2;
+
+        @SetFromFlag(defaultVal="4")
+        Long long2;
+
+        @SetFromFlag(defaultVal="5")
+        Float float2;
+
+        @SetFromFlag(defaultVal="6")
+        Double double2;
+
+        @SetFromFlag(defaultVal="a")
+        Character char2;
+
+        @SetFromFlag(defaultVal="true")
+        Boolean bool2;
+
+        MyEntity() {
+            super(MutableMap.of(), null);
+        }
+        
+        MyEntity(Map flags) {
+            super(flags, null);
+        }
+        
+        MyEntity(Entity parent) {
+            super(MutableMap.of(), parent);
+        }
+        
+        MyEntity(Map flags, Entity parent) {
+            super(flags, parent);
+        }
+    }
+}


[3/7] git commit: Convert OwnedChildrenTest from groovy to Java

Posted by gr...@apache.org.
Convert OwnedChildrenTest 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/374a087d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/374a087d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/374a087d

Branch: refs/heads/master
Commit: 374a087d9876dc13598de05677eb06fee45ff56d
Parents: 3c9a9e6
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 12:48:17 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:18 2014 +0000

----------------------------------------------------------------------
 .../entity/basic/OwnedChildrenTest.groovy       | 209 ------------------
 .../entity/basic/OwnedChildrenTest.java         | 211 +++++++++++++++++++
 2 files changed, 211 insertions(+), 209 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/374a087d/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.groovy b/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.groovy
deleted file mode 100644
index d22764e..0000000
--- a/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.groovy
+++ /dev/null
@@ -1,209 +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.entity.basic
-
-import static org.testng.Assert.*
-
-import org.testng.annotations.AfterMethod
-import org.testng.annotations.BeforeMethod
-import org.testng.annotations.Test
-
-import brooklyn.entity.Application
-import brooklyn.entity.Entity
-
-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(owner:app) {}
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getApplication(), app)
-    }
-    
-    @Test
-    public void testSetParentInConstructorMap() {
-        Entity e = new AbstractEntity(parent:app) {}
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getApplication(), app)
-    }
-    
-    @Test
-    public void testSetParentInConstructorArgument() {
-        Entity e = new AbstractEntity(app) {}
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getApplication(), app)
-    }
-    
-    @Test
-    public void testSetParentInSetterMethod() {
-        Entity e = new AbstractEntity() {}
-        e.setParent(app)
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getApplication(), app)
-    }
-
-    @Test
-    public void testAddChild() {
-        Entity e = new AbstractEntity() {}
-        app.addChild(e)
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getApplication(), app)
-    }
-    
-    @Test
-    public void testSetParentWhenMatchesParentSetInConstructor() {
-        Entity e = new AbstractEntity(parent:app) {}
-        e.setParent(app)
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(app.getChildren(), [e])
-    }
-    
-    @Test(expectedExceptions = [ UnsupportedOperationException.class ])
-    public void testSetParentWhenDiffersFromParentSetInConstructor() {
-        Entity e = new AbstractEntity(parent:app) {}
-        Entity e2 = new AbstractEntity() {}
-        e.setParent(e2)
-        fail();
-    }
-    
-    @Test
-    public void testParentCanHaveMultipleChildren() {
-        Entity e = new AbstractEntity(parent:app) {}
-        Entity e2 = new AbstractEntity(parent:app) {}
-        
-        assertEquals(e.getParent(), app)
-        assertEquals(e2.getParent(), app)
-        assertEquals(app.getChildren(), [e,e2])
-    }
-    
-    @Test
-    public void testHierarchyOfOwners() {
-        Entity e = new AbstractEntity(parent:app) {}
-        Entity e2 = new AbstractEntity(parent:e) {}
-        Entity e3 = new AbstractEntity(parent:e2) {}
-        
-        assertEquals(app.getParent(), null)
-        assertEquals(e.getParent(), app)
-        assertEquals(e2.getParent(), e)
-        assertEquals(e3.getParent(), e2)
-        
-        assertEquals(app.getChildren(), [e])
-        assertEquals(e.getChildren(), [e2])
-        assertEquals(e2.getChildren(), [e3])
-        assertEquals(e3.getChildren(), [])
-    }
-    
-    @Test(enabled = false) // FIXME fails currently
-    public void testRemoveChild() {
-        Entity e = new AbstractEntity(parent:app) {}
-        app.removeChild(e)
-        
-        assertEquals(app.getChildren(), [])
-        assertEquals(e.getParent(), null)
-    }
-    
-    @Test
-    public void testParentalLoopForbiddenViaAddChild() {
-        Entity e = new AbstractEntity() {}
-        Entity e2 = new AbstractEntity(parent:e) {}
-        try {
-            e2.addChild(e)
-            fail()
-        } catch (IllegalStateException ex) {
-            // success
-        }
-        
-        assertEquals(e.getChildren(), [e2])
-        assertEquals(e2.getChildren(), [])
-        assertEquals(e.getParent(), null)
-        assertEquals(e2.getParent(), e)
-    }
-    
-    @Test
-    public void testParentalLoopForbiddenViaSetParent() {
-        Entity e = new AbstractEntity() {}
-        Entity e2 = new AbstractEntity(parent:e) {}
-        try {
-            e.setParent(e2)
-            fail()
-        } catch (IllegalStateException ex) {
-			ex.printStackTrace();
-            // success
-        }
-        assertEquals(e.getChildren(), [e2])
-        assertEquals(e2.getChildren(), [])
-        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(parent:app) {}
-        AbstractEntity e2 = new AbstractEntity(parent:e) {}
-        
-		use (Entities) {
-			assertTrue(e2.isAncestor(app))
-			assertTrue(e2.isAncestor(e))
-			assertFalse(e.isAncestor(e2))
-		}
-    }
-    
-    @Test
-    public void testIsDescendant() {
-        AbstractEntity e = new AbstractEntity(parent:app) {}
-        AbstractEntity e2 = new AbstractEntity(parent:e) {}
-
-		use (Entities) {
-			assertTrue(app.isDescendant(e))
-			assertTrue(app.isDescendant(e2))
-			assertFalse(e2.isDescendant(e))
-		}
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/374a087d/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.java b/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.java
new file mode 100644
index 0000000..f2f75e7
--- /dev/null
+++ b/core/src/test/java/brooklyn/entity/basic/OwnedChildrenTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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.entity.basic;
+
+import static 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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.Application;
+import brooklyn.entity.Entity;
+
+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));
+    }
+}


[5/7] git commit: Convert TestPortSupplierLocation from groovy to java

Posted by gr...@apache.org.
Convert TestPortSupplierLocation 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/cf762134
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/cf762134
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/cf762134

Branch: refs/heads/master
Commit: cf762134b177d6d3ae816bdd9e679ae7f7a7fc68
Parents: 0826dfc
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 4 14:00:04 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 4 14:37:19 2014 +0000

----------------------------------------------------------------------
 .../basic/TestPortSupplierLocation.groovy       | 94 --------------------
 .../basic/TestPortSupplierLocation.java         | 90 +++++++++++++++++++
 2 files changed, 90 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/cf762134/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.groovy
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.groovy b/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.groovy
deleted file mode 100644
index 51579d6..0000000
--- a/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.groovy
+++ /dev/null
@@ -1,94 +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.location.basic
-
-import org.testng.Assert
-import org.testng.annotations.AfterMethod
-import org.testng.annotations.BeforeMethod
-import org.testng.annotations.Test
-
-import brooklyn.entity.basic.Entities
-import brooklyn.entity.proxying.EntitySpec
-import brooklyn.event.basic.PortAttributeSensorAndConfigKey
-import brooklyn.event.feed.ConfigToAttributes
-import brooklyn.test.entity.TestApplication
-import brooklyn.test.entity.TestEntity
-
-public class TestPortSupplierLocation {
-
-    SimulatedLocation l;
-    PortAttributeSensorAndConfigKey ps;
-    TestApplication app;
-    TestEntity e;
-    
-    @BeforeMethod
-    public void setup() {
-        l = new SimulatedLocation();
-        app = TestApplication.Factory.newManagedInstanceForTests();
-        e = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        app.start([l]);
-        
-        ps = new PortAttributeSensorAndConfigKey("some.port", "for testing", "1234+");
-    }
-
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() throws Exception {
-        if (app != null) Entities.destroyAll(app.getManagementContext());
-    }
-
-    @Test
-    public void testObtainsPort() {
-        ConfigToAttributes.apply(e, ps);
-        
-        int p = e.getAttribute(ps);
-        Assert.assertEquals(p, 1234);
-        
-        //sensor access should keep the same value
-        p = e.getAttribute(ps);
-        Assert.assertEquals(p, 1234);
-    }
-    
-    @Test
-    public void testRepeatedConvertAccessIncrements() {
-        int p = ps.getAsSensorValue(e);
-        Assert.assertEquals(p, 1234);
-
-        //but direct access should see it as being reserved (not required behaviour, but it is the current behaviour)
-        int p2 = ps.getAsSensorValue(e);
-        Assert.assertEquals(p2, 1235);
-    }
-
-    @Test
-    public void testNullBeforeSetting() {
-        // currently getting the attribute before explicitly setting return null; i.e. no "auto-set" -- 
-        // but this behaviour may be changed
-        Integer p = e.getAttribute(ps);
-        Assert.assertEquals(p, null);
-    }
-
-    @Test
-    public void testSimulatedRestrictedPermitted() {
-        l.setPermittedPorts(PortRanges.fromString("1240+"));
-        
-        ConfigToAttributes.apply(e, ps);
-        int p = e.getAttribute(ps);
-        Assert.assertEquals((int)p, 1240);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/cf762134/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.java b/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.java
new file mode 100644
index 0000000..2d3c3bb
--- /dev/null
+++ b/core/src/test/java/brooklyn/location/basic/TestPortSupplierLocation.java
@@ -0,0 +1,90 @@
+/*
+ * 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.location.basic;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.BrooklynAppUnitTestSupport;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.event.basic.PortAttributeSensorAndConfigKey;
+import brooklyn.event.feed.ConfigToAttributes;
+import brooklyn.test.entity.TestEntity;
+
+import com.google.common.collect.ImmutableList;
+
+public class TestPortSupplierLocation extends BrooklynAppUnitTestSupport {
+
+    SimulatedLocation loc;
+    PortAttributeSensorAndConfigKey ps;
+    TestEntity entity;
+    
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        loc = app.newSimulatedLocation();
+        entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        app.start(ImmutableList.of(loc));
+        
+        ps = new PortAttributeSensorAndConfigKey("some.port", "for testing", "1234+");
+    }
+
+    @Test
+    public void testObtainsPort() throws Exception {
+        ConfigToAttributes.apply(entity, ps);
+        
+        int p = entity.getAttribute(ps);
+        assertEquals(p, 1234);
+        
+        //sensor access should keep the same value
+        p = entity.getAttribute(ps);
+        assertEquals(p, 1234);
+    }
+    
+    @Test
+    public void testRepeatedConvertAccessIncrements() throws Exception {
+        int p = ps.getAsSensorValue(entity);
+        assertEquals(p, 1234);
+
+        //but direct access should see it as being reserved (not required behaviour, but it is the current behaviour)
+        int p2 = ps.getAsSensorValue(entity);
+        assertEquals(p2, 1235);
+    }
+
+    @Test
+    public void testNullBeforeSetting() throws Exception {
+        // currently getting the attribute before explicitly setting return null; i.e. no "auto-set" -- 
+        // but this behaviour may be changed
+        Integer p = entity.getAttribute(ps);
+        assertEquals(p, null);
+    }
+
+    @Test
+    public void testSimulatedRestrictedPermitted() throws Exception {
+        loc.setPermittedPorts(PortRanges.fromString("1240+"));
+        
+        ConfigToAttributes.apply(entity, ps);
+        int p = entity.getAttribute(ps);
+        assertEquals((int)p, 1240);
+    }
+
+}


[4/7] git commit: Convert PolicySubscriptionTest from groovy to java

Posted by gr...@apache.org.
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);
+        }
+    }
+}