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(() -> { |
128 |
< |
navigableMap.containsKey(new Object()); |
129 |
< |
}, |
130 |
< |
ClassCastException.class, |
131 |
< |
description + ": Compareable should be required"); |
127 |
> |
assertThrowsCCE( |
128 |
> |
() -> navigableMap.containsKey(new Object()), |
129 |
> |
description + ": Comparable should be required"); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
176 |
|
*/ |
177 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
178 |
|
public void testHeadMap(String description, NavigableMap navigableMap) { |
179 |
< |
assertThrows( |
179 |
> |
assertThrowsNPE( |
180 |
|
() -> { NavigableMap ss = navigableMap.headMap(null, false); }, |
183 |
– |
NullPointerException.class, |
181 |
|
description + ": Must throw NullPointerException for null element"); |
182 |
|
|
183 |
< |
assertThrows( |
183 |
> |
assertThrowsCCE( |
184 |
|
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, |
188 |
– |
ClassCastException.class, |
185 |
|
description + ": Must throw ClassCastException for non-Comparable element"); |
186 |
|
|
187 |
|
NavigableMap ss = navigableMap.headMap("1", false); |
202 |
|
*/ |
203 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
204 |
|
public void testSubMap(String description, NavigableMap navigableMap) { |
205 |
< |
assertThrows( |
205 |
> |
assertThrowsNPE( |
206 |
|
() -> { |
207 |
|
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); |
208 |
|
}, |
213 |
– |
NullPointerException.class, |
209 |
|
description + ": Must throw NullPointerException for null element"); |
210 |
|
|
211 |
< |
assertThrows( |
211 |
> |
assertThrowsNPE( |
212 |
|
() -> { |
213 |
|
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); |
214 |
|
}, |
220 |
– |
NullPointerException.class, |
215 |
|
description + ": Must throw NullPointerException for null element"); |
216 |
|
|
217 |
< |
assertThrows( |
217 |
> |
assertThrowsNPE( |
218 |
|
() -> { |
219 |
|
SortedMap ss = navigableMap.subMap(null, null); |
220 |
|
}, |
227 |
– |
NullPointerException.class, |
221 |
|
description + ": Must throw NullPointerException for null element"); |
222 |
|
|
223 |
|
Object obj1 = new Object(); |
224 |
|
Object obj2 = new Object(); |
225 |
|
|
226 |
< |
assertThrows( |
226 |
> |
assertThrowsCCE( |
227 |
|
() -> { |
228 |
|
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); |
229 |
|
}, |
230 |
< |
ClassCastException.class, description |
238 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
230 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
231 |
|
|
232 |
< |
assertThrows( |
232 |
> |
assertThrowsCCE( |
233 |
|
() -> { |
234 |
|
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); |
235 |
|
}, |
236 |
< |
ClassCastException.class, description |
245 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
236 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
237 |
|
|
238 |
< |
assertThrows( |
238 |
> |
assertThrowsCCE( |
239 |
|
() -> { |
240 |
|
SortedMap ss = navigableMap.subMap(obj1, obj2); |
241 |
|
}, |
242 |
< |
ClassCastException.class, description |
252 |
< |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
242 |
> |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
243 |
|
|
244 |
|
// minimal range |
245 |
|
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); |
250 |
|
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
251 |
|
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
252 |
|
|
253 |
< |
assertThrows( |
254 |
< |
() -> { |
255 |
< |
navigableMap.subMap(last, true, first, false); |
266 |
< |
}, |
267 |
< |
IllegalArgumentException.class, description |
268 |
< |
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
253 |
> |
assertThrowsIAE( |
254 |
> |
() -> navigableMap.subMap(last, true, first, false), |
255 |
> |
description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
256 |
|
|
257 |
|
navigableMap.subMap(first, true, last, false); |
258 |
|
} |
270 |
|
// slightly smaller |
271 |
|
NavigableMap ns = subMap.subMap(first, false, last, false); |
272 |
|
// slight expansion |
273 |
< |
assertThrows(() -> { |
274 |
< |
ns.subMap(first, true, last, true); |
288 |
< |
}, |
289 |
< |
IllegalArgumentException.class, |
273 |
> |
assertThrowsIAE( |
274 |
> |
() -> ns.subMap(first, true, last, true), |
275 |
|
description + ": Expansion should not be allowed"); |
276 |
|
|
277 |
|
// much smaller |
289 |
|
NavigableMap ns = subMap.headMap(BigInteger.ONE, false); |
290 |
|
|
291 |
|
// slight expansion |
292 |
< |
assertThrows(() -> { |
293 |
< |
ns.headMap(BigInteger.ONE, true); |
309 |
< |
}, |
310 |
< |
IllegalArgumentException.class, |
292 |
> |
assertThrowsIAE( |
293 |
> |
() -> ns.headMap(BigInteger.ONE, true), |
294 |
|
description + ": Expansion should not be allowed"); |
295 |
|
|
296 |
|
// much smaller |
308 |
|
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); |
309 |
|
|
310 |
|
// slight expansion |
311 |
< |
assertThrows(() -> { |
312 |
< |
ns.tailMap(BigInteger.ONE, true); |
330 |
< |
}, |
331 |
< |
IllegalArgumentException.class, |
311 |
> |
assertThrowsIAE( |
312 |
> |
() -> ns.tailMap(BigInteger.ONE, true), |
313 |
|
description + ": Expansion should not be allowed"); |
314 |
|
|
315 |
|
// much smaller |
321 |
|
*/ |
322 |
|
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
323 |
|
public void testTailMap(String description, NavigableMap navigableMap) { |
324 |
< |
assertThrows(() -> { |
325 |
< |
navigableMap.tailMap(null); |
345 |
< |
}, |
346 |
< |
NullPointerException.class, |
324 |
> |
assertThrowsNPE( |
325 |
> |
() -> navigableMap.tailMap(null), |
326 |
|
description + ": Must throw NullPointerException for null element"); |
327 |
|
|
328 |
< |
assertThrows(() -> { |
329 |
< |
navigableMap.tailMap(new Object()); |
330 |
< |
}, ClassCastException.class); |
328 |
> |
assertThrowsCCE( |
329 |
> |
() -> navigableMap.tailMap(new Object()), |
330 |
> |
description); |
331 |
|
|
332 |
|
NavigableMap ss = navigableMap.tailMap("1", true); |
333 |
|
|