test/java/util/Collections/EmptyNavigableMap.java
Print this page
rev 7461 : 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
Reviewed-by: dmocek, martin, smarks
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * 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.
@@ -21,331 +21,353 @@
* questions.
*/
/*
* @test
- * @bug 4533691
- * @summary Unit test for Collections.emptySortedSet
+ * @bug 4533691 7129185
+ * @summary Unit test for Collections.emptyNavigableMap
+ * @run testng EmptyNavigableMap
*/
-
-import java.lang.reflect.Method;
import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.NavigableMap;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
-public class EmptySortedSet {
- static int status = 0;
- private static final String FAILED = " failed. ";
- private static final String PERIOD = ".";
- private final String thisClassName = this.getClass().getName();
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
- public static void main(String[] args) throws Exception {
- new EmptySortedSet();
- }
+public class EmptyNavigableMap {
- public EmptySortedSet() throws Exception {
- run();
+ public static <T> void assertInstance(T actual, Class<? extends T> expected) {
+ assertInstance(expected.isInstance(actual), null);
}
- /**
- * Returns {@code true} if the {@link Object} passed in is an empty
- * {@link SortedSet}.
- *
- * @param obj the object to test
- * @return {@code true} if the {@link Object} is an empty {@link SortedSet}
- * otherwise {@code false}.
- */
- private boolean isEmptySortedSet(Object obj) {
- boolean isEmptySortedSet = false;
-
- // We determine if the object is an empty sorted set by testing if it's
- // an instance of SortedSet, and if so, if it's empty. Currently the
- // testing doesn't include checks of the other methods.
- if (obj instanceof SortedSet) {
- SortedSet ss = (SortedSet) obj;
-
- if ((ss.isEmpty()) && (ss.size() == 0)) {
- isEmptySortedSet = true;
+ public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
+ assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
+ + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
}
+
+ public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) {
+ assertInstance(obj, NavigableMap.class);
+ assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0));
}
- return isEmptySortedSet;
+ public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) {
+ assertInstance(obj, NavigableMap.class, message);
+ assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0),
+ ((null != message) ? message : "") + " Not empty. ");
}
- private void run() throws Exception {
- Method[] methods = this.getClass().getDeclaredMethods();
+ public interface Thrower<T extends Throwable> {
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- String methodName = method.getName();
+ public void run() throws T;
+ }
+
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
+ assertThrows(thrower, throwable, null);
+ }
- if (methodName.startsWith("test")) {
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
+ Throwable result;
try {
- Object obj = method.invoke(this, new Object[0]);
- } catch(Exception e) {
- throw new Exception(this.getClass().getName() + "." +
- methodName + " test failed, test exception "
- + "follows\n" + e.getCause());
+ 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(SortedMap<?,?> set) {
+ if (null == set.comparator()) {
+ // natural order
+ return false;
}
+
+ if (Collections.reverseOrder() == set.comparator()) {
+ // reverse natural order.
+ return true;
}
- private void throwException(String methodName, String reason)
- throws Exception
- {
- StringBuilder sb = new StringBuilder(thisClassName);
- sb.append(PERIOD);
- sb.append(methodName);
- sb.append(FAILED);
- sb.append(reason);
- throw new Exception(sb.toString());
+ if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
+ // it's a Collections.reverseOrder(Comparator).
+ return true;
}
- /**
- *
- */
- private void test00() throws Exception {
- //throwException("test00", "This test has not been implemented yet.");
+ throw new IllegalStateException("can't determine ordering for " + set);
}
/**
* Tests that the comparator is {@code null}.
*/
- private void testComparatorIsNull() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Comparator comparator = sortedSet.comparator();
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) {
+ Comparator comparator = navigableMap.comparator();
- if (comparator != null) {
- throwException("testComparatorIsNull", "Comparator is not null.");
- }
+ assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
}
/**
- * Tests that the contains method returns {@code false}.
+ * Tests that contains requires Comparable
*/
- private void testContains() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- if (sortedSet.contains(new Object())) {
- throwException("testContains", "Should not contain any elements.");
- }
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
+ assertThrows(() -> {
+ navigableMap.containsKey(new Object());
+ },
+ ClassCastException.class,
+ description + ": Compareable should be required");
}
/**
- * Tests that the containsAll method returns {@code false}.
+ * Tests that the contains method returns {@code false}.
*/
- private void testContainsAll() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- TreeSet treeSet = new TreeSet();
- treeSet.add("1");
- treeSet.add("2");
- treeSet.add("3");
-
- if (sortedSet.containsAll(treeSet)) {
- throwException("testContainsAll",
- "Should not contain any elements.");
- }
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContains(String description, NavigableMap<?,?> navigableMap) {
+ assertFalse(navigableMap.containsKey(new Integer(1)),
+ description + ": Should not contain any elements.");
+ assertFalse(navigableMap.containsValue(new Integer(1)),
+ description + ": Should not contain any elements.");
}
/**
- * Tests that the iterator is empty.
+ * Tests that the containsAll method returns {@code false}.
*/
- private void testEmptyIterator() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Iterator emptyIterator = sortedSet.iterator();
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContainsAll(String description, NavigableMap<?,?> navigableMap) {
+ TreeMap treeMap = new TreeMap();
+ treeMap.put("1", 1);
+ treeMap.put("2", 2);
+ treeMap.put("3", 3);
- if ((emptyIterator != null) && (emptyIterator.hasNext())) {
- throwException("testEmptyIterator", "The iterator is not empty.");
- }
+ assertFalse(navigableMap.equals(treeMap), "Should not contain any elements.");
}
/**
- * Tests that the set is empty.
+ * Tests that the iterator is empty.
*/
- private void testIsEmpty() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- if ((sortedSet != null) && (!sortedSet.isEmpty())) {
- throwException("testSizeIsZero", "The set is not empty.");
- }
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) {
+ assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty.");
+ assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty.");
+ assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty.");
}
/**
- * Tests that the first() method throws NoSuchElementException
+ * Tests that the set is empty.
*/
- private void testFirst() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- try {
- sortedSet.first();
- throwException("testFirst",
- "NoSuchElemenException was not thrown.");
- } catch(NoSuchElementException nsee) {
- // Do nothing
- }
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) {
+ assertTrue(navigableMap.isEmpty(), "The set is not empty.");
}
/**
- * Tests the headSet() method.
+ * Tests the headMap() method.
*/
- private void testHeadSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss;
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testHeadMap(String description, NavigableMap navigableMap) {
+ assertThrows(
+ () -> { NavigableMap ss = navigableMap.headMap(null, false); },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
- try {
- ss = sortedSet.headSet(null);
- throwException("testHeadSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
- }
+ assertThrows(
+ () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
+ ClassCastException.class,
+ description + ": Must throw ClassCastException for non-Comparable element");
- try {
- ss = sortedSet.headSet(new Object());
- throwException("testHeadSet",
- "Must throw ClassCastException for non-Comparable element");
- } catch(ClassCastException cce) {
- // Do nothing.
- }
+ NavigableMap ss = navigableMap.headMap("1", false);
- ss = sortedSet.headSet("1");
-
- if ((ss == null) || !isEmptySortedSet(ss)) {
- throwException("testHeadSet",
- "Returned value is null or not an EmptySortedSet.");
- }
- }
-
- /**
- * Tests that the last() method throws NoSuchElementException
- */
- private void testLast() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- try {
- sortedSet.last();
- throwException("testLast",
- "NoSuchElemenException was not thrown.");
- } catch(NoSuchElementException nsee) {
- // Do nothing
- }
+ assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
/**
* Tests that the size is 0.
*/
- private void testSizeIsZero() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- int size = sortedSet.size();
-
- if (size > 0) {
- throwException("testSizeIsZero",
- "The size of the set is greater then 0.");
- }
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) {
+ assertTrue(0 == navigableMap.size(), "The size of the set is not 0.");
}
/**
- * Tests the subSet() method.
- */
- private void testSubSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss = sortedSet.headSet("1");
-
- try {
- ss = sortedSet.subSet(null, BigInteger.TEN);
- ss = sortedSet.subSet(BigInteger.ZERO, null);
- ss = sortedSet.subSet(null, null);
- throwException("testSubSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
- }
+ * Tests the subMap() method.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSubMap(String description, NavigableMap navigableMap) {
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(null, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
- try {
Object obj1 = new Object();
Object obj2 = new Object();
- ss = sortedSet.subSet(obj1, BigInteger.TEN);
- ss = sortedSet.subSet(BigInteger.ZERO, obj2);
- ss = sortedSet.subSet(obj1, obj2);
- throwException("testSubSet",
- "Must throw ClassCastException for parameter which is "
- + "not Comparable.");
- } catch(ClassCastException cce) {
- // Do nothing.
- }
- try {
- ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.ZERO);
- ss = sortedSet.subSet(BigInteger.TEN, BigInteger.ZERO);
- throwException("testSubSet",
- "Must throw IllegalArgumentException when fromElement is "
- + "not less then then toElement.");
- } catch(IllegalArgumentException iae) {
- // Do nothing.
- }
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
- ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.TEN);
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
- if (!isEmptySortedSet(ss)) {
- throw new Exception("Returned value is not empty sorted set.");
- }
- }
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(obj1, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
- /**
- * Tests the tailSet() method.
- */
- private void testTailSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss;
+ // 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);
- try {
- ss = sortedSet.tailSet(null);
- throwException("testTailSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
+ Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ assertThrows(
+ () -> {
+ navigableMap.subMap(last, true, first, false);
+ },
+ IllegalArgumentException.class, description
+ + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");
+
+ navigableMap.subMap(first, true, last, false);
}
- try {
- SortedSet ss2 = sortedSet.tailSet(new Object());
- throwException("testTailSet",
- "Must throw ClassCastException for non-Comparable element");
- } catch(ClassCastException cce) {
- // Do nothing.
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSubMapRanges(String description, NavigableMap navigableMap) {
+ Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ NavigableMap subMap = navigableMap.subMap(first, true, last, true);
+
+ // same subset
+ subMap.subMap(first, true, last, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.subMap(first, false, last, false);
+ // slight exapansion
+ assertThrows(() -> {
+ ns.subMap(first, true, last, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.subMap(first, false, BigInteger.ONE, false);
}
- ss = sortedSet.tailSet("1");
-
- if ((ss == null) || !isEmptySortedSet(ss)) {
- throwException("testTailSet",
- "Returned value is null or not an EmptySortedSet.");
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testheadMapRanges(String description, NavigableMap navigableMap) {
+ NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);
+
+ // same subset
+ subMap.headMap(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.headMap(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
+
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testTailMapRanges(String description, NavigableMap navigableMap) {
+ NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
+
+ // same subset
+ subMap.tailMap(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.tailMap(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
/**
- * Tests that the array has a size of 0.
+ * Tests the tailMap() method.
*/
- private void testToArray() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Object[] emptySortedSetArray = sortedSet.toArray();
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testTailMap(String description, NavigableMap navigableMap) {
+ assertThrows(() -> {
+ navigableMap.tailMap(null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
- if ((emptySortedSetArray == null) || (emptySortedSetArray.length > 0)) {
- throwException("testToArray",
- "Returned null array or array with length > 0.");
- }
+ assertThrows(() -> {
+ navigableMap.tailMap(new Object());
+ }, ClassCastException.class);
- String[] strings = new String[2];
- strings[0] = "1";
- strings[1] = "2";
- emptySortedSetArray = sortedSet.toArray(strings);
+ NavigableMap ss = navigableMap.tailMap("1", true);
- if ((emptySortedSetArray == null) || (emptySortedSetArray[0] != null)) {
- throwException("testToArray",
- "Returned null array or array with length > 0.");
+ assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
+
+ @DataProvider(name = "NavigableMap<?,?>", parallel = true)
+ public static Iterator<Object[]> navigableMapsProvider() {
+ return makeNavigableMaps().iterator();
+ }
+
+ public static Collection<Object[]> makeNavigableMaps() {
+ return Arrays.asList(
+ new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())},
+ new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())},
+ new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())},
+ new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()},
+ new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()},
+ new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()}
+ );
}
}