test/java/util/Collection/testlibrary/CollectionAsserts.java

Print this page
rev 7682 : 8021591: Additional explicit null checks
Reviewed-by: duke

@@ -39,10 +39,14 @@
  * @library
  * CollectionAssert -- assertion methods for lambda test cases
  */
 public class CollectionAsserts {
 
+    private CollectionAsserts() {
+        // no instances
+    }
+
     public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {
         assertCountSum(it.iterator(), count, sum);
     }
 
     public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {

@@ -115,35 +119,46 @@
             uniq.add(each);
         }
     }
 
     public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {
-        assertContents(actual.iterator(), expected.iterator());
+        assertContents(actual, expected, null);
+    }
+
+    public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) {
+        assertContents(actual.iterator(), expected.iterator(), msg);
     }
 
     public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {
+        assertContents(actual, expected, null);
+    }
+
+    public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected, String msg) {
         List<T> history = new ArrayList<>();
 
         while (expected.hasNext()) {
             if (!actual.hasNext()) {
                 List<T> expectedData = new ArrayList<>(history);
                 while (expected.hasNext())
                     expectedData.add(expected.next());
-                fail(String.format("Premature end of data; expected=%s, found=%s", expectedData, history));
+                fail(String.format("%s Premature end of data; expected=%s, found=%s",
+                    (msg == null ? "" : msg), expectedData, history));
             }
             T a = actual.next();
             T e = expected.next();
             history.add(a);
 
             if (!Objects.equals(a, e))
-                fail(String.format("Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s", history, e, a));
+                fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s",
+                    (msg == null ? "" : msg), history, e, a));
         }
         if (actual.hasNext()) {
             List<T> rest = new ArrayList<>();
             while (actual.hasNext())
                 rest.add(actual.next());
-            fail(String.format("Unexpected data %s after %s", rest, history));
+            fail(String.format("%s Unexpected data %s after %s",
+                (msg == null ? "" : msg), rest, history));
         }
     }
 
     @SafeVarargs
     @SuppressWarnings("varargs")

@@ -164,19 +179,23 @@
 
         return Objects.equals(sa, sb);
     }
 
     public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
-        ArrayList<T> one = new ArrayList<>();
+        assertContentsUnordered(actual, expected, null);
+    }
+
+    public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) {
+        List<T> one = new ArrayList<>();
         for (T t : actual)
             one.add(t);
-        ArrayList<T> two = new ArrayList<>();
+        List<T> two = new ArrayList<>();
         for (T t : expected)
             two.add(t);
         Collections.sort(one);
         Collections.sort(two);
-        assertContents(one, two);
+        assertContents(one, two, msg);
     }
 
     static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) {
         Iterator<Iterable<T>> mI = splits.iterator();
         Iterator<T> pI = null;