1 |
|
/* |
2 |
< |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
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 |
27 |
|
* @summary Unit test for Collections.emptyNavigableSet |
28 |
|
* @run testng EmptyNavigableSet |
29 |
|
*/ |
30 |
+ |
|
31 |
+ |
import org.testng.Assert; |
32 |
+ |
import org.testng.Assert.ThrowingRunnable; |
33 |
+ |
import org.testng.annotations.DataProvider; |
34 |
+ |
import org.testng.annotations.Test; |
35 |
+ |
|
36 |
|
import java.math.BigInteger; |
37 |
|
import java.util.Arrays; |
38 |
|
import java.util.Collection; |
39 |
|
import java.util.Collections; |
40 |
|
import java.util.Comparator; |
41 |
|
import java.util.Iterator; |
36 |
– |
import java.util.NoSuchElementException; |
42 |
|
import java.util.NavigableSet; |
43 |
+ |
import java.util.NoSuchElementException; |
44 |
|
import java.util.SortedSet; |
45 |
|
import java.util.TreeSet; |
40 |
– |
import org.testng.annotations.Test; |
41 |
– |
import org.testng.annotations.DataProvider; |
46 |
|
|
43 |
– |
import static org.testng.Assert.fail; |
47 |
|
import static org.testng.Assert.assertFalse; |
48 |
|
import static org.testng.Assert.assertSame; |
49 |
|
import static org.testng.Assert.assertTrue; |
51 |
|
public class EmptyNavigableSet { |
52 |
|
|
53 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected) { |
54 |
< |
assertInstance(expected.isInstance(actual), null); |
54 |
> |
assertInstance(actual, expected, null); |
55 |
|
} |
56 |
|
|
57 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { |
70 |
|
((null != message) ? message : "") + " Not empty. "); |
71 |
|
} |
72 |
|
|
73 |
< |
public interface Thrower<T extends Throwable> { |
73 |
> |
private <T extends Throwable> void assertThrows(Class<T> throwableClass, |
74 |
> |
ThrowingRunnable runnable, |
75 |
> |
String message) { |
76 |
> |
try { |
77 |
> |
Assert.assertThrows(throwableClass, runnable); |
78 |
> |
} catch (AssertionError e) { |
79 |
> |
throw new AssertionError(String.format("%s%n%s", |
80 |
> |
((null != message) ? message : ""), e.getMessage()), e); |
81 |
> |
} |
82 |
> |
} |
83 |
|
|
84 |
< |
public void run() throws T; |
84 |
> |
private void assertThrowsCCE(ThrowingRunnable r, String s) { |
85 |
> |
assertThrows(ClassCastException.class, r, s); |
86 |
|
} |
87 |
|
|
88 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
89 |
< |
assertThrows(thrower, throwable, null); |
88 |
> |
private void assertThrowsNPE(ThrowingRunnable r, String s) { |
89 |
> |
assertThrows(NullPointerException.class, r, s); |
90 |
|
} |
91 |
|
|
92 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
93 |
< |
Throwable result; |
94 |
< |
try { |
82 |
< |
thrower.run(); |
83 |
< |
fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
84 |
< |
return; |
85 |
< |
} catch (Throwable caught) { |
86 |
< |
result = caught; |
87 |
< |
} |
92 |
> |
private void assertThrowsIAE(ThrowingRunnable r, String s) { |
93 |
> |
assertThrows(IllegalArgumentException.class, r, s); |
94 |
> |
} |
95 |
|
|
96 |
< |
assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
96 |
> |
private void assertThrowsNSEE(ThrowingRunnable r, String s) { |
97 |
> |
assertThrows(NoSuchElementException.class, r, s); |
98 |
|
} |
99 |
|
|
100 |
|
public static final boolean isDescending(SortedSet<?> set) { |
131 |
|
*/ |
132 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
133 |
|
public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) { |
134 |
< |
assertThrows(() -> { |
135 |
< |
navigableSet.contains(new Object()); |
128 |
< |
}, |
129 |
< |
ClassCastException.class, |
134 |
> |
assertThrowsCCE( |
135 |
> |
() -> navigableSet.contains(new Object()), |
136 |
|
description + ": Comparable should be required"); |
137 |
|
} |
138 |
|
|
182 |
|
*/ |
183 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
184 |
|
public void testFirst(String description, NavigableSet<?> navigableSet) { |
185 |
< |
assertThrows(() -> { |
180 |
< |
navigableSet.first(); |
181 |
< |
}, NoSuchElementException.class, description); |
185 |
> |
assertThrowsNSEE(navigableSet::first, description); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
190 |
|
*/ |
191 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
192 |
|
public void testHeadSet(String description, NavigableSet navigableSet) { |
193 |
< |
assertThrows( |
193 |
> |
assertThrowsNPE( |
194 |
|
() -> { NavigableSet ns = navigableSet.headSet(null, false); }, |
191 |
– |
NullPointerException.class, |
195 |
|
description + ": Must throw NullPointerException for null element"); |
196 |
|
|
197 |
< |
assertThrows( |
197 |
> |
assertThrowsCCE( |
198 |
|
() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); }, |
196 |
– |
ClassCastException.class, |
199 |
|
description + ": Must throw ClassCastException for non-Comparable element"); |
200 |
|
|
201 |
|
NavigableSet ns = navigableSet.headSet("1", false); |
208 |
|
*/ |
209 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
210 |
|
public void testLast(String description, NavigableSet<?> navigableSet) { |
211 |
< |
assertThrows(() -> { |
210 |
< |
navigableSet.last(); |
211 |
< |
}, NoSuchElementException.class, description); |
211 |
> |
assertThrowsNSEE(navigableSet::last, description); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
224 |
|
*/ |
225 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
226 |
|
public void testSubSet(String description, NavigableSet navigableSet) { |
227 |
< |
assertThrows( |
227 |
> |
assertThrowsNPE( |
228 |
|
() -> { |
229 |
|
SortedSet ss = navigableSet.subSet(null, BigInteger.TEN); |
230 |
|
}, |
231 |
– |
NullPointerException.class, |
231 |
|
description + ": Must throw NullPointerException for null element"); |
232 |
|
|
233 |
< |
assertThrows( |
233 |
> |
assertThrowsNPE( |
234 |
|
() -> { |
235 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null); |
236 |
|
}, |
238 |
– |
NullPointerException.class, |
237 |
|
description + ": Must throw NullPointerException for null element"); |
238 |
|
|
239 |
< |
assertThrows( |
239 |
> |
assertThrowsNPE( |
240 |
|
() -> { |
241 |
|
SortedSet ss = navigableSet.subSet(null, null); |
242 |
|
}, |
245 |
– |
NullPointerException.class, |
243 |
|
description + ": Must throw NullPointerException for null element"); |
244 |
|
|
245 |
|
Object obj1 = new Object(); |
246 |
|
Object obj2 = new Object(); |
247 |
|
|
248 |
< |
assertThrows( |
248 |
> |
assertThrowsCCE( |
249 |
|
() -> { |
250 |
|
SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN); |
251 |
|
}, |
252 |
< |
ClassCastException.class, description |
256 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
252 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
253 |
|
|
254 |
< |
assertThrows( |
254 |
> |
assertThrowsCCE( |
255 |
|
() -> { |
256 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2); |
257 |
|
}, |
258 |
< |
ClassCastException.class, description |
263 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
258 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
259 |
|
|
260 |
< |
assertThrows( |
260 |
> |
assertThrowsCCE( |
261 |
|
() -> { |
262 |
|
SortedSet ss = navigableSet.subSet(obj1, obj2); |
263 |
|
}, |
264 |
< |
ClassCastException.class, description |
270 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
264 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
265 |
|
|
266 |
|
// minimal range |
267 |
|
navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false); |
272 |
|
Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; |
273 |
|
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
274 |
|
|
275 |
< |
assertThrows( |
276 |
< |
() -> { |
277 |
< |
navigableSet.subSet(last, true, first, false); |
284 |
< |
}, |
285 |
< |
IllegalArgumentException.class, description |
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); |
293 |
|
// slightly smaller |
294 |
|
NavigableSet ns = subSet.subSet(first, false, last, false); |
295 |
|
// slight expansion |
296 |
< |
assertThrows(() -> { |
297 |
< |
ns.subSet(first, true, last, true); |
306 |
< |
}, |
307 |
< |
IllegalArgumentException.class, |
296 |
> |
assertThrowsIAE( |
297 |
> |
() -> ns.subSet(first, true, last, true), |
298 |
|
description + ": Expansion should not be allowed"); |
299 |
|
|
300 |
|
// much smaller |
312 |
|
NavigableSet ns = subSet.headSet(BigInteger.ONE, false); |
313 |
|
|
314 |
|
// slight expansion |
315 |
< |
assertThrows(() -> { |
316 |
< |
ns.headSet(BigInteger.ONE, true); |
327 |
< |
}, |
328 |
< |
IllegalArgumentException.class, |
315 |
> |
assertThrowsIAE( |
316 |
> |
() -> ns.headSet(BigInteger.ONE, true), |
317 |
|
description + ": Expansion should not be allowed"); |
318 |
|
|
319 |
|
// much smaller |
331 |
|
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); |
332 |
|
|
333 |
|
// slight expansion |
334 |
< |
assertThrows(() -> { |
335 |
< |
ns.tailSet(BigInteger.ONE, true); |
348 |
< |
}, |
349 |
< |
IllegalArgumentException.class, |
334 |
> |
assertThrowsIAE( |
335 |
> |
() -> ns.tailSet(BigInteger.ONE, true), |
336 |
|
description + ": Expansion should not be allowed"); |
337 |
|
|
338 |
|
// much smaller |
344 |
|
*/ |
345 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
346 |
|
public void testTailSet(String description, NavigableSet navigableSet) { |
347 |
< |
assertThrows(() -> { |
348 |
< |
navigableSet.tailSet(null); |
363 |
< |
}, |
364 |
< |
NullPointerException.class, |
347 |
> |
assertThrowsNPE( |
348 |
> |
() -> navigableSet.tailSet(null), |
349 |
|
description + ": Must throw NullPointerException for null element"); |
350 |
|
|
351 |
< |
assertThrows(() -> { |
352 |
< |
navigableSet.tailSet(new Object()); |
353 |
< |
}, ClassCastException.class); |
351 |
> |
assertThrowsCCE( |
352 |
> |
() -> navigableSet.tailSet(new Object()), |
353 |
> |
description); |
354 |
|
|
355 |
|
NavigableSet ss = navigableSet.tailSet("1", true); |
356 |
|
|