< prev index next >

test/java/util/Collections/EmptyNavigableSet.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -35,14 +35,16 @@
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.NavigableSet;
 import java.util.SortedSet;
 import java.util.TreeSet;
+
+import org.testng.Assert;
+import org.testng.Assert.ThrowingRunnable;
 import org.testng.annotations.Test;
 import org.testng.annotations.DataProvider;
 
-import static org.testng.Assert.fail;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
 public class EmptyNavigableSet {

@@ -65,30 +67,35 @@
         assertInstance(obj, NavigableSet.class, message);
         assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
             ((null != message) ? message : "") + " Not empty. ");
     }
 
-    public interface Thrower<T extends Throwable> {
+    private <T extends Throwable> void assertThrows(Class<T> throwableClass,
+                                                    ThrowingRunnable runnable,
+                                                    String message) {
+        try {
+            Assert.assertThrows(throwableClass, runnable);
+        } catch (AssertionError e) {
+            throw new AssertionError(String.format("%s%n%s",
+                    ((null != message) ? message : ""), e.getMessage()), e);
+        }
+    }
 
-        public void run() throws T;
+    private void assertThrowsCCE(ThrowingRunnable r, String s) {
+        assertThrows(ClassCastException.class, r, s);
     }
 
-    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
-        assertThrows(thrower, throwable, null);
+    private void assertThrowsNPE(ThrowingRunnable r, String s) {
+        assertThrows(NullPointerException.class, r, s);
     }
 
-    public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
-        Throwable result;
-        try {
-            thrower.run();
-            fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
-            return;
-        } catch (Throwable caught) {
-            result = caught;
+    private void assertThrowsIAE(ThrowingRunnable r, String s) {
+        assertThrows(IllegalArgumentException.class, r, s);
         }
 
-        assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+    private void assertThrowsNSEE(ThrowingRunnable r, String s) {
+        assertThrows(NoSuchElementException.class, r, s);
     }
 
     public static final boolean isDescending(SortedSet<?> set) {
         if (null == set.comparator()) {
             // natural order

@@ -121,14 +128,13 @@
     /**
      * Tests that contains requires Comparable
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
-        assertThrows(() -> {
+        assertThrowsCCE(() -> {
             navigableSet.contains(new Object());
         },
-            ClassCastException.class,
             description + ": Compareable should be required");
     }
 
     /**
      * Tests that the contains method returns {@code false}.

@@ -174,28 +180,26 @@
     /**
      * Tests that the first() method throws NoSuchElementException
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testFirst(String description, NavigableSet<?> navigableSet) {
-        assertThrows(() -> {
+        assertThrowsNSEE(() -> {
             navigableSet.first();
-        }, NoSuchElementException.class, description);
+        }, description);
     }
 
     /**
      * Tests the headSet() method.
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testHeadSet(String description, NavigableSet navigableSet) {
-        assertThrows(
+        assertThrowsNPE(
             () -> { NavigableSet ns = navigableSet.headSet(null, false); },
-            NullPointerException.class,
             description + ": Must throw NullPointerException for null element");
 
-        assertThrows(
+        assertThrowsCCE(
             () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
-            ClassCastException.class,
             description + ": Must throw ClassCastException for non-Comparable element");
 
         NavigableSet ns = navigableSet.headSet("1", false);
 
         assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");

@@ -204,13 +208,13 @@
     /**
      * Tests that the last() method throws NoSuchElementException
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testLast(String description, NavigableSet<?> navigableSet) {
-        assertThrows(() -> {
+        assertThrowsNSEE(() -> {
             navigableSet.last();
-        }, NoSuchElementException.class, description);
+        }, description);
     }
 
     /**
      * Tests that the size is 0.
      */

@@ -222,69 +226,63 @@
     /**
      * Tests the subSet() method.
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testSubSet(String description, NavigableSet navigableSet) {
-        assertThrows(
+        assertThrowsNPE(
             () -> {
                 SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
             },
-            NullPointerException.class,
             description + ": Must throw NullPointerException for null element");
 
-        assertThrows(
+        assertThrowsNPE(
             () -> {
                 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
             },
-            NullPointerException.class,
             description + ": Must throw NullPointerException for null element");
 
-        assertThrows(
+        assertThrowsNPE(
             () -> {
                 SortedSet ss = navigableSet.subSet(null, null);
             },
-            NullPointerException.class,
             description + ": Must throw NullPointerException for null element");
 
         Object obj1 = new Object();
         Object obj2 = new Object();
 
-        assertThrows(
+        assertThrowsCCE(
             () -> {
                 SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
             },
-            ClassCastException.class, description
-            + ": Must throw ClassCastException for parameter which is not Comparable.");
+            description + ": Must throw ClassCastException for parameter which is not Comparable.");
 
-        assertThrows(
+        assertThrowsCCE(
             () -> {
                 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
             },
-            ClassCastException.class, description
-            + ": Must throw ClassCastException for parameter which is not Comparable.");
+            description + ": Must throw ClassCastException for parameter which is not Comparable.");
 
-        assertThrows(
+        assertThrowsCCE(
             () -> {
                 SortedSet ss = navigableSet.subSet(obj1, obj2);
             },
-            ClassCastException.class, description
-            + ": Must throw ClassCastException for parameter which is not Comparable.");
+            description + ": Must throw ClassCastException for parameter which is not Comparable.");
 
         // minimal range
         navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
         navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
         navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
         navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
 
         Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
         Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
 
-            assertThrows(
+            assertThrowsIAE(
                 () -> {
                     navigableSet.subSet(last, true, first, false);
                 },
-                IllegalArgumentException.class, description
+                description
                 + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
 
         navigableSet.subSet(first, true, last, false);
     }
 

@@ -299,14 +297,13 @@
         subSet.subSet(first, true, last, true);
 
         // slightly smaller
         NavigableSet ns = subSet.subSet(first, false, last, false);
         // slight expansion
-        assertThrows(() -> {
+        assertThrowsIAE(() -> {
             ns.subSet(first, true, last, true);
         },
-            IllegalArgumentException.class,
             description + ": Expansion should not be allowed");
 
         // much smaller
         subSet.subSet(first, false, BigInteger.ONE, false);
     }

@@ -320,14 +317,13 @@
 
         // slightly smaller
         NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
 
         // slight expansion
-        assertThrows(() -> {
+        assertThrowsIAE(() -> {
             ns.headSet(BigInteger.ONE, true);
         },
-            IllegalArgumentException.class,
             description + ": Expansion should not be allowed");
 
         // much smaller
         subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
     }

@@ -341,14 +337,13 @@
 
         // slightly smaller
         NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
 
         // slight expansion
-        assertThrows(() -> {
+        assertThrowsIAE(() -> {
             ns.tailSet(BigInteger.ONE, true);
         },
-            IllegalArgumentException.class,
             description + ": Expansion should not be allowed");
 
         // much smaller
         subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
     }

@@ -356,19 +351,18 @@
     /**
      * Tests the tailSet() method.
      */
     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
     public void testTailSet(String description, NavigableSet navigableSet) {
-        assertThrows(() -> {
+        assertThrowsNPE(() -> {
             navigableSet.tailSet(null);
         },
-            NullPointerException.class,
             description + ": Must throw NullPointerException for null element");
 
-        assertThrows(() -> {
+        assertThrowsCCE(() -> {
             navigableSet.tailSet(new Object());
-        }, ClassCastException.class);
+        }, description);
 
         NavigableSet ss = navigableSet.tailSet("1", true);
 
         assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
     }
< prev index next >