1 /* 2 * Copyright (c) 2011, 2017, 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 31 package test.java.util.Collections; 32 33 import org.testng.Assert; 34 import org.testng.Assert.ThrowingRunnable; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import java.math.BigInteger; 39 import java.util.Arrays; 40 import java.util.Collection; 41 import java.util.Collections; 42 import java.util.Comparator; 43 import java.util.Iterator; 44 import java.util.NavigableSet; 45 import java.util.NoSuchElementException; 46 import java.util.SortedSet; 47 import java.util.TreeSet; 48 49 import static org.testng.Assert.assertFalse; 50 import static org.testng.Assert.assertNull; 51 import static org.testng.Assert.assertSame; 52 import static org.testng.Assert.assertTrue; 53 54 public class EmptyNavigableSet { 55 assertInstance(T actual, Class<? extends T> expected)56 public static <T> void assertInstance(T actual, Class<? extends T> expected) { 57 assertInstance(actual, expected, null); 58 } 59 assertInstance(T actual, Class<? extends T> expected, String message)60 public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { 61 assertTrue(expected.isInstance(actual), ((null != message) ? message : "") 62 + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". "); 63 } 64 assertEmptyNavigableSet(Object obj)65 public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) { 66 assertInstance(obj, NavigableSet.class); 67 assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0)); 68 } 69 assertEmptyNavigableSet(Object obj, String message)70 public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) { 71 assertInstance(obj, NavigableSet.class, message); 72 assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0), 73 ((null != message) ? message : "") + " Not empty. "); 74 } 75 assertThrows(Class<T> throwableClass, ThrowingRunnable runnable, String message)76 private <T extends Throwable> void assertThrows(Class<T> throwableClass, 77 ThrowingRunnable runnable, 78 String message) { 79 try { 80 Assert.assertThrows(throwableClass, runnable); 81 } catch (AssertionError e) { 82 throw new AssertionError(String.format("%s%n%s", 83 ((null != message) ? message : ""), e.getMessage()), e); 84 } 85 } 86 assertThrowsCCE(ThrowingRunnable r, String s)87 private void assertThrowsCCE(ThrowingRunnable r, String s) { 88 assertThrows(ClassCastException.class, r, s); 89 } 90 assertThrowsNPE(ThrowingRunnable r, String s)91 private void assertThrowsNPE(ThrowingRunnable r, String s) { 92 assertThrows(NullPointerException.class, r, s); 93 } 94 assertThrowsIAE(ThrowingRunnable r, String s)95 private void assertThrowsIAE(ThrowingRunnable r, String s) { 96 assertThrows(IllegalArgumentException.class, r, s); 97 } 98 assertThrowsNSEE(ThrowingRunnable r, String s)99 private void assertThrowsNSEE(ThrowingRunnable r, String s) { 100 assertThrows(NoSuchElementException.class, r, s); 101 } 102 isDescending(SortedSet<?> set)103 public static final boolean isDescending(SortedSet<?> set) { 104 if (null == set.comparator()) { 105 // natural order 106 return false; 107 } 108 109 if (Collections.reverseOrder() == set.comparator()) { 110 // reverse natural order. 111 return true; 112 } 113 114 if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) { 115 // it's a Collections.reverseOrder(Comparator). 116 return true; 117 } 118 119 throw new IllegalStateException("can't determine ordering for " + set); 120 } 121 122 /** 123 * Tests that the comparator is {@code null}. 124 */ 125 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testComparatorIsNull(String description, NavigableSet<?> navigableSet)126 public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) { 127 Comparator comparator = navigableSet.comparator(); 128 129 assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null."); 130 } 131 132 /** 133 * Tests that contains requires Comparable 134 */ 135 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testContainsRequiresComparable(String description, NavigableSet<?> navigableSet)136 public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) { 137 assertThrowsCCE( 138 () -> navigableSet.contains(new Object()), 139 description + ": Comparable should be required"); 140 } 141 142 /** 143 * Tests that the contains method returns {@code false}. 144 */ 145 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testContains(String description, NavigableSet<?> navigableSet)146 public void testContains(String description, NavigableSet<?> navigableSet) { 147 assertFalse(navigableSet.contains(new Integer(1)), 148 description + ": Should not contain any elements."); 149 } 150 151 /** 152 * Tests that the containsAll method returns {@code false}. 153 */ 154 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testContainsAll(String description, NavigableSet<?> navigableSet)155 public void testContainsAll(String description, NavigableSet<?> navigableSet) { 156 TreeSet treeSet = new TreeSet(); 157 treeSet.add("1"); 158 treeSet.add("2"); 159 treeSet.add("3"); 160 161 assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements."); 162 } 163 164 /** 165 * Tests that the iterator is empty. 166 */ 167 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testEmptyIterator(String description, NavigableSet<?> navigableSet)168 public void testEmptyIterator(String description, NavigableSet<?> navigableSet) { 169 assertFalse(navigableSet.iterator().hasNext(), "The iterator is not empty."); 170 } 171 172 /** 173 * Tests that the set is empty. 174 */ 175 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testIsEmpty(String description, NavigableSet<?> navigableSet)176 public void testIsEmpty(String description, NavigableSet<?> navigableSet) { 177 assertTrue(navigableSet.isEmpty(), "The set is not empty."); 178 } 179 180 /** 181 * Tests that the first() method throws NoSuchElementException 182 */ 183 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testFirst(String description, NavigableSet<?> navigableSet)184 public void testFirst(String description, NavigableSet<?> navigableSet) { 185 assertThrowsNSEE(navigableSet::first, description); 186 } 187 188 /** 189 * Tests the headSet() method. 190 */ 191 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testHeadSet(String description, NavigableSet navigableSet)192 public void testHeadSet(String description, NavigableSet navigableSet) { 193 assertThrowsNPE( 194 () -> { NavigableSet ns = navigableSet.headSet(null, false); }, 195 description + ": Must throw NullPointerException for null element"); 196 197 assertThrowsCCE( 198 () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); }, 199 description + ": Must throw ClassCastException for non-Comparable element"); 200 201 NavigableSet ns = navigableSet.headSet("1", false); 202 203 assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set."); 204 } 205 206 /** 207 * Tests that the last() method throws NoSuchElementException 208 */ 209 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testLast(String description, NavigableSet<?> navigableSet)210 public void testLast(String description, NavigableSet<?> navigableSet) { 211 assertThrowsNSEE(navigableSet::last, description); 212 } 213 214 /** 215 * Tests that the size is 0. 216 */ 217 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testSizeIsZero(String description, NavigableSet<?> navigableSet)218 public void testSizeIsZero(String description, NavigableSet<?> navigableSet) { 219 assertTrue(0 == navigableSet.size(), "The size of the set is not 0."); 220 } 221 222 /** 223 * Tests the subSet() method. 224 */ 225 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testSubSet(String description, NavigableSet navigableSet)226 public void testSubSet(String description, NavigableSet navigableSet) { 227 assertThrowsNPE( 228 () -> { 229 SortedSet ss = navigableSet.subSet(null, BigInteger.TEN); 230 }, 231 description + ": Must throw NullPointerException for null element"); 232 233 assertThrowsNPE( 234 () -> { 235 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null); 236 }, 237 description + ": Must throw NullPointerException for null element"); 238 239 assertThrowsNPE( 240 () -> { 241 SortedSet ss = navigableSet.subSet(null, null); 242 }, 243 description + ": Must throw NullPointerException for null element"); 244 245 Object obj1 = new Object(); 246 Object obj2 = new Object(); 247 248 assertThrowsCCE( 249 () -> { 250 SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN); 251 }, 252 description + ": Must throw ClassCastException for parameter which is not Comparable."); 253 254 assertThrowsCCE( 255 () -> { 256 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2); 257 }, 258 description + ": Must throw ClassCastException for parameter which is not Comparable."); 259 260 assertThrowsCCE( 261 () -> { 262 SortedSet ss = navigableSet.subSet(obj1, obj2); 263 }, 264 description + ": Must throw ClassCastException for parameter which is not Comparable."); 265 266 // minimal range 267 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false); 268 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true); 269 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false); 270 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true); 271 272 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; 273 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 274 275 assertThrowsIAE( 276 () -> navigableSet.subSet(last, true, first, false), 277 description 278 + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); 279 280 navigableSet.subSet(first, true, last, false); 281 } 282 283 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testSubSetRanges(String description, NavigableSet navigableSet)284 public void testSubSetRanges(String description, NavigableSet navigableSet) { 285 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; 286 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 287 288 NavigableSet subSet = navigableSet.subSet(first, true, last, true); 289 290 // same subset 291 subSet.subSet(first, true, last, true); 292 293 // slightly smaller 294 NavigableSet ns = subSet.subSet(first, false, last, false); 295 // slight expansion 296 assertThrowsIAE( 297 () -> ns.subSet(first, true, last, true), 298 description + ": Expansion should not be allowed"); 299 300 // much smaller 301 subSet.subSet(first, false, BigInteger.ONE, false); 302 } 303 304 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testheadSetRanges(String description, NavigableSet navigableSet)305 public void testheadSetRanges(String description, NavigableSet navigableSet) { 306 NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true); 307 308 // same subset 309 subSet.headSet(BigInteger.ONE, true); 310 311 // slightly smaller 312 NavigableSet ns = subSet.headSet(BigInteger.ONE, false); 313 314 // slight expansion 315 assertThrowsIAE( 316 () -> ns.headSet(BigInteger.ONE, true), 317 description + ": Expansion should not be allowed"); 318 319 // much smaller 320 subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true); 321 } 322 323 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testTailSetRanges(String description, NavigableSet navigableSet)324 public void testTailSetRanges(String description, NavigableSet navigableSet) { 325 NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true); 326 327 // same subset 328 subSet.tailSet(BigInteger.ONE, true); 329 330 // slightly smaller 331 NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); 332 333 // slight expansion 334 assertThrowsIAE( 335 () -> ns.tailSet(BigInteger.ONE, true), 336 description + ": Expansion should not be allowed"); 337 338 // much smaller 339 subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false); 340 } 341 342 /** 343 * Tests the tailSet() method. 344 */ 345 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testTailSet(String description, NavigableSet navigableSet)346 public void testTailSet(String description, NavigableSet navigableSet) { 347 assertThrowsNPE( 348 () -> navigableSet.tailSet(null), 349 description + ": Must throw NullPointerException for null element"); 350 351 assertThrowsCCE( 352 () -> navigableSet.tailSet(new Object()), 353 description); 354 355 NavigableSet ss = navigableSet.tailSet("1", true); 356 357 assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set."); 358 } 359 360 /** 361 * Tests that the array has a size of 0. 362 */ 363 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) testToArray(String description, NavigableSet<?> navigableSet)364 public void testToArray(String description, NavigableSet<?> navigableSet) { 365 Object[] emptyNavigableSetArray = navigableSet.toArray(); 366 367 assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array."); 368 369 emptyNavigableSetArray = new Object[20]; 370 371 Object[] result = navigableSet.toArray(emptyNavigableSetArray); 372 373 assertSame(emptyNavigableSetArray, result); 374 375 assertNull(result[0]); 376 } 377 378 @DataProvider(name = "NavigableSet<?>", parallel = true) navigableSetsProvider()379 public static Iterator<Object[]> navigableSetsProvider() { 380 return makeNavigableSets().iterator(); 381 } 382 makeNavigableSets()383 public static Collection<Object[]> makeNavigableSets() { 384 return Arrays.asList( 385 new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())}, 386 new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())}, 387 new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())}, 388 new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()}, 389 new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()}, 390 new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()} 391 ); 392 } 393 } 394