Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8BA00200BDC for ; Tue, 29 Nov 2016 09:21:55 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 88E5D160B15; Tue, 29 Nov 2016 08:21:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B47DC160B27 for ; Tue, 29 Nov 2016 09:21:54 +0100 (CET) Received: (qmail 4253 invoked by uid 500); 29 Nov 2016 08:21:54 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 4116 invoked by uid 99); 29 Nov 2016 08:21:51 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Nov 2016 08:21:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 91A1FE38FC; Tue, 29 Nov 2016 08:21:50 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: svet@apache.org To: commits@brooklyn.apache.org Date: Tue, 29 Nov 2016 08:21:50 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/6] brooklyn-server git commit: Tidy EntityAsserts archived-at: Tue, 29 Nov 2016 08:21:55 -0000 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 Authored: Thu Nov 17 11:11:40 2016 +0000 Committer: Aled Sage 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 void assertAttributeEquals(Entity entity, AttributeSensor attribute, T expected) { assertEquals(entity.getAttribute(attribute), expected, "entity=" + entity + "; attribute=" + attribute); @@ -66,7 +68,7 @@ public class EntityAsserts { public static void assertAttributeEqualsEventually(Map flags, final Entity entity, final AttributeSensor 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 assertAttributeEventually(Map flags, final Entity entity, final AttributeSensor attribute, final Predicate predicate) { final AtomicReference result = new AtomicReference(); - 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 assertAttribute(final Entity entity, final AttributeSensor attribute, final Predicate 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 void assertPredicateEventuallyTrue(final T entity, final Predicate predicate) { - assertPredicateEventuallyTrue(Maps.newLinkedHashMap(), entity, predicate); + assertPredicateEventuallyTrue(ImmutableMap.of(), entity, predicate); } public static void assertPredicateEventuallyTrue(Map flags, final T entity, final Predicate 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 void assertAttributeEqualsContinually(final Entity entity, final AttributeSensor attribute, final T expected) { - assertAttributeEqualsContinually(Maps.newLinkedHashMap(), entity, attribute, expected); + assertAttributeEqualsContinually(ImmutableMap.of(), entity, attribute, expected); } public static void assertAttributeEqualsContinually(Map flags, final Entity entity, final AttributeSensor 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 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 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) map; + } +}