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 |
33 |
|
import java.util.Collections; |
34 |
|
import java.util.Comparator; |
35 |
|
import java.util.Iterator; |
36 |
– |
import java.util.NoSuchElementException; |
36 |
|
import java.util.NavigableMap; |
37 |
|
import java.util.SortedMap; |
38 |
|
import java.util.TreeMap; |
39 |
+ |
|
40 |
+ |
import org.testng.Assert; |
41 |
+ |
import org.testng.Assert.ThrowingRunnable; |
42 |
|
import org.testng.annotations.Test; |
43 |
|
import org.testng.annotations.DataProvider; |
44 |
|
|
43 |
– |
import static org.testng.Assert.fail; |
44 |
– |
import static org.testng.Assert.assertEquals; |
45 |
|
import static org.testng.Assert.assertTrue; |
46 |
|
import static org.testng.Assert.assertFalse; |
47 |
– |
import static org.testng.Assert.assertSame; |
47 |
|
|
48 |
|
public class EmptyNavigableMap { |
49 |
|
|
50 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected) { |
51 |
< |
assertInstance(expected.isInstance(actual), null); |
51 |
> |
assertInstance(actual, expected, null); |
52 |
|
} |
53 |
|
|
54 |
|
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { |
67 |
|
((null != message) ? message : "") + " Not empty. "); |
68 |
|
} |
69 |
|
|
70 |
< |
public interface Thrower<T extends Throwable> { |
71 |
< |
|
72 |
< |
public void run() throws T; |
70 |
> |
private <T extends Throwable> void assertThrows(Class<T> throwableClass, |
71 |
> |
ThrowingRunnable runnable, |
72 |
> |
String message) { |
73 |
> |
try { |
74 |
> |
Assert.assertThrows(throwableClass, runnable); |
75 |
> |
} catch (AssertionError e) { |
76 |
> |
throw new AssertionError(String.format("%s%n%s", |
77 |
> |
((null != message) ? message : ""), e.getMessage()), e); |
78 |
> |
} |
79 |
|
} |
80 |
|
|
81 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
82 |
< |
assertThrows(thrower, throwable, null); |
81 |
> |
private void assertThrowsCCE(ThrowingRunnable r, String s) { |
82 |
> |
assertThrows(ClassCastException.class, r, s); |
83 |
|
} |
84 |
|
|
85 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
86 |
< |
Throwable result; |
87 |
< |
try { |
83 |
< |
thrower.run(); |
84 |
< |
fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
85 |
< |
return; |
86 |
< |
} catch (Throwable caught) { |
87 |
< |
result = caught; |
88 |
< |
} |
85 |
> |
private void assertThrowsNPE(ThrowingRunnable r, String s) { |
86 |
> |
assertThrows(NullPointerException.class, r, s); |
87 |
> |
} |
88 |
|
|
89 |
< |
assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
89 |
> |
private void assertThrowsIAE(ThrowingRunnable r, String s) { |
90 |
> |
assertThrows(IllegalArgumentException.class, r, s); |
91 |
|
} |
92 |
|
|
93 |
|
public static final boolean isDescending(SortedMap<?,?> set) { |
124 |
|
*/ |
125 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
126 |
|
public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) { |
127 |
< |
assertThrows(() -> { |
127 |
> |
assertThrowsCCE(() -> { |
128 |
|
navigableMap.containsKey(new Object()); |
129 |
|
}, |
130 |
– |
ClassCastException.class, |
130 |
|
description + ": Compareable should be required"); |
131 |
|
} |
132 |
|
|
177 |
|
*/ |
178 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
179 |
|
public void testHeadMap(String description, NavigableMap navigableMap) { |
180 |
< |
assertThrows( |
180 |
> |
assertThrowsNPE( |
181 |
|
() -> { NavigableMap ss = navigableMap.headMap(null, false); }, |
183 |
– |
NullPointerException.class, |
182 |
|
description + ": Must throw NullPointerException for null element"); |
183 |
|
|
184 |
< |
assertThrows( |
184 |
> |
assertThrowsCCE( |
185 |
|
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, |
188 |
– |
ClassCastException.class, |
186 |
|
description + ": Must throw ClassCastException for non-Comparable element"); |
187 |
|
|
188 |
|
NavigableMap ss = navigableMap.headMap("1", false); |
203 |
|
*/ |
204 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
205 |
|
public void testSubMap(String description, NavigableMap navigableMap) { |
206 |
< |
assertThrows( |
206 |
> |
assertThrowsNPE( |
207 |
|
() -> { |
208 |
|
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); |
209 |
|
}, |
213 |
– |
NullPointerException.class, |
210 |
|
description + ": Must throw NullPointerException for null element"); |
211 |
|
|
212 |
< |
assertThrows( |
212 |
> |
assertThrowsNPE( |
213 |
|
() -> { |
214 |
|
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); |
215 |
|
}, |
220 |
– |
NullPointerException.class, |
216 |
|
description + ": Must throw NullPointerException for null element"); |
217 |
|
|
218 |
< |
assertThrows( |
218 |
> |
assertThrowsNPE( |
219 |
|
() -> { |
220 |
|
SortedMap ss = navigableMap.subMap(null, null); |
221 |
|
}, |
227 |
– |
NullPointerException.class, |
222 |
|
description + ": Must throw NullPointerException for null element"); |
223 |
|
|
224 |
|
Object obj1 = new Object(); |
225 |
|
Object obj2 = new Object(); |
226 |
|
|
227 |
< |
assertThrows( |
227 |
> |
assertThrowsCCE( |
228 |
|
() -> { |
229 |
|
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); |
230 |
|
}, |
231 |
< |
ClassCastException.class, description |
238 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
231 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
232 |
|
|
233 |
< |
assertThrows( |
233 |
> |
assertThrowsCCE( |
234 |
|
() -> { |
235 |
|
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); |
236 |
|
}, |
237 |
< |
ClassCastException.class, description |
245 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
237 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
238 |
|
|
239 |
< |
assertThrows( |
239 |
> |
assertThrowsCCE( |
240 |
|
() -> { |
241 |
|
SortedMap ss = navigableMap.subMap(obj1, obj2); |
242 |
|
}, |
243 |
< |
ClassCastException.class, description |
252 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
243 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
244 |
|
|
245 |
|
// minimal range |
246 |
|
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); |
251 |
|
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
252 |
|
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
253 |
|
|
254 |
< |
assertThrows( |
254 |
> |
assertThrowsIAE( |
255 |
|
() -> { |
256 |
|
navigableMap.subMap(last, true, first, false); |
257 |
|
}, |
258 |
< |
IllegalArgumentException.class, description |
268 |
< |
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
258 |
> |
description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
259 |
|
|
260 |
|
navigableMap.subMap(first, true, last, false); |
261 |
|
} |
273 |
|
// slightly smaller |
274 |
|
NavigableMap ns = subMap.subMap(first, false, last, false); |
275 |
|
// slight expansion |
276 |
< |
assertThrows(() -> { |
276 |
> |
assertThrowsIAE(() -> { |
277 |
|
ns.subMap(first, true, last, true); |
278 |
|
}, |
289 |
– |
IllegalArgumentException.class, |
279 |
|
description + ": Expansion should not be allowed"); |
280 |
|
|
281 |
|
// much smaller |
293 |
|
NavigableMap ns = subMap.headMap(BigInteger.ONE, false); |
294 |
|
|
295 |
|
// slight expansion |
296 |
< |
assertThrows(() -> { |
296 |
> |
assertThrowsIAE(() -> { |
297 |
|
ns.headMap(BigInteger.ONE, true); |
298 |
|
}, |
310 |
– |
IllegalArgumentException.class, |
299 |
|
description + ": Expansion should not be allowed"); |
300 |
|
|
301 |
|
// much smaller |
313 |
|
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); |
314 |
|
|
315 |
|
// slight expansion |
316 |
< |
assertThrows(() -> { |
316 |
> |
assertThrowsIAE(() -> { |
317 |
|
ns.tailMap(BigInteger.ONE, true); |
318 |
|
}, |
331 |
– |
IllegalArgumentException.class, |
319 |
|
description + ": Expansion should not be allowed"); |
320 |
|
|
321 |
|
// much smaller |
327 |
|
*/ |
328 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
329 |
|
public void testTailMap(String description, NavigableMap navigableMap) { |
330 |
< |
assertThrows(() -> { |
330 |
> |
assertThrowsNPE(() -> { |
331 |
|
navigableMap.tailMap(null); |
332 |
|
}, |
346 |
– |
NullPointerException.class, |
333 |
|
description + ": Must throw NullPointerException for null element"); |
334 |
|
|
335 |
< |
assertThrows(() -> { |
335 |
> |
assertThrowsCCE(() -> { |
336 |
|
navigableMap.tailMap(new Object()); |
337 |
< |
}, ClassCastException.class); |
337 |
> |
}, |
338 |
> |
description); |
339 |
|
|
340 |
|
NavigableMap ss = navigableMap.tailMap("1", true); |
341 |
|
|