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

[20/23] brooklyn-server git commit: more assertion routines, map equality and unordered iterable equality

more assertion routines, map equality and unordered iterable equality


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

Branch: refs/heads/master
Commit: 508183b1b85f7ee118d948739e4cc72121623a85
Parents: 8b72769
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Wed Oct 4 11:08:51 2017 +0100
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Wed Oct 4 14:45:39 2017 +0100

----------------------------------------------------------------------
 .../java/org/apache/brooklyn/test/Asserts.java  | 20 ++++++++++++++++++--
 .../org/apache/brooklyn/test/AssertsTest.java   | 17 +++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/508183b1/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
index dc672b5..3cd70b1 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
@@ -1452,8 +1452,24 @@ public class Asserts {
     }
 
     public static void assertSize(Iterable<?> list, int expectedSize) {
-        if (list==null) fail("List is null");
-        if (Iterables.size(list)!=expectedSize) fail("List has wrong size "+Iterables.size(list)+" (expected "+expectedSize+"): "+list);
+        if (list==null) fail("Collection is null");
+        if (Iterables.size(list)!=expectedSize) fail("Collection has wrong size "+Iterables.size(list)+" (expected "+expectedSize+"): "+list);
+    }
+
+    public static void assertSize(Map<?,?> map, int expectedSize) {
+        if (map==null) fail("Map is null");
+        if (Iterables.size(map.keySet())!=expectedSize) fail("Map has wrong size "+map.size()+" (expected "+expectedSize+"): "+map);
+    }
+
+    /** Ignores duplicates and order */
+    public static void assertSameUnorderedContents(Iterable<?> i1, Iterable<?> i2) {
+        if (i1==null || i2==null) {
+            if (i1==null && i2==null) {
+                return ;
+            }
+            fail("Collections differ in that one is null: "+i1+" and "+i2);
+        }
+        assertEquals(MutableSet.copyOf(i1), MutableSet.copyOf(i2));
     }
 
     public static void assertInstanceOf(Object obj, Class<?> type) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/508183b1/utils/common/src/test/java/org/apache/brooklyn/test/AssertsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/test/AssertsTest.java b/utils/common/src/test/java/org/apache/brooklyn/test/AssertsTest.java
index bd411b7..1105acd 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/test/AssertsTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/test/AssertsTest.java
@@ -25,7 +25,9 @@ import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.brooklyn.test.Asserts.ShouldHaveFailedPreviouslyAssertionError;
+import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.time.Duration;
 import org.testng.Assert;
@@ -170,4 +172,19 @@ public class AssertsTest {
         // check code flowed the way we expected
         Asserts.assertEquals(reached, 3);
     }
+    
+    @Test
+    public void testAssertSize() {
+        Asserts.assertSize(MutableList.of("x", "x", "y"), 3);
+        Asserts.assertSize(MutableSet.of("x", "x", "y"), 2);
+        Asserts.assertSize(MutableMap.of("x", "x", "y", "y"), 2);
+    }
+    
+    @Test
+    public void testAssertSetListEqualityAndSameUnoderderedContents() {
+        Assert.assertEquals(MutableSet.of("x", "x", "y"), MutableSet.of("x", "y", "x"));
+        Assert.assertNotEquals(MutableList.of("x", "x", "y"), MutableList.of("x", "y", "x"));
+        // above are baseline checks
+        Asserts.assertSameUnorderedContents(MutableList.of("x", "x", "y"), MutableList.of("y", "x"));
+    }
 }