1 |
/* |
2 |
* Copyright (c) 2011, 2013, 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 |
6 |
* under the terms of the GNU General Public License version 2 only, as |
7 |
* published by the Free Software Foundation. |
8 |
* |
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
13 |
* accompanied this code). |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License version |
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
* |
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
21 |
* questions. |
22 |
*/ |
23 |
|
24 |
/* |
25 |
* @test |
26 |
* @bug 4533691 7129185 |
27 |
* @summary Unit test for Collections.emptyNavigableMap |
28 |
* @run testng EmptyNavigableMap |
29 |
*/ |
30 |
import java.math.BigInteger; |
31 |
import java.util.Arrays; |
32 |
import java.util.Collection; |
33 |
import java.util.Collections; |
34 |
import java.util.Comparator; |
35 |
import java.util.Iterator; |
36 |
import java.util.NavigableMap; |
37 |
import java.util.SortedMap; |
38 |
import java.util.TreeMap; |
39 |
import org.testng.annotations.Test; |
40 |
import org.testng.annotations.DataProvider; |
41 |
|
42 |
import static org.testng.Assert.fail; |
43 |
import static org.testng.Assert.assertTrue; |
44 |
import static org.testng.Assert.assertFalse; |
45 |
|
46 |
public class EmptyNavigableMap { |
47 |
|
48 |
public static <T> void assertInstance(T actual, Class<? extends T> expected) { |
49 |
assertInstance(expected.isInstance(actual), null); |
50 |
} |
51 |
|
52 |
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { |
53 |
assertTrue(expected.isInstance(actual), ((null != message) ? message : "") |
54 |
+ " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". "); |
55 |
} |
56 |
|
57 |
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) { |
58 |
assertInstance(obj, NavigableMap.class); |
59 |
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0)); |
60 |
} |
61 |
|
62 |
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) { |
63 |
assertInstance(obj, NavigableMap.class, message); |
64 |
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0), |
65 |
((null != message) ? message : "") + " Not empty. "); |
66 |
} |
67 |
|
68 |
public interface Thrower<T extends Throwable> { |
69 |
|
70 |
public void run() throws T; |
71 |
} |
72 |
|
73 |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
74 |
assertThrows(thrower, throwable, null); |
75 |
} |
76 |
|
77 |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
78 |
Throwable result; |
79 |
try { |
80 |
thrower.run(); |
81 |
fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
82 |
return; |
83 |
} catch (Throwable caught) { |
84 |
result = caught; |
85 |
} |
86 |
|
87 |
assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); |
88 |
} |
89 |
|
90 |
public static final boolean isDescending(SortedMap<?,?> set) { |
91 |
if (null == set.comparator()) { |
92 |
// natural order |
93 |
return false; |
94 |
} |
95 |
|
96 |
if (Collections.reverseOrder() == set.comparator()) { |
97 |
// reverse natural order. |
98 |
return true; |
99 |
} |
100 |
|
101 |
if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) { |
102 |
// it's a Collections.reverseOrder(Comparator). |
103 |
return true; |
104 |
} |
105 |
|
106 |
throw new IllegalStateException("can't determine ordering for " + set); |
107 |
} |
108 |
|
109 |
/** |
110 |
* Tests that the comparator is {@code null}. |
111 |
*/ |
112 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
113 |
public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) { |
114 |
Comparator comparator = navigableMap.comparator(); |
115 |
|
116 |
assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null."); |
117 |
} |
118 |
|
119 |
/** |
120 |
* Tests that contains requires Comparable |
121 |
*/ |
122 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
123 |
public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) { |
124 |
assertThrows(() -> { |
125 |
navigableMap.containsKey(new Object()); |
126 |
}, |
127 |
ClassCastException.class, |
128 |
description + ": Comparable should be required"); |
129 |
} |
130 |
|
131 |
/** |
132 |
* Tests that the contains method returns {@code false}. |
133 |
*/ |
134 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
135 |
public void testContains(String description, NavigableMap<?,?> navigableMap) { |
136 |
assertFalse(navigableMap.containsKey(new Integer(1)), |
137 |
description + ": Should not contain any elements."); |
138 |
assertFalse(navigableMap.containsValue(new Integer(1)), |
139 |
description + ": Should not contain any elements."); |
140 |
} |
141 |
|
142 |
/** |
143 |
* Tests that the containsAll method returns {@code false}. |
144 |
*/ |
145 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
146 |
public void testContainsAll(String description, NavigableMap<?,?> navigableMap) { |
147 |
TreeMap treeMap = new TreeMap(); |
148 |
treeMap.put("1", 1); |
149 |
treeMap.put("2", 2); |
150 |
treeMap.put("3", 3); |
151 |
|
152 |
assertFalse(navigableMap.equals(treeMap), "Should not contain any elements."); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Tests that the iterator is empty. |
157 |
*/ |
158 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
159 |
public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) { |
160 |
assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty."); |
161 |
assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty."); |
162 |
assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty."); |
163 |
} |
164 |
|
165 |
/** |
166 |
* Tests that the set is empty. |
167 |
*/ |
168 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
169 |
public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) { |
170 |
assertTrue(navigableMap.isEmpty(), "The set is not empty."); |
171 |
} |
172 |
|
173 |
/** |
174 |
* Tests the headMap() method. |
175 |
*/ |
176 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
177 |
public void testHeadMap(String description, NavigableMap navigableMap) { |
178 |
assertThrows( |
179 |
() -> { NavigableMap ss = navigableMap.headMap(null, false); }, |
180 |
NullPointerException.class, |
181 |
description + ": Must throw NullPointerException for null element"); |
182 |
|
183 |
assertThrows( |
184 |
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, |
185 |
ClassCastException.class, |
186 |
description + ": Must throw ClassCastException for non-Comparable element"); |
187 |
|
188 |
NavigableMap ss = navigableMap.headMap("1", false); |
189 |
|
190 |
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Tests that the size is 0. |
195 |
*/ |
196 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
197 |
public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) { |
198 |
assertTrue(0 == navigableMap.size(), "The size of the set is not 0."); |
199 |
} |
200 |
|
201 |
/** |
202 |
* Tests the subMap() method. |
203 |
*/ |
204 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
205 |
public void testSubMap(String description, NavigableMap navigableMap) { |
206 |
assertThrows( |
207 |
() -> { |
208 |
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); |
209 |
}, |
210 |
NullPointerException.class, |
211 |
description + ": Must throw NullPointerException for null element"); |
212 |
|
213 |
assertThrows( |
214 |
() -> { |
215 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); |
216 |
}, |
217 |
NullPointerException.class, |
218 |
description + ": Must throw NullPointerException for null element"); |
219 |
|
220 |
assertThrows( |
221 |
() -> { |
222 |
SortedMap ss = navigableMap.subMap(null, null); |
223 |
}, |
224 |
NullPointerException.class, |
225 |
description + ": Must throw NullPointerException for null element"); |
226 |
|
227 |
Object obj1 = new Object(); |
228 |
Object obj2 = new Object(); |
229 |
|
230 |
assertThrows( |
231 |
() -> { |
232 |
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); |
233 |
}, |
234 |
ClassCastException.class, description |
235 |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
236 |
|
237 |
assertThrows( |
238 |
() -> { |
239 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); |
240 |
}, |
241 |
ClassCastException.class, description |
242 |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
243 |
|
244 |
assertThrows( |
245 |
() -> { |
246 |
SortedMap ss = navigableMap.subMap(obj1, obj2); |
247 |
}, |
248 |
ClassCastException.class, description |
249 |
+ ": Must throw ClassCastException for parameter which is not Comparable."); |
250 |
|
251 |
// minimal range |
252 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); |
253 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true); |
254 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false); |
255 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true); |
256 |
|
257 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
258 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
259 |
|
260 |
assertThrows( |
261 |
() -> { |
262 |
navigableMap.subMap(last, true, first, false); |
263 |
}, |
264 |
IllegalArgumentException.class, description |
265 |
+ ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
266 |
|
267 |
navigableMap.subMap(first, true, last, false); |
268 |
} |
269 |
|
270 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
271 |
public void testSubMapRanges(String description, NavigableMap navigableMap) { |
272 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
273 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
274 |
|
275 |
NavigableMap subMap = navigableMap.subMap(first, true, last, true); |
276 |
|
277 |
// same subset |
278 |
subMap.subMap(first, true, last, true); |
279 |
|
280 |
// slightly smaller |
281 |
NavigableMap ns = subMap.subMap(first, false, last, false); |
282 |
// slight expansion |
283 |
assertThrows(() -> { |
284 |
ns.subMap(first, true, last, true); |
285 |
}, |
286 |
IllegalArgumentException.class, |
287 |
description + ": Expansion should not be allowed"); |
288 |
|
289 |
// much smaller |
290 |
subMap.subMap(first, false, BigInteger.ONE, false); |
291 |
} |
292 |
|
293 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
294 |
public void testheadMapRanges(String description, NavigableMap navigableMap) { |
295 |
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true); |
296 |
|
297 |
// same subset |
298 |
subMap.headMap(BigInteger.ONE, true); |
299 |
|
300 |
// slightly smaller |
301 |
NavigableMap ns = subMap.headMap(BigInteger.ONE, false); |
302 |
|
303 |
// slight expansion |
304 |
assertThrows(() -> { |
305 |
ns.headMap(BigInteger.ONE, true); |
306 |
}, |
307 |
IllegalArgumentException.class, |
308 |
description + ": Expansion should not be allowed"); |
309 |
|
310 |
// much smaller |
311 |
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true); |
312 |
} |
313 |
|
314 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
315 |
public void testTailMapRanges(String description, NavigableMap navigableMap) { |
316 |
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true); |
317 |
|
318 |
// same subset |
319 |
subMap.tailMap(BigInteger.ONE, true); |
320 |
|
321 |
// slightly smaller |
322 |
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); |
323 |
|
324 |
// slight expansion |
325 |
assertThrows(() -> { |
326 |
ns.tailMap(BigInteger.ONE, true); |
327 |
}, |
328 |
IllegalArgumentException.class, |
329 |
description + ": Expansion should not be allowed"); |
330 |
|
331 |
// much smaller |
332 |
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false); |
333 |
} |
334 |
|
335 |
/** |
336 |
* Tests the tailMap() method. |
337 |
*/ |
338 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
339 |
public void testTailMap(String description, NavigableMap navigableMap) { |
340 |
assertThrows(() -> { |
341 |
navigableMap.tailMap(null); |
342 |
}, |
343 |
NullPointerException.class, |
344 |
description + ": Must throw NullPointerException for null element"); |
345 |
|
346 |
assertThrows(() -> { |
347 |
navigableMap.tailMap(new Object()); |
348 |
}, ClassCastException.class); |
349 |
|
350 |
NavigableMap ss = navigableMap.tailMap("1", true); |
351 |
|
352 |
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); |
353 |
} |
354 |
|
355 |
@DataProvider(name = "NavigableMap<?,?>", parallel = true) |
356 |
public static Iterator<Object[]> navigableMapsProvider() { |
357 |
return makeNavigableMaps().iterator(); |
358 |
} |
359 |
|
360 |
public static Collection<Object[]> makeNavigableMaps() { |
361 |
return Arrays.asList( |
362 |
new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())}, |
363 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())}, |
364 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())}, |
365 |
new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()}, |
366 |
new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()}, |
367 |
new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()} |
368 |
); |
369 |
} |
370 |
} |