< prev index next >
test/java/util/Collections/EmptyNavigableMap.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.
@@ -34,14 +34,16 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
+
+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.assertTrue;
import static org.testng.Assert.assertFalse;
public class EmptyNavigableMap {
@@ -63,30 +65,31 @@
assertInstance(obj, NavigableMap.class, message);
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0),
((null != message) ? message : "") + " Not empty. ");
}
- public interface Thrower<T extends Throwable> {
-
- public void run() throws T;
+ 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 static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
- assertThrows(thrower, throwable, null);
+ 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, String message) {
- Throwable result;
- try {
- thrower.run();
- fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
- return;
- } catch (Throwable caught) {
- result = caught;
+ private void assertThrowsNPE(ThrowingRunnable r, String s) {
+ assertThrows(NullPointerException.class, r, s);
}
- assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+ private void assertThrowsIAE(ThrowingRunnable r, String s) {
+ assertThrows(IllegalArgumentException.class, r, s);
}
public static final boolean isDescending(SortedMap<?,?> set) {
if (null == set.comparator()) {
// natural order
@@ -119,14 +122,13 @@
/**
* Tests that contains requires Comparable
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
- assertThrows(() -> {
+ assertThrowsCCE(() -> {
navigableMap.containsKey(new Object());
},
- ClassCastException.class,
description + ": Compareable should be required");
}
/**
* Tests that the contains method returns {@code false}.
@@ -173,18 +175,16 @@
/**
* Tests the headMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testHeadMap(String description, NavigableMap navigableMap) {
- assertThrows(
+ assertThrowsNPE(
() -> { NavigableMap ss = navigableMap.headMap(null, false); },
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
- assertThrows(
+ assertThrowsCCE(
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
- ClassCastException.class,
description + ": Must throw ClassCastException for non-Comparable element");
NavigableMap ss = navigableMap.headMap("1", false);
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
@@ -201,70 +201,63 @@
/**
* Tests the subMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testSubMap(String description, NavigableMap navigableMap) {
- assertThrows(
+ assertThrowsNPE(
() -> {
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
- assertThrows(
+ assertThrowsNPE(
() -> {
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
- assertThrows(
+ assertThrowsNPE(
() -> {
SortedMap ss = navigableMap.subMap(null, null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
Object obj1 = new Object();
Object obj2 = new Object();
- assertThrows(
+ assertThrowsCCE(
() -> {
SortedMap ss = navigableMap.subMap(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(
() -> {
SortedMap ss = navigableMap.subMap(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(
() -> {
SortedMap ss = navigableMap.subMap(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
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true);
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false);
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true);
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
- assertThrows(
+ assertThrowsIAE(
() -> {
navigableMap.subMap(last, true, first, false);
},
- IllegalArgumentException.class, description
- + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
+ description + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
navigableMap.subMap(first, true, last, false);
}
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
@@ -278,14 +271,13 @@
subMap.subMap(first, true, last, true);
// slightly smaller
NavigableMap ns = subMap.subMap(first, false, last, false);
// slight expansion
- assertThrows(() -> {
+ assertThrowsIAE(() -> {
ns.subMap(first, true, last, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.subMap(first, false, BigInteger.ONE, false);
}
@@ -299,14 +291,13 @@
// slightly smaller
NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
// slight expansion
- assertThrows(() -> {
+ assertThrowsIAE(() -> {
ns.headMap(BigInteger.ONE, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
@@ -320,14 +311,13 @@
// slightly smaller
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
// slight expansion
- assertThrows(() -> {
+ assertThrowsIAE(() -> {
ns.tailMap(BigInteger.ONE, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
@@ -335,19 +325,19 @@
/**
* Tests the tailMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMap(String description, NavigableMap navigableMap) {
- assertThrows(() -> {
+ assertThrowsNPE(() -> {
navigableMap.tailMap(null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
- assertThrows(() -> {
+ assertThrowsCCE(() -> {
navigableMap.tailMap(new Object());
- }, ClassCastException.class);
+ },
+ description);
NavigableMap ss = navigableMap.tailMap("1", true);
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
< prev index next >