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 |
37 |
|
import java.util.NavigableSet; |
38 |
|
import java.util.SortedSet; |
39 |
|
import java.util.TreeSet; |
40 |
+ |
|
41 |
+ |
import org.testng.Assert; |
42 |
+ |
import org.testng.Assert.ThrowingRunnable; |
43 |
|
import org.testng.annotations.Test; |
44 |
|
import org.testng.annotations.DataProvider; |
45 |
|
|
43 |
– |
import static org.testng.Assert.fail; |
46 |
|
import static org.testng.Assert.assertFalse; |
47 |
|
import static org.testng.Assert.assertSame; |
48 |
|
import static org.testng.Assert.assertTrue; |
50 |
|
public class EmptyNavigableSet { |
51 |
|
|
52 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected) { |
53 |
< |
assertInstance(expected.isInstance(actual), null); |
53 |
> |
assertInstance(actual, expected, null); |
54 |
|
} |
55 |
|
|
56 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { |
69 |
|
((null != message) ? message : "") + " Not empty. "); |
70 |
|
} |
71 |
|
|
72 |
< |
public interface Thrower<T extends Throwable> { |
72 |
> |
private <T extends Throwable> void assertThrows(Class<T> throwableClass, |
73 |
> |
ThrowingRunnable runnable, |
74 |
> |
String message) { |
75 |
> |
try { |
76 |
> |
Assert.assertThrows(throwableClass, runnable); |
77 |
> |
} catch (AssertionError e) { |
78 |
> |
throw new AssertionError(String.format("%s%n%s", |
79 |
> |
((null != message) ? message : ""), e.getMessage()), e); |
80 |
> |
} |
81 |
> |
} |
82 |
|
|
83 |
< |
public void run() throws T; |
83 |
> |
private void assertThrowsCCE(ThrowingRunnable r, String s) { |
84 |
> |
assertThrows(ClassCastException.class, r, s); |
85 |
|
} |
86 |
|
|
87 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
88 |
< |
assertThrows(thrower, throwable, null); |
87 |
> |
private void assertThrowsNPE(ThrowingRunnable r, String s) { |
88 |
> |
assertThrows(NullPointerException.class, r, s); |
89 |
|
} |
90 |
|
|
91 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
92 |
< |
Throwable result; |
93 |
< |
try { |
82 |
< |
thrower.run(); |
83 |
< |
fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
84 |
< |
return; |
85 |
< |
} catch (Throwable caught) { |
86 |
< |
result = caught; |
87 |
< |
} |
91 |
> |
private void assertThrowsIAE(ThrowingRunnable r, String s) { |
92 |
> |
assertThrows(IllegalArgumentException.class, r, s); |
93 |
> |
} |
94 |
|
|
95 |
< |
assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
95 |
> |
private void assertThrowsNSEE(ThrowingRunnable r, String s) { |
96 |
> |
assertThrows(NoSuchElementException.class, r, s); |
97 |
|
} |
98 |
|
|
99 |
|
public static final boolean isDescending(SortedSet<?> set) { |
130 |
|
*/ |
131 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
132 |
|
public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) { |
133 |
< |
assertThrows(() -> { |
133 |
> |
assertThrowsCCE(() -> { |
134 |
|
navigableSet.contains(new Object()); |
135 |
|
}, |
136 |
< |
ClassCastException.class, |
130 |
< |
description + ": Comparable should be required"); |
136 |
> |
description + ": Compareable should be required"); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
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( |
275 |
> |
assertThrowsIAE( |
276 |
|
() -> { |
277 |
|
navigableSet.subSet(last, true, first, false); |
278 |
|
}, |
279 |
< |
IllegalArgumentException.class, description |
279 |
> |
description |
280 |
|
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
281 |
|
|
282 |
|
navigableSet.subSet(first, true, last, false); |
295 |
|
// slightly smaller |
296 |
|
NavigableSet ns = subSet.subSet(first, false, last, false); |
297 |
|
// slight expansion |
298 |
< |
assertThrows(() -> { |
298 |
> |
assertThrowsIAE(() -> { |
299 |
|
ns.subSet(first, true, last, true); |
300 |
|
}, |
307 |
– |
IllegalArgumentException.class, |
301 |
|
description + ": Expansion should not be allowed"); |
302 |
|
|
303 |
|
// much smaller |
315 |
|
NavigableSet ns = subSet.headSet(BigInteger.ONE, false); |
316 |
|
|
317 |
|
// slight expansion |
318 |
< |
assertThrows(() -> { |
318 |
> |
assertThrowsIAE(() -> { |
319 |
|
ns.headSet(BigInteger.ONE, true); |
320 |
|
}, |
328 |
– |
IllegalArgumentException.class, |
321 |
|
description + ": Expansion should not be allowed"); |
322 |
|
|
323 |
|
// much smaller |
335 |
|
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); |
336 |
|
|
337 |
|
// slight expansion |
338 |
< |
assertThrows(() -> { |
338 |
> |
assertThrowsIAE(() -> { |
339 |
|
ns.tailSet(BigInteger.ONE, true); |
340 |
|
}, |
349 |
– |
IllegalArgumentException.class, |
341 |
|
description + ": Expansion should not be allowed"); |
342 |
|
|
343 |
|
// much smaller |
349 |
|
*/ |
350 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
351 |
|
public void testTailSet(String description, NavigableSet navigableSet) { |
352 |
< |
assertThrows(() -> { |
352 |
> |
assertThrowsNPE(() -> { |
353 |
|
navigableSet.tailSet(null); |
354 |
|
}, |
364 |
– |
NullPointerException.class, |
355 |
|
description + ": Must throw NullPointerException for null element"); |
356 |
|
|
357 |
< |
assertThrows(() -> { |
357 |
> |
assertThrowsCCE(() -> { |
358 |
|
navigableSet.tailSet(new Object()); |
359 |
< |
}, ClassCastException.class); |
359 |
> |
}, description); |
360 |
|
|
361 |
|
NavigableSet ss = navigableSet.tailSet("1", true); |
362 |
|
|