1 |
/* |
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 |
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 |
|
40 |
import org.testng.Assert; |
41 |
import org.testng.Assert.ThrowingRunnable; |
42 |
import org.testng.annotations.Test; |
43 |
import org.testng.annotations.DataProvider; |
44 |
|
45 |
import static org.testng.Assert.assertTrue; |
46 |
import static org.testng.Assert.assertFalse; |
47 |
|
48 |
public class EmptyNavigableMap { |
49 |
|
50 |
public static <T> void assertInstance(T actual, Class<? extends T> expected) { |
51 |
assertInstance(actual, expected, null); |
52 |
} |
53 |
|
54 |
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { |
55 |
assertTrue(expected.isInstance(actual), ((null != message) ? message : "") |
56 |
+ " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". "); |
57 |
} |
58 |
|
59 |
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) { |
60 |
assertInstance(obj, NavigableMap.class); |
61 |
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0)); |
62 |
} |
63 |
|
64 |
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) { |
65 |
assertInstance(obj, NavigableMap.class, message); |
66 |
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0), |
67 |
((null != message) ? message : "") + " Not empty. "); |
68 |
} |
69 |
|
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 |
private void assertThrowsCCE(ThrowingRunnable r, String s) { |
82 |
assertThrows(ClassCastException.class, r, s); |
83 |
} |
84 |
|
85 |
private void assertThrowsNPE(ThrowingRunnable r, String s) { |
86 |
assertThrows(NullPointerException.class, r, s); |
87 |
} |
88 |
|
89 |
private void assertThrowsIAE(ThrowingRunnable r, String s) { |
90 |
assertThrows(IllegalArgumentException.class, r, s); |
91 |
} |
92 |
|
93 |
public static final boolean isDescending(SortedMap<?,?> set) { |
94 |
if (null == set.comparator()) { |
95 |
// natural order |
96 |
return false; |
97 |
} |
98 |
|
99 |
if (Collections.reverseOrder() == set.comparator()) { |
100 |
// reverse natural order. |
101 |
return true; |
102 |
} |
103 |
|
104 |
if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) { |
105 |
// it's a Collections.reverseOrder(Comparator). |
106 |
return true; |
107 |
} |
108 |
|
109 |
throw new IllegalStateException("can't determine ordering for " + set); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Tests that the comparator is {@code null}. |
114 |
*/ |
115 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
116 |
public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) { |
117 |
Comparator comparator = navigableMap.comparator(); |
118 |
|
119 |
assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null."); |
120 |
} |
121 |
|
122 |
/** |
123 |
* Tests that contains requires Comparable |
124 |
*/ |
125 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
126 |
public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) { |
127 |
assertThrowsCCE(() -> { |
128 |
navigableMap.containsKey(new Object()); |
129 |
}, |
130 |
description + ": Compareable should be required"); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Tests that the contains method returns {@code false}. |
135 |
*/ |
136 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
137 |
public void testContains(String description, NavigableMap<?,?> navigableMap) { |
138 |
assertFalse(navigableMap.containsKey(new Integer(1)), |
139 |
description + ": Should not contain any elements."); |
140 |
assertFalse(navigableMap.containsValue(new Integer(1)), |
141 |
description + ": Should not contain any elements."); |
142 |
} |
143 |
|
144 |
/** |
145 |
* Tests that the containsAll method returns {@code false}. |
146 |
*/ |
147 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
148 |
public void testContainsAll(String description, NavigableMap<?,?> navigableMap) { |
149 |
TreeMap treeMap = new TreeMap(); |
150 |
treeMap.put("1", 1); |
151 |
treeMap.put("2", 2); |
152 |
treeMap.put("3", 3); |
153 |
|
154 |
assertFalse(navigableMap.equals(treeMap), "Should not contain any elements."); |
155 |
} |
156 |
|
157 |
/** |
158 |
* Tests that the iterator is empty. |
159 |
*/ |
160 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
161 |
public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) { |
162 |
assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty."); |
163 |
assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty."); |
164 |
assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty."); |
165 |
} |
166 |
|
167 |
/** |
168 |
* Tests that the set is empty. |
169 |
*/ |
170 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
171 |
public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) { |
172 |
assertTrue(navigableMap.isEmpty(), "The set is not empty."); |
173 |
} |
174 |
|
175 |
/** |
176 |
* Tests the headMap() method. |
177 |
*/ |
178 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
179 |
public void testHeadMap(String description, NavigableMap navigableMap) { |
180 |
assertThrowsNPE( |
181 |
() -> { NavigableMap ss = navigableMap.headMap(null, false); }, |
182 |
description + ": Must throw NullPointerException for null element"); |
183 |
|
184 |
assertThrowsCCE( |
185 |
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, |
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 |
assertThrowsNPE( |
207 |
() -> { |
208 |
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); |
209 |
}, |
210 |
description + ": Must throw NullPointerException for null element"); |
211 |
|
212 |
assertThrowsNPE( |
213 |
() -> { |
214 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); |
215 |
}, |
216 |
description + ": Must throw NullPointerException for null element"); |
217 |
|
218 |
assertThrowsNPE( |
219 |
() -> { |
220 |
SortedMap ss = navigableMap.subMap(null, null); |
221 |
}, |
222 |
description + ": Must throw NullPointerException for null element"); |
223 |
|
224 |
Object obj1 = new Object(); |
225 |
Object obj2 = new Object(); |
226 |
|
227 |
assertThrowsCCE( |
228 |
() -> { |
229 |
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); |
230 |
}, |
231 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
232 |
|
233 |
assertThrowsCCE( |
234 |
() -> { |
235 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); |
236 |
}, |
237 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
238 |
|
239 |
assertThrowsCCE( |
240 |
() -> { |
241 |
SortedMap ss = navigableMap.subMap(obj1, obj2); |
242 |
}, |
243 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
244 |
|
245 |
// minimal range |
246 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); |
247 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true); |
248 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false); |
249 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true); |
250 |
|
251 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
252 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
253 |
|
254 |
assertThrowsIAE( |
255 |
() -> { |
256 |
navigableMap.subMap(last, true, first, false); |
257 |
}, |
258 |
description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); |
259 |
|
260 |
navigableMap.subMap(first, true, last, false); |
261 |
} |
262 |
|
263 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
264 |
public void testSubMapRanges(String description, NavigableMap navigableMap) { |
265 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
266 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
267 |
|
268 |
NavigableMap subMap = navigableMap.subMap(first, true, last, true); |
269 |
|
270 |
// same subset |
271 |
subMap.subMap(first, true, last, true); |
272 |
|
273 |
// slightly smaller |
274 |
NavigableMap ns = subMap.subMap(first, false, last, false); |
275 |
// slight expansion |
276 |
assertThrowsIAE(() -> { |
277 |
ns.subMap(first, true, last, true); |
278 |
}, |
279 |
description + ": Expansion should not be allowed"); |
280 |
|
281 |
// much smaller |
282 |
subMap.subMap(first, false, BigInteger.ONE, false); |
283 |
} |
284 |
|
285 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
286 |
public void testheadMapRanges(String description, NavigableMap navigableMap) { |
287 |
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true); |
288 |
|
289 |
// same subset |
290 |
subMap.headMap(BigInteger.ONE, true); |
291 |
|
292 |
// slightly smaller |
293 |
NavigableMap ns = subMap.headMap(BigInteger.ONE, false); |
294 |
|
295 |
// slight expansion |
296 |
assertThrowsIAE(() -> { |
297 |
ns.headMap(BigInteger.ONE, true); |
298 |
}, |
299 |
description + ": Expansion should not be allowed"); |
300 |
|
301 |
// much smaller |
302 |
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true); |
303 |
} |
304 |
|
305 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
306 |
public void testTailMapRanges(String description, NavigableMap navigableMap) { |
307 |
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true); |
308 |
|
309 |
// same subset |
310 |
subMap.tailMap(BigInteger.ONE, true); |
311 |
|
312 |
// slightly smaller |
313 |
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); |
314 |
|
315 |
// slight expansion |
316 |
assertThrowsIAE(() -> { |
317 |
ns.tailMap(BigInteger.ONE, true); |
318 |
}, |
319 |
description + ": Expansion should not be allowed"); |
320 |
|
321 |
// much smaller |
322 |
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false); |
323 |
} |
324 |
|
325 |
/** |
326 |
* Tests the tailMap() method. |
327 |
*/ |
328 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
329 |
public void testTailMap(String description, NavigableMap navigableMap) { |
330 |
assertThrowsNPE(() -> { |
331 |
navigableMap.tailMap(null); |
332 |
}, |
333 |
description + ": Must throw NullPointerException for null element"); |
334 |
|
335 |
assertThrowsCCE(() -> { |
336 |
navigableMap.tailMap(new Object()); |
337 |
}, |
338 |
description); |
339 |
|
340 |
NavigableMap ss = navigableMap.tailMap("1", true); |
341 |
|
342 |
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); |
343 |
} |
344 |
|
345 |
@DataProvider(name = "NavigableMap<?,?>", parallel = true) |
346 |
public static Iterator<Object[]> navigableMapsProvider() { |
347 |
return makeNavigableMaps().iterator(); |
348 |
} |
349 |
|
350 |
public static Collection<Object[]> makeNavigableMaps() { |
351 |
return Arrays.asList( |
352 |
new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())}, |
353 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())}, |
354 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())}, |
355 |
new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()}, |
356 |
new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()}, |
357 |
new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()} |
358 |
); |
359 |
} |
360 |
} |