< prev index next >
test/java/util/Collections/EmptyNavigableSet.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2011, 2013, 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.
--- 1,7 ----
/*
! * 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,48 ****
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
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 {
--- 35,50 ----
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.assertFalse;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
public class EmptyNavigableSet {
*** 65,94 ****
assertInstance(obj, NavigableSet.class, message);
assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
((null != message) ? message : "") + " Not empty. ");
}
! public interface Thrower<T extends Throwable> {
! public void run() throws T;
}
! public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
! assertThrows(thrower, throwable, null);
}
! 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;
}
! assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
}
public static final boolean isDescending(SortedSet<?> set) {
if (null == set.comparator()) {
// natural order
--- 67,101 ----
assertInstance(obj, NavigableSet.class, message);
assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
((null != message) ? message : "") + " Not empty. ");
}
! 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);
! }
! }
! private void assertThrowsCCE(ThrowingRunnable r, String s) {
! assertThrows(ClassCastException.class, r, s);
}
! private void assertThrowsNPE(ThrowingRunnable r, String s) {
! assertThrows(NullPointerException.class, r, s);
}
! private void assertThrowsIAE(ThrowingRunnable r, String s) {
! assertThrows(IllegalArgumentException.class, r, s);
}
! 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,134 ****
/**
* Tests that contains requires Comparable
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
! assertThrows(() -> {
navigableSet.contains(new Object());
},
- ClassCastException.class,
description + ": Compareable should be required");
}
/**
* Tests that the contains method returns {@code false}.
--- 128,140 ----
/**
* Tests that contains requires Comparable
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
! assertThrowsCCE(() -> {
navigableSet.contains(new Object());
},
description + ": Compareable should be required");
}
/**
* Tests that the contains method returns {@code false}.
*** 174,201 ****
/**
* Tests that the first() method throws NoSuchElementException
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testFirst(String description, NavigableSet<?> navigableSet) {
! assertThrows(() -> {
navigableSet.first();
! }, NoSuchElementException.class, description);
}
/**
* Tests the headSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testHeadSet(String description, NavigableSet navigableSet) {
! assertThrows(
() -> { NavigableSet ns = navigableSet.headSet(null, false); },
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
! assertThrows(
() -> { 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.");
--- 180,205 ----
/**
* Tests that the first() method throws NoSuchElementException
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testFirst(String description, NavigableSet<?> navigableSet) {
! assertThrowsNSEE(() -> {
navigableSet.first();
! }, description);
}
/**
* Tests the headSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testHeadSet(String description, NavigableSet navigableSet) {
! assertThrowsNPE(
() -> { NavigableSet ns = navigableSet.headSet(null, false); },
description + ": Must throw NullPointerException for null element");
! assertThrowsCCE(
() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
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,216 ****
/**
* Tests that the last() method throws NoSuchElementException
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testLast(String description, NavigableSet<?> navigableSet) {
! assertThrows(() -> {
navigableSet.last();
! }, NoSuchElementException.class, description);
}
/**
* Tests that the size is 0.
*/
--- 208,220 ----
/**
* Tests that the last() method throws NoSuchElementException
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testLast(String description, NavigableSet<?> navigableSet) {
! assertThrowsNSEE(() -> {
navigableSet.last();
! }, description);
}
/**
* Tests that the size is 0.
*/
*** 222,290 ****
/**
* Tests the subSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSet(String description, NavigableSet navigableSet) {
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(null, null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
Object obj1 = new Object();
Object obj2 = new Object();
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
},
! ClassCastException.class, description
! + ": Must throw ClassCastException for parameter which is not Comparable.");
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
},
! ClassCastException.class, description
! + ": Must throw ClassCastException for parameter which is not Comparable.");
! assertThrows(
() -> {
SortedSet ss = navigableSet.subSet(obj1, obj2);
},
! ClassCastException.class, 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(
() -> {
navigableSet.subSet(last, true, first, false);
},
! IllegalArgumentException.class, description
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
navigableSet.subSet(first, true, last, false);
}
--- 226,288 ----
/**
* Tests the subSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSet(String description, NavigableSet navigableSet) {
! assertThrowsNPE(
() -> {
SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
},
description + ": Must throw NullPointerException for null element");
! assertThrowsNPE(
() -> {
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
},
description + ": Must throw NullPointerException for null element");
! assertThrowsNPE(
() -> {
SortedSet ss = navigableSet.subSet(null, null);
},
description + ": Must throw NullPointerException for null element");
Object obj1 = new Object();
Object obj2 = new Object();
! assertThrowsCCE(
() -> {
SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
},
! description + ": Must throw ClassCastException for parameter which is not Comparable.");
! assertThrowsCCE(
() -> {
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
},
! description + ": Must throw ClassCastException for parameter which is not Comparable.");
! assertThrowsCCE(
() -> {
SortedSet ss = navigableSet.subSet(obj1, obj2);
},
! 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;
! assertThrowsIAE(
() -> {
navigableSet.subSet(last, true, first, false);
},
! description
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
navigableSet.subSet(first, true, last, false);
}
*** 299,312 ****
subSet.subSet(first, true, last, true);
// slightly smaller
NavigableSet ns = subSet.subSet(first, false, last, false);
// slight expansion
! assertThrows(() -> {
ns.subSet(first, true, last, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.subSet(first, false, BigInteger.ONE, false);
}
--- 297,309 ----
subSet.subSet(first, true, last, true);
// slightly smaller
NavigableSet ns = subSet.subSet(first, false, last, false);
// slight expansion
! assertThrowsIAE(() -> {
ns.subSet(first, true, last, true);
},
description + ": Expansion should not be allowed");
// much smaller
subSet.subSet(first, false, BigInteger.ONE, false);
}
*** 320,333 ****
// slightly smaller
NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
// slight expansion
! assertThrows(() -> {
ns.headSet(BigInteger.ONE, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
--- 317,329 ----
// slightly smaller
NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
// slight expansion
! assertThrowsIAE(() -> {
ns.headSet(BigInteger.ONE, true);
},
description + ": Expansion should not be allowed");
// much smaller
subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
*** 341,354 ****
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight expansion
! assertThrows(() -> {
ns.tailSet(BigInteger.ONE, true);
},
- IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
--- 337,349 ----
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight expansion
! assertThrowsIAE(() -> {
ns.tailSet(BigInteger.ONE, true);
},
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
*** 356,374 ****
/**
* Tests the tailSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSet(String description, NavigableSet navigableSet) {
! assertThrows(() -> {
navigableSet.tailSet(null);
},
- NullPointerException.class,
description + ": Must throw NullPointerException for null element");
! assertThrows(() -> {
navigableSet.tailSet(new Object());
! }, ClassCastException.class);
NavigableSet ss = navigableSet.tailSet("1", true);
assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
}
--- 351,368 ----
/**
* Tests the tailSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSet(String description, NavigableSet navigableSet) {
! assertThrowsNPE(() -> {
navigableSet.tailSet(null);
},
description + ": Must throw NullPointerException for null element");
! assertThrowsCCE(() -> {
navigableSet.tailSet(new Object());
! }, description);
NavigableSet ss = navigableSet.tailSet("1", true);
assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
}
< prev index next >