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; |
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(() -> { |
185 |
> |
assertThrowsNSEE(() -> { |
186 |
|
navigableSet.first(); |
187 |
< |
}, NoSuchElementException.class, description); |
187 |
> |
}, description); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
192 |
|
*/ |
193 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
194 |
|
public void testHeadSet(String description, NavigableSet navigableSet) { |
195 |
< |
assertThrows( |
195 |
> |
assertThrowsNPE( |
196 |
|
() -> { NavigableSet ns = navigableSet.headSet(null, false); }, |
191 |
– |
NullPointerException.class, |
197 |
|
description + ": Must throw NullPointerException for null element"); |
198 |
|
|
199 |
< |
assertThrows( |
199 |
> |
assertThrowsCCE( |
200 |
|
() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); }, |
196 |
– |
ClassCastException.class, |
201 |
|
description + ": Must throw ClassCastException for non-Comparable element"); |
202 |
|
|
203 |
|
NavigableSet ns = navigableSet.headSet("1", false); |
210 |
|
*/ |
211 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
212 |
|
public void testLast(String description, NavigableSet<?> navigableSet) { |
213 |
< |
assertThrows(() -> { |
213 |
> |
assertThrowsNSEE(() -> { |
214 |
|
navigableSet.last(); |
215 |
< |
}, NoSuchElementException.class, description); |
215 |
> |
}, description); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
228 |
|
*/ |
229 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
230 |
|
public void testSubSet(String description, NavigableSet navigableSet) { |
231 |
< |
assertThrows( |
231 |
> |
assertThrowsNPE( |
232 |
|
() -> { |
233 |
|
SortedSet ss = navigableSet.subSet(null, BigInteger.TEN); |
234 |
|
}, |
231 |
– |
NullPointerException.class, |
235 |
|
description + ": Must throw NullPointerException for null element"); |
236 |
|
|
237 |
< |
assertThrows( |
237 |
> |
assertThrowsNPE( |
238 |
|
() -> { |
239 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null); |
240 |
|
}, |
238 |
– |
NullPointerException.class, |
241 |
|
description + ": Must throw NullPointerException for null element"); |
242 |
|
|
243 |
< |
assertThrows( |
243 |
> |
assertThrowsNPE( |
244 |
|
() -> { |
245 |
|
SortedSet ss = navigableSet.subSet(null, null); |
246 |
|
}, |
245 |
– |
NullPointerException.class, |
247 |
|
description + ": Must throw NullPointerException for null element"); |
248 |
|
|
249 |
|
Object obj1 = new Object(); |
250 |
|
Object obj2 = new Object(); |
251 |
|
|
252 |
< |
assertThrows( |
252 |
> |
assertThrowsCCE( |
253 |
|
() -> { |
254 |
|
SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN); |
255 |
|
}, |
256 |
< |
ClassCastException.class, description |
256 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
256 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
257 |
|
|
258 |
< |
assertThrows( |
258 |
> |
assertThrowsCCE( |
259 |
|
() -> { |
260 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2); |
261 |
|
}, |
262 |
< |
ClassCastException.class, description |
263 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
262 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
263 |
|
|
264 |
< |
assertThrows( |
264 |
> |
assertThrowsCCE( |
265 |
|
() -> { |
266 |
|
SortedSet ss = navigableSet.subSet(obj1, obj2); |
267 |
|
}, |
268 |
< |
ClassCastException.class, description |
270 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
268 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
269 |
|
|
270 |
|
// minimal range |
271 |
|
navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false); |
276 |
|
Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; |
277 |
|
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
278 |
|
|
279 |
< |
assertThrows( |
279 |
> |
assertThrowsIAE( |
280 |
|
() -> { |
281 |
|
navigableSet.subSet(last, true, first, false); |
282 |
|
}, |
283 |
< |
IllegalArgumentException.class, description |
283 |
> |
description |
284 |
|
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
285 |
|
|
286 |
|
navigableSet.subSet(first, true, last, false); |
299 |
|
// slightly smaller |
300 |
|
NavigableSet ns = subSet.subSet(first, false, last, false); |
301 |
|
// slight expansion |
302 |
< |
assertThrows(() -> { |
302 |
> |
assertThrowsIAE(() -> { |
303 |
|
ns.subSet(first, true, last, true); |
304 |
|
}, |
307 |
– |
IllegalArgumentException.class, |
305 |
|
description + ": Expansion should not be allowed"); |
306 |
|
|
307 |
|
// much smaller |
319 |
|
NavigableSet ns = subSet.headSet(BigInteger.ONE, false); |
320 |
|
|
321 |
|
// slight expansion |
322 |
< |
assertThrows(() -> { |
322 |
> |
assertThrowsIAE(() -> { |
323 |
|
ns.headSet(BigInteger.ONE, true); |
324 |
|
}, |
328 |
– |
IllegalArgumentException.class, |
325 |
|
description + ": Expansion should not be allowed"); |
326 |
|
|
327 |
|
// much smaller |
339 |
|
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); |
340 |
|
|
341 |
|
// slight expansion |
342 |
< |
assertThrows(() -> { |
342 |
> |
assertThrowsIAE(() -> { |
343 |
|
ns.tailSet(BigInteger.ONE, true); |
344 |
|
}, |
349 |
– |
IllegalArgumentException.class, |
345 |
|
description + ": Expansion should not be allowed"); |
346 |
|
|
347 |
|
// much smaller |
353 |
|
*/ |
354 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
355 |
|
public void testTailSet(String description, NavigableSet navigableSet) { |
356 |
< |
assertThrows(() -> { |
356 |
> |
assertThrowsNPE(() -> { |
357 |
|
navigableSet.tailSet(null); |
358 |
|
}, |
364 |
– |
NullPointerException.class, |
359 |
|
description + ": Must throw NullPointerException for null element"); |
360 |
|
|
361 |
< |
assertThrows(() -> { |
361 |
> |
assertThrowsCCE(() -> { |
362 |
|
navigableSet.tailSet(new Object()); |
363 |
< |
}, ClassCastException.class); |
363 |
> |
}, description); |
364 |
|
|
365 |
|
NavigableSet ss = navigableSet.tailSet("1", true); |
366 |
|
|