brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/6] brooklyn-server git commit: Tidy EntityAsserts
Date Tue, 29 Nov 2016 08:21:50 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master a20fdeac4 -> 721ced78d


Tidy EntityAsserts

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/bad32800
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/bad32800
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/bad32800

Branch: refs/heads/master
Commit: bad328004bdedf7771d350aa25839e9dc4c6dc2c
Parents: 72dccf0
Author: Aled Sage <aled.sage@gmail.com>
Authored: Thu Nov 17 11:11:40 2016 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Nov 25 23:28:19 2016 +0000

----------------------------------------------------------------------
 .../brooklyn/core/entity/EntityAsserts.java     | 40 +++++++++++++-------
 1 file changed, 27 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/bad32800/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
index 449d498..043332a 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
@@ -33,9 +33,10 @@ import org.apache.brooklyn.api.sensor.SensorEvent;
 import org.apache.brooklyn.api.sensor.SensorEventListener;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
-import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
 import java.util.Map;
@@ -51,6 +52,7 @@ import static org.apache.brooklyn.test.Asserts.*;
  */
 public class EntityAsserts {
 
+    private static final Logger LOG = LoggerFactory.getLogger(EntityAsserts.class);
 
     public static <T> void assertAttributeEquals(Entity entity, AttributeSensor<T>
attribute, T expected) {
         assertEquals(entity.getAttribute(attribute), expected, "entity=" + entity + "; attribute="
+ attribute);
@@ -66,7 +68,7 @@ public class EntityAsserts {
 
     public static <T> void assertAttributeEqualsEventually(Map<?,?> flags, final
Entity entity, final AttributeSensor<T> attribute, final T expected) {
         // Not using assertAttributeEventually(predicate) so get nicer error message
-        Asserts.succeedsEventually((Map) flags, new Runnable() {
+        Asserts.succeedsEventually(castToMapWithStringKeys(flags), new Runnable() {
             @Override
             public void run() {
                 assertAttributeEquals(entity, attribute, expected);
@@ -88,10 +90,9 @@ public class EntityAsserts {
 
     public static <T> T assertAttributeEventually(Map<?,?> flags, final Entity
entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate)
{
         final AtomicReference<T> result = new AtomicReference<T>();
-        Asserts.succeedsEventually((Map)flags, new Runnable() {
+        Asserts.succeedsEventually(castToMapWithStringKeys(flags), new Runnable() {
             @Override public void run() {
-                T val = entity.getAttribute(attribute);
-                Asserts.assertTrue(predicate.apply(val), "val=" + val);
+                T val = assertAttribute(entity, attribute, predicate);
                 result.set(val);
             }});
         return result.get();
@@ -99,24 +100,24 @@ public class EntityAsserts {
 
     public static <T> T assertAttribute(final Entity entity, final AttributeSensor<T>
attribute, final Predicate<? super T> predicate) {
         T val = entity.getAttribute(attribute);
-        Asserts.assertTrue(predicate.apply(val), "val=" + val);
+        Asserts.assertTrue(predicate.apply(val), "attribute="+attribute+"; val=" + val);
         return val;
     }
 
 
     public static <T extends Entity> void assertPredicateEventuallyTrue(final T entity,
final Predicate<? super T> predicate) {
-        assertPredicateEventuallyTrue(Maps.newLinkedHashMap(), entity, predicate);
+        assertPredicateEventuallyTrue(ImmutableMap.of(), entity, predicate);
     }
 
     public static <T extends Entity> void assertPredicateEventuallyTrue(Map<?,?>
flags, final T entity, final Predicate<? super T> predicate) {
-        Asserts.succeedsEventually((Map)flags, new Runnable() {
+        Asserts.succeedsEventually(castToMapWithStringKeys(flags), new Runnable() {
             @Override public void run() {
-                Asserts.assertTrue(predicate.apply(entity), "predicate unsatisfied");
+                Asserts.assertTrue(predicate.apply(entity), "predicate " + predicate + "
unsatisfied for "+ entity);
             }});
     }
 
     public static <T> void assertAttributeEqualsContinually(final Entity entity, final
AttributeSensor<T> attribute, final T expected) {
-        assertAttributeEqualsContinually(Maps.newLinkedHashMap(), entity, attribute, expected);
+        assertAttributeEqualsContinually(ImmutableMap.of(), entity, attribute, expected);
     }
 
     public static <T> void assertAttributeEqualsContinually(Map<?,?> flags, final
Entity entity, final AttributeSensor<T> attribute, final T expected) {
@@ -131,14 +132,13 @@ public class EntityAsserts {
     }
 
     public static void assertGroupSizeEqualsEventually(Map<?,?> flags, final Group
group, final int expected) {
-        Asserts.succeedsEventually((Map)flags, new Runnable() {
+        Asserts.succeedsEventually(castToMapWithStringKeys(flags), new Runnable() {
             @Override public void run() {
                 Collection<Entity> members = group.getMembers();
                 assertEquals(members.size(), expected, "members=" + members);
             }});
     }
 
-
     /**
      * Asserts that the entity's value for this attribute changes, by registering a subscription
and checking the value.
      *
@@ -253,4 +253,18 @@ public class EntityAsserts {
         assertAttributeEquals(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);
     }
 
-}
\ No newline at end of file
+    @SuppressWarnings("unchecked")
+    private static Map<String, ?> castToMapWithStringKeys(Map<?, ?> map) {
+        // TODO when checking that all keys are strings
+        if (map == null) return ImmutableMap.of();
+        for (Object key : map.keySet()) {
+            if (!(key instanceof String)) {
+                IllegalArgumentException e = new IllegalArgumentException("Invalid non-string
key(s), type " + key.getClass().getName()+" in map");
+                e.fillInStackTrace();
+                LOG.warn("Deprecated: invalid key(s) in map (continuing)", e);
+                break;
+            }
+        }
+        return (Map<String, ?>) map;
+    }
+}


Mime
View raw message