You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ge...@apache.org on 2017/04/06 11:08:47 UTC

[4/5] brooklyn-server git commit: Add greaterThan and lessThan test framework assertions

Add greaterThan and lessThan test framework assertions

Relies on the two objects being compared being instances of
Comparable<T>.


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

Branch: refs/heads/master
Commit: c51c69a2612adf3d10ae16f01b4e20d1f37e6297
Parents: 1578088
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Thu Apr 6 10:16:54 2017 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Apr 6 10:20:15 2017 +0100

----------------------------------------------------------------------
 .../test/framework/TestFrameworkAssertions.java | 27 +++++++++++++++++++-
 .../framework/TestFrameworkAssertionsTest.java  | 24 +++++++++++++++--
 2 files changed, 48 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c51c69a2/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
index c344557..68d51d8 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
@@ -66,6 +66,8 @@ public class TestFrameworkAssertions {
     public static final String IS_EMPTY = "isEmpty";
     public static final String NOT_EMPTY = "notEmpty";
     public static final String HAS_TRUTH_VALUE = "hasTruthValue";
+    public static final String GREATER_THAN = "greaterThan";
+    public static final String LESS_THAN = "lessThan";
     public static final String UNKNOWN_CONDITION = "unknown condition";
 
     public static class AssertionOptions {
@@ -334,6 +336,10 @@ public class TestFrameworkAssertions {
             return null != actual && actual.toString().matches(expected.toString());
         case HAS_TRUTH_VALUE:
             return isTrue(expected) == isTrue(actual);
+        case GREATER_THAN:
+            return canCompare(actual, expected) && compare(actual, expected) > 0;
+        case LESS_THAN:
+            return canCompare(actual, expected) && compare(actual, expected) < 0;
         default:
             return false;
         }
@@ -343,10 +349,29 @@ public class TestFrameworkAssertions {
         // Everything but UNKNOWN_CONDITION. The conditions should really be an enum!
         Set<String> allConditions = ImmutableSet.of(
                 IS_NULL, NOT_NULL, IS_EQUAL_TO, EQUAL_TO, EQUALS, NOT_EQUAL,
-                MATCHES, CONTAINS, IS_EMPTY, NOT_EMPTY, HAS_TRUTH_VALUE);
+                MATCHES, CONTAINS, IS_EMPTY, NOT_EMPTY, HAS_TRUTH_VALUE,
+                GREATER_THAN, LESS_THAN);
         return allConditions.contains(condition);
     }
 
+    /** @return True if actual and expected are both non-null instances of {@code Comparable<T>}. */
+    private static boolean canCompare(@Nullable Object actual, @Nullable Object expected) {
+        return actual != null
+                && expected != null
+                && actual instanceof Comparable
+                && actual.getClass().equals(expected.getClass());
+    }
+
+    @SuppressWarnings("unchecked")
+    private static int compare(@Nullable Object actual, @Nullable Object expected) {
+        if (!canCompare(actual, expected)) {
+            throw new IllegalArgumentException("Arguments are not comparable: " + actual + ", " + expected);
+        }
+        Comparable a = (Comparable) actual;
+        Comparable e = (Comparable) expected;
+        return a.compareTo(e);
+    }
+
     static void failAssertion(String target, String assertion, Object expected, Object actual) {
         throw new AssertionError(Joiner.on(' ').join(
             Objects.toString(target),

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c51c69a2/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java
index 3999952..360f072 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestFrameworkAssertionsTest.java
@@ -21,6 +21,7 @@ package org.apache.brooklyn.test.framework;
 import static org.testng.Assert.assertTrue;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -28,6 +29,7 @@ import java.util.Objects;
 
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.test.framework.TestFrameworkAssertions.AssertionOptions;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.apache.brooklyn.util.time.Duration;
 import org.slf4j.Logger;
@@ -71,6 +73,12 @@ public class TestFrameworkAssertionsTest {
                 {"some-non-null-value", Arrays.asList(ImmutableMap.of("notEmpty", Boolean.TRUE))},
                 {"true", Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.TRUE))},
                 {"false", Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.FALSE))},
+
+                {25, Collections.singletonList(ImmutableMap.of("greaterThan", 24))},
+                {"b", Collections.singletonList(ImmutableMap.of("greaterThan", "a"))},
+                {24, Collections.singletonList(ImmutableMap.of("lessThan", 25))},
+                {"a", Collections.singletonList(ImmutableMap.of("lessThan", "b"))},
+
                 {"some-non-null-value", Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.FALSE))},
         };
     }
@@ -137,6 +145,18 @@ public class TestFrameworkAssertionsTest {
                 {"<html><body><h1>Im a H1 tag!</h1></body></html>", "contains", "quack", Arrays.asList(ImmutableMap.of("contains", "quack"))},
                 {"{\"a\":\"b\",\"c\":\"d\",\"e\":123,\"g\":false}", "contains", "moo", Arrays.asList(ImmutableMap.of("contains", "moo"))},
 
+                {25, "lessThan", 24, Collections.singletonList(ImmutableMap.of("lessThan", 24))},
+                {"b", "lessThan", "a", Collections.singletonList(ImmutableMap.of("lessThan", "a"))},
+                {null, "lessThan", "a", Collections.singletonList(ImmutableMap.of("lessThan", "a"))},
+                {"a", "lessThan", null, Collections.singletonList(MutableMap.of("lessThan", null))},
+                {5, "lessThan", 5, Collections.singletonList(ImmutableMap.of("lessThan", 5))},
+
+                {24, "greaterThan", 25, Collections.singletonList(ImmutableMap.of("greaterThan", 25))},
+                {"a", "greaterThan", "b", Collections.singletonList(ImmutableMap.of("greaterThan", "b"))},
+                {null, "greaterThan", "a", Collections.singletonList(ImmutableMap.of("greaterThan", "a"))},
+                {"a", "greaterThan", null, Collections.singletonList(MutableMap.of("greaterThan", null))},
+                {5, "greaterThan", 5, Collections.singletonList(ImmutableMap.of("greaterThan", 5))},
+
                 {"true", "hasTruthValue", Boolean.FALSE, Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.FALSE))},
                 {"false", "hasTruthValue", Boolean.TRUE, Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.TRUE))},
                 {"some-not-null-value", "hasTruthValue", Boolean.TRUE, Arrays.asList(ImmutableMap.of("hasTruthValue", Boolean.TRUE))}
@@ -161,7 +181,7 @@ public class TestFrameworkAssertionsTest {
                     .timeout(timeout).assertions(assertions));
             Asserts.shouldHaveFailedPreviously();
         } catch (AssertionError e) {
-            Asserts.expectedFailureContains(e, Objects.toString(data), condition, expected.toString());
+            Asserts.expectedFailureContains(e, Objects.toString(data), condition, expected != null ? expected.toString() : "null");
         }
     }
 
@@ -187,7 +207,7 @@ public class TestFrameworkAssertionsTest {
                     .assertions(assertions));
             Asserts.shouldHaveFailedPreviously();
         } catch (AssertionError e) {
-            Asserts.expectedFailureContains(e, Objects.toString(data), condition, expected.toString());
+            Asserts.expectedFailureContains(e, Objects.toString(data), condition, expected != null ? expected.toString() : "null");
         }
     }