test/java/util/Collections/EmptyNavigableSet.java

Print this page
rev 7019 : 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
Reviewed-by: duke

*** 1,7 **** /* ! * Copyright (c) 2011, 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, 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,351 **** * questions. */ /* * @test ! * @bug 4533691 ! * @summary Unit test for Collections.emptySortedSet */ - - import java.lang.reflect.Method; import java.math.BigInteger; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.SortedSet; import java.util.TreeSet; ! 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(); ! public static void main(String[] args) throws Exception { ! new EmptySortedSet(); ! } ! public EmptySortedSet() throws Exception { ! run(); } ! /** ! * 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; } } ! return isEmptySortedSet; } ! private void run() throws Exception { ! Method[] methods = this.getClass().getDeclaredMethods(); ! for (int i = 0; i < methods.length; i++) { ! Method method = methods[i]; ! String methodName = method.getName(); ! if (methodName.startsWith("test")) { 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()); } } } } ! 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()); } ! /** ! * ! */ ! private void test00() throws Exception { ! //throwException("test00", "This test has not been implemented yet."); } /** * Tests that the comparator is {@code null}. */ ! private void testComparatorIsNull() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! Comparator comparator = sortedSet.comparator(); ! if (comparator != null) { ! throwException("testComparatorIsNull", "Comparator is not null."); } } /** * Tests that the contains method returns {@code false}. */ ! private void testContains() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! ! if (sortedSet.contains(new Object())) { ! throwException("testContains", "Should not contain any elements."); ! } } /** * Tests that the containsAll 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."); ! } } /** * Tests that the iterator is empty. */ ! private void testEmptyIterator() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! Iterator emptyIterator = sortedSet.iterator(); ! if ((emptyIterator != null) && (emptyIterator.hasNext())) { ! throwException("testEmptyIterator", "The iterator is not empty."); ! } } /** * Tests that the set is empty. */ ! private void testIsEmpty() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! ! if ((sortedSet != null) && (!sortedSet.isEmpty())) { ! throwException("testSizeIsZero", "The set is not empty."); ! } } /** * Tests that the first() method throws NoSuchElementException */ ! private void testFirst() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! ! try { ! sortedSet.first(); ! throwException("testFirst", ! "NoSuchElemenException was not thrown."); ! } catch(NoSuchElementException nsee) { ! // Do nothing ! } } /** * Tests the headSet() method. */ ! private void testHeadSet() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); SortedSet ss; try { ! ss = sortedSet.headSet(null); ! throwException("testHeadSet", ! "Must throw NullPointerException for null element"); ! } catch(NullPointerException npe) { // Do nothing } try { ! ss = sortedSet.headSet(new Object()); ! throwException("testHeadSet", ! "Must throw ClassCastException for non-Comparable element"); ! } catch(ClassCastException cce) { // Do nothing. } ! 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 ! } } /** * 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."); ! } } /** * 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 - } - - 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. - } ! ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.TEN); ! if (!isEmptySortedSet(ss)) { ! throw new Exception("Returned value is not empty sorted set."); ! } } /** * Tests the tailSet() method. */ ! private void testTailSet() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! SortedSet ss; ! try { ! ss = sortedSet.tailSet(null); ! throwException("testTailSet", ! "Must throw NullPointerException for null element"); ! } catch(NullPointerException npe) { ! // Do nothing ! } ! ! try { ! SortedSet ss2 = sortedSet.tailSet(new Object()); ! throwException("testTailSet", ! "Must throw ClassCastException for non-Comparable element"); ! } catch(ClassCastException cce) { ! // Do nothing. ! } ! ss = sortedSet.tailSet("1"); ! ! if ((ss == null) || !isEmptySortedSet(ss)) { ! throwException("testTailSet", ! "Returned value is null or not an EmptySortedSet."); ! } } /** * Tests that the array has a size of 0. */ ! private void testToArray() throws Exception { ! SortedSet sortedSet = Collections.emptySortedSet(); ! Object[] emptySortedSetArray = sortedSet.toArray(); ! ! if ((emptySortedSetArray == null) || (emptySortedSetArray.length > 0)) { ! throwException("testToArray", ! "Returned null array or array with length > 0."); ! } ! ! String[] strings = new String[2]; ! strings[0] = "1"; ! strings[1] = "2"; ! emptySortedSetArray = sortedSet.toArray(strings); ! ! if ((emptySortedSetArray == null) || (emptySortedSetArray[0] != null)) { ! throwException("testToArray", ! "Returned null array or array with length > 0."); } } } --- 21,414 ---- * questions. */ /* * @test ! * @bug 4533691 7129185 ! * @summary Unit test for Collections.emptyNavigableSet ! * @run testng EmptyNavigableSet */ 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.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.assertEquals; ! import static org.testng.Assert.assertTrue; ! import static org.testng.Assert.assertFalse; ! import static org.testng.Assert.assertSame; ! public class EmptyNavigableSet { ! public static <T> void assertInstance(T actual, Class<? extends T> expected) { ! assertInstance(expected.isInstance(actual), null); } ! 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 assertEmptyNavigableSet(Object obj) { + assertInstance(obj, NavigableSet.class); + assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0)); } ! public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) { ! 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 + return false; } + + if (Collections.reverseOrder() == set.comparator()) { + // reverse natural order. + return true; } ! if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) { ! // it's a Collections.reverseOrder(Comparator). ! return true; } ! throw new IllegalStateException("can't determine ordering for " + set); } /** * Tests that the comparator is {@code null}. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) { ! Comparator comparator = navigableSet.comparator(); ! assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null."); } + + /** + * 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}. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testContains(String description, NavigableSet<?> navigableSet) { ! assertFalse(navigableSet.contains(new Integer(1)), ! description + ": Should not contain any elements."); } /** * Tests that the containsAll method returns {@code false}. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testContainsAll(String description, NavigableSet<?> navigableSet) { TreeSet treeSet = new TreeSet(); treeSet.add("1"); treeSet.add("2"); treeSet.add("3"); ! assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements."); } /** * Tests that the iterator is empty. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testEmptyIterator(String description, NavigableSet<?> navigableSet) { ! Iterator emptyIterator = navigableSet.iterator(); ! assertFalse((emptyIterator != null) && (emptyIterator.hasNext()), ! "The iterator is not empty."); } /** * Tests that the set is empty. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testIsEmpty(String description, NavigableSet<?> navigableSet) { ! assertTrue(navigableSet.isEmpty(), "The set is not empty."); } /** * 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) { SortedSet ss; try { ! ss = navigableSet.headSet(null); ! fail(description + ": Must throw NullPointerException for null element"); ! } catch (NullPointerException npe) { // Do nothing } try { ! ss = navigableSet.headSet(new Object()); ! fail(description + ": Must throw ClassCastException for non-Comparable element"); ! } catch (ClassCastException cce) { // Do nothing. } ! ss = navigableSet.headSet("1", false); ! assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set."); } /** * 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. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testSizeIsZero(String description, NavigableSet<?> navigableSet) { ! assertTrue(0 == navigableSet.size(), "The size of the set is not 0."); } /** * 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 then then toElement."); ! ! navigableSet.subSet(first, true, last, false); ! } ! ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testSubSetRanges(String description, NavigableSet navigableSet) { ! Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; ! Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; ! ! NavigableSet subSet = navigableSet.subSet(first, true, last, true); ! ! // same subset ! subSet.subSet(first, true, last, true); ! ! // slightly smaller ! NavigableSet ns = subSet.subSet(first, false, last, false); ! // slight exapansion ! assertThrows(() -> { ! ns.subSet(first, true, last, true); ! }, ! IllegalArgumentException.class, ! description + ": Expansion should not be allowed"); ! ! // much smaller ! subSet.subSet(first, false, BigInteger.ONE, false); ! } ! ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testheadSetRanges(String description, NavigableSet navigableSet) { ! NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true); ! ! // same subset ! subSet.headSet(BigInteger.ONE, true); ! ! // slightly smaller ! NavigableSet ns = subSet.headSet(BigInteger.ONE, false); ! // slight exapansion ! 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); ! } ! ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testTailSetRanges(String description, NavigableSet navigableSet) { ! NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true); ! ! // same subset ! subSet.tailSet(BigInteger.ONE, true); ! ! // slightly smaller ! NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); ! // slight exapansion ! 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); } /** * 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."); } /** * Tests that the array has a size of 0. */ ! @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) ! public void testToArray(String description, NavigableSet<?> navigableSet) { ! Object[] emptyNavigableSetArray = navigableSet.toArray(); ! ! assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array."); ! ! emptyNavigableSetArray = new Object[20]; ! ! Object[] result = navigableSet.toArray(emptyNavigableSetArray); ! ! assertSame(emptyNavigableSetArray, result); ! ! assertTrue(result[0] == null); ! } ! ! @DataProvider(name = "NavigableSet<?>", parallel = true) ! public static Iterator<Object[]> navigableSetsProvider() { ! return makeNavigableSets().iterator(); } + + public static Collection<Object[]> makeNavigableSets() { + return Arrays.asList( + new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())}, + new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())}, + new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())}, + new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()}, + new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()}, + new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()} + ); } }