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(() -> { |
134 |
< |
navigableSet.contains(new Object()); |
135 |
< |
}, |
129 |
< |
ClassCastException.class, |
130 |
< |
description + ": Compareable should be required"); |
133 |
> |
assertThrowsCCE( |
134 |
> |
() -> navigableSet.contains(new Object()), |
135 |
> |
description + ": Comparable should be required"); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
181 |
|
*/ |
182 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
183 |
|
public void testFirst(String description, NavigableSet<?> navigableSet) { |
184 |
< |
assertThrows(() -> { |
180 |
< |
navigableSet.first(); |
181 |
< |
}, NoSuchElementException.class, description); |
184 |
> |
assertThrowsNSEE(navigableSet::first, description); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
189 |
|
*/ |
190 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
191 |
|
public void testHeadSet(String description, NavigableSet navigableSet) { |
192 |
< |
assertThrows( |
192 |
> |
assertThrowsNPE( |
193 |
|
() -> { NavigableSet ns = navigableSet.headSet(null, false); }, |
191 |
– |
NullPointerException.class, |
194 |
|
description + ": Must throw NullPointerException for null element"); |
195 |
|
|
196 |
< |
assertThrows( |
196 |
> |
assertThrowsCCE( |
197 |
|
() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); }, |
196 |
– |
ClassCastException.class, |
198 |
|
description + ": Must throw ClassCastException for non-Comparable element"); |
199 |
|
|
200 |
|
NavigableSet ns = navigableSet.headSet("1", false); |
207 |
|
*/ |
208 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
209 |
|
public void testLast(String description, NavigableSet<?> navigableSet) { |
210 |
< |
assertThrows(() -> { |
210 |
< |
navigableSet.last(); |
211 |
< |
}, NoSuchElementException.class, description); |
210 |
> |
assertThrowsNSEE(navigableSet::last, description); |
211 |
|
} |
212 |
|
|
213 |
|
/** |
223 |
|
*/ |
224 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
225 |
|
public void testSubSet(String description, NavigableSet navigableSet) { |
226 |
< |
assertThrows( |
226 |
> |
assertThrowsNPE( |
227 |
|
() -> { |
228 |
|
SortedSet ss = navigableSet.subSet(null, BigInteger.TEN); |
229 |
|
}, |
231 |
– |
NullPointerException.class, |
230 |
|
description + ": Must throw NullPointerException for null element"); |
231 |
|
|
232 |
< |
assertThrows( |
232 |
> |
assertThrowsNPE( |
233 |
|
() -> { |
234 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null); |
235 |
|
}, |
238 |
– |
NullPointerException.class, |
236 |
|
description + ": Must throw NullPointerException for null element"); |
237 |
|
|
238 |
< |
assertThrows( |
238 |
> |
assertThrowsNPE( |
239 |
|
() -> { |
240 |
|
SortedSet ss = navigableSet.subSet(null, null); |
241 |
|
}, |
245 |
– |
NullPointerException.class, |
242 |
|
description + ": Must throw NullPointerException for null element"); |
243 |
|
|
244 |
|
Object obj1 = new Object(); |
245 |
|
Object obj2 = new Object(); |
246 |
|
|
247 |
< |
assertThrows( |
247 |
> |
assertThrowsCCE( |
248 |
|
() -> { |
249 |
|
SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN); |
250 |
|
}, |
251 |
< |
ClassCastException.class, description |
256 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
251 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
252 |
|
|
253 |
< |
assertThrows( |
253 |
> |
assertThrowsCCE( |
254 |
|
() -> { |
255 |
|
SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2); |
256 |
|
}, |
257 |
< |
ClassCastException.class, description |
263 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
257 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
258 |
|
|
259 |
< |
assertThrows( |
259 |
> |
assertThrowsCCE( |
260 |
|
() -> { |
261 |
|
SortedSet ss = navigableSet.subSet(obj1, obj2); |
262 |
|
}, |
263 |
< |
ClassCastException.class, description |
270 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
263 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
264 |
|
|
265 |
|
// minimal range |
266 |
|
navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false); |
271 |
|
Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; |
272 |
|
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
273 |
|
|
274 |
< |
assertThrows( |
275 |
< |
() -> { |
276 |
< |
navigableSet.subSet(last, true, first, false); |
284 |
< |
}, |
285 |
< |
IllegalArgumentException.class, description |
274 |
> |
assertThrowsIAE( |
275 |
> |
() -> navigableSet.subSet(last, true, first, false), |
276 |
> |
description |
277 |
|
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
278 |
|
|
279 |
|
navigableSet.subSet(first, true, last, false); |
292 |
|
// slightly smaller |
293 |
|
NavigableSet ns = subSet.subSet(first, false, last, false); |
294 |
|
// slight expansion |
295 |
< |
assertThrows(() -> { |
296 |
< |
ns.subSet(first, true, last, true); |
306 |
< |
}, |
307 |
< |
IllegalArgumentException.class, |
295 |
> |
assertThrowsIAE( |
296 |
> |
() -> ns.subSet(first, true, last, true), |
297 |
|
description + ": Expansion should not be allowed"); |
298 |
|
|
299 |
|
// much smaller |
311 |
|
NavigableSet ns = subSet.headSet(BigInteger.ONE, false); |
312 |
|
|
313 |
|
// slight expansion |
314 |
< |
assertThrows(() -> { |
315 |
< |
ns.headSet(BigInteger.ONE, true); |
327 |
< |
}, |
328 |
< |
IllegalArgumentException.class, |
314 |
> |
assertThrowsIAE( |
315 |
> |
() -> ns.headSet(BigInteger.ONE, true), |
316 |
|
description + ": Expansion should not be allowed"); |
317 |
|
|
318 |
|
// much smaller |
330 |
|
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); |
331 |
|
|
332 |
|
// slight expansion |
333 |
< |
assertThrows(() -> { |
334 |
< |
ns.tailSet(BigInteger.ONE, true); |
348 |
< |
}, |
349 |
< |
IllegalArgumentException.class, |
333 |
> |
assertThrowsIAE( |
334 |
> |
() -> ns.tailSet(BigInteger.ONE, true), |
335 |
|
description + ": Expansion should not be allowed"); |
336 |
|
|
337 |
|
// much smaller |
343 |
|
*/ |
344 |
|
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class) |
345 |
|
public void testTailSet(String description, NavigableSet navigableSet) { |
346 |
< |
assertThrows(() -> { |
347 |
< |
navigableSet.tailSet(null); |
363 |
< |
}, |
364 |
< |
NullPointerException.class, |
346 |
> |
assertThrowsNPE( |
347 |
> |
() -> navigableSet.tailSet(null), |
348 |
|
description + ": Must throw NullPointerException for null element"); |
349 |
|
|
350 |
< |
assertThrows(() -> { |
351 |
< |
navigableSet.tailSet(new Object()); |
352 |
< |
}, ClassCastException.class); |
350 |
> |
assertThrowsCCE( |
351 |
> |
() -> navigableSet.tailSet(new Object()), |
352 |
> |
description); |
353 |
|
|
354 |
|
NavigableSet ss = navigableSet.tailSet("1", true); |
355 |
|
|