1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4533691 7129185
  27  * @summary Unit test for Collections.emptyNavigableSet
  28  * @run testng EmptyNavigableSet
  29  */
  30 import java.math.BigInteger;
  31 import java.util.Arrays;
  32 import java.util.Collection;
  33 import java.util.Collections;
  34 import java.util.Comparator;
  35 import java.util.Iterator;
  36 import java.util.NoSuchElementException;
  37 import java.util.NavigableSet;
  38 import java.util.SortedSet;
  39 import java.util.TreeSet;
  40 import org.testng.annotations.Test;
  41 import org.testng.annotations.DataProvider;
  42 
  43 import static org.testng.Assert.fail;
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertTrue;
  46 import static org.testng.Assert.assertFalse;
  47 import static org.testng.Assert.assertSame;
  48 
  49 public class EmptyNavigableSet {
  50 
  51     public static <T> void assertInstance(T actual, Class<? extends T> expected) {
  52         assertInstance(expected.isInstance(actual), null);
  53     }
  54 
  55     public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
  56         assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
  57             + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
  58     }
  59 
  60     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) {
  61         assertInstance(obj, NavigableSet.class);
  62         assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));
  63     }
  64 
  65     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {
  66         assertInstance(obj, NavigableSet.class, message);
  67         assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
  68             ((null != message) ? message : "") + " Not empty. ");
  69     }
  70 
  71     public interface Thrower<T extends Throwable> {
  72 
  73         public void run() throws T;
  74     }
  75 
  76     public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
  77         assertThrows(thrower, throwable, null);
  78     }
  79 
  80     public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
  81         Throwable result;
  82         try {
  83             thrower.run();
  84             fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
  85             return;
  86         } catch (Throwable caught) {
  87             result = caught;
  88         }
  89 
  90         assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
  91     }
  92 
  93     public static final boolean isDescending(SortedSet<?> set) {
  94         if (null == set.comparator()) {
  95             // natural order
  96             return false;
  97         }
  98 
  99         if (Collections.reverseOrder() == set.comparator()) {
 100             // reverse natural order.
 101             return true;
 102         }
 103 
 104         if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
 105             // it's a Collections.reverseOrder(Comparator).
 106             return true;
 107         }
 108 
 109         throw new IllegalStateException("can't determine ordering for " + set);
 110     }
 111 
 112     /**
 113      * Tests that the comparator is {@code null}.
 114      */
 115     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 116     public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {
 117         Comparator comparator = navigableSet.comparator();
 118 
 119         assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
 120     }
 121 
 122     /**
 123      * Tests that contains requires Comparable
 124      */
 125     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 126     public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
 127         assertThrows(() -> {
 128             navigableSet.contains(new Object());
 129         },
 130             ClassCastException.class,
 131             description + ": Compareable should be required");
 132     }
 133 
 134     /**
 135      * Tests that the contains method returns {@code false}.
 136      */
 137     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 138     public void testContains(String description, NavigableSet<?> navigableSet) {
 139         assertFalse(navigableSet.contains(new Integer(1)),
 140             description + ": Should not contain any elements.");
 141     }
 142 
 143     /**
 144      * Tests that the containsAll method returns {@code false}.
 145      */
 146     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 147     public void testContainsAll(String description, NavigableSet<?> navigableSet) {
 148         TreeSet treeSet = new TreeSet();
 149         treeSet.add("1");
 150         treeSet.add("2");
 151         treeSet.add("3");
 152 
 153         assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");
 154     }
 155 
 156     /**
 157      * Tests that the iterator is empty.
 158      */
 159     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 160     public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
 161         Iterator emptyIterator = navigableSet.iterator();
 162 
 163         assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
 164             "The iterator is not empty.");
 165     }
 166 
 167     /**
 168      * Tests that the set is empty.
 169      */
 170     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 171     public void testIsEmpty(String description, NavigableSet<?> navigableSet) {
 172         assertTrue(navigableSet.isEmpty(), "The set is not empty.");
 173     }
 174 
 175     /**
 176      * Tests that the first() method throws NoSuchElementException
 177      */
 178     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 179     public void testFirst(String description, NavigableSet<?> navigableSet) {
 180         assertThrows(() -> {
 181             navigableSet.first();
 182         }, NoSuchElementException.class, description);
 183     }
 184 
 185     /**
 186      * Tests the headSet() method.
 187      */
 188     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 189     public void testHeadSet(String description, NavigableSet navigableSet) {
 190         SortedSet ss;
 191 
 192         try {
 193             ss = navigableSet.headSet(null);
 194             fail(description + ": Must throw NullPointerException for null element");
 195         } catch (NullPointerException npe) {
 196             // Do nothing
 197         }
 198 
 199         try {
 200             ss = navigableSet.headSet(new Object());
 201             fail(description + ": Must throw ClassCastException for non-Comparable element");
 202         } catch (ClassCastException cce) {
 203             // Do nothing.
 204         }
 205 
 206         ss = navigableSet.headSet("1", false);
 207 
 208         assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
 209     }
 210 
 211     /**
 212      * Tests that the last() method throws NoSuchElementException
 213      */
 214     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 215     public void testLast(String description, NavigableSet<?> navigableSet) {
 216         assertThrows(() -> {
 217             navigableSet.last();
 218         }, NoSuchElementException.class, description);
 219     }
 220 
 221     /**
 222      * Tests that the size is 0.
 223      */
 224     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 225     public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {
 226         assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");
 227     }
 228 
 229     /**
 230      * Tests the subSet() method.
 231      */
 232     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 233     public void testSubSet(String description, NavigableSet navigableSet) {
 234         assertThrows(
 235             () -> {
 236                 SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
 237             },
 238             NullPointerException.class,
 239             description + ": Must throw NullPointerException for null element");
 240 
 241         assertThrows(
 242             () -> {
 243                 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
 244             },
 245             NullPointerException.class,
 246             description + ": Must throw NullPointerException for null element");
 247 
 248         assertThrows(
 249             () -> {
 250                 SortedSet ss = navigableSet.subSet(null, null);
 251             },
 252             NullPointerException.class,
 253             description + ": Must throw NullPointerException for null element");
 254 
 255         Object obj1 = new Object();
 256         Object obj2 = new Object();
 257 
 258         assertThrows(
 259             () -> {
 260                 SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
 261             },
 262             ClassCastException.class, description
 263             + ": Must throw ClassCastException for parameter which is not Comparable.");
 264 
 265         assertThrows(
 266             () -> {
 267                 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
 268             },
 269             ClassCastException.class, description
 270             + ": Must throw ClassCastException for parameter which is not Comparable.");
 271 
 272         assertThrows(
 273             () -> {
 274                 SortedSet ss = navigableSet.subSet(obj1, obj2);
 275             },
 276             ClassCastException.class, description
 277             + ": Must throw ClassCastException for parameter which is not Comparable.");
 278 
 279         // minimal range
 280         navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
 281         navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
 282         navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
 283         navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
 284 
 285         Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
 286         Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
 287 
 288             assertThrows(
 289                 () -> {
 290                     navigableSet.subSet(last, true, first, false);
 291                 },
 292                 IllegalArgumentException.class, description
 293                 + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");
 294 
 295         navigableSet.subSet(first, true, last, false);
 296     }
 297 
 298     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 299     public void testSubSetRanges(String description, NavigableSet navigableSet) {
 300         Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
 301         Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
 302 
 303         NavigableSet subSet = navigableSet.subSet(first, true, last, true);
 304 
 305         // same subset
 306         subSet.subSet(first, true, last, true);
 307 
 308         // slightly smaller
 309         NavigableSet ns = subSet.subSet(first, false, last, false);
 310         // slight exapansion
 311         assertThrows(() -> {
 312             ns.subSet(first, true, last, true);
 313         },
 314             IllegalArgumentException.class,
 315             description + ": Expansion should not be allowed");
 316 
 317         // much smaller
 318         subSet.subSet(first, false, BigInteger.ONE, false);
 319     }
 320 
 321     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 322     public void testheadSetRanges(String description, NavigableSet navigableSet) {
 323         NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
 324 
 325         // same subset
 326         subSet.headSet(BigInteger.ONE, true);
 327 
 328         // slightly smaller
 329         NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
 330         // slight exapansion
 331         assertThrows(() -> {
 332             ns.headSet(BigInteger.ONE, true);
 333         },
 334             IllegalArgumentException.class,
 335             description + ": Expansion should not be allowed");
 336 
 337         // much smaller
 338         subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
 339     }
 340 
 341     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 342     public void testTailSetRanges(String description, NavigableSet navigableSet) {
 343         NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
 344 
 345         // same subset
 346         subSet.tailSet(BigInteger.ONE, true);
 347 
 348         // slightly smaller
 349         NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
 350         // slight exapansion
 351         assertThrows(() -> {
 352             ns.tailSet(BigInteger.ONE, true);
 353         },
 354             IllegalArgumentException.class,
 355             description + ": Expansion should not be allowed");
 356 
 357         // much smaller
 358         subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
 359     }
 360 
 361     /**
 362      * Tests the tailSet() method.
 363      */
 364     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 365     public void testTailSet(String description, NavigableSet navigableSet) {
 366         assertThrows(() -> {
 367             navigableSet.tailSet(null);
 368         },
 369             NullPointerException.class,
 370             description + ": Must throw NullPointerException for null element");
 371 
 372         assertThrows(() -> {
 373             navigableSet.tailSet(new Object());
 374         }, ClassCastException.class);
 375 
 376         NavigableSet ss = navigableSet.tailSet("1", true);
 377 
 378         assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
 379     }
 380 
 381     /**
 382      * Tests that the array has a size of 0.
 383      */
 384     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
 385     public void testToArray(String description, NavigableSet<?> navigableSet) {
 386         Object[] emptyNavigableSetArray = navigableSet.toArray();
 387 
 388         assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
 389 
 390         emptyNavigableSetArray = new Object[20];
 391 
 392         Object[] result = navigableSet.toArray(emptyNavigableSetArray);
 393 
 394         assertSame(emptyNavigableSetArray, result);
 395 
 396         assertTrue(result[0] == null);
 397     }
 398 
 399     @DataProvider(name = "NavigableSet<?>", parallel = true)
 400     public static Iterator<Object[]> navigableSetsProvider() {
 401         return makeNavigableSets().iterator();
 402     }
 403 
 404     public static Collection<Object[]> makeNavigableSets() {
 405         return Arrays.asList(
 406             new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
 407             new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
 408             new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
 409             new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
 410             new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
 411             new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
 412         );
 413     }
 414 }