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 |
description + ": Compareable should be required"); |
130 |
} |
131 |
|
132 |
/** |
133 |
* Tests that the contains method returns {@code false}. |
134 |
*/ |
135 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
136 |
public void testContains(String description, NavigableMap<?,?> navigableMap) { |
137 |
assertFalse(navigableMap.containsKey(new Integer(1)), |
138 |
description + ": Should not contain any elements."); |
139 |
assertFalse(navigableMap.containsValue(new Integer(1)), |
140 |
description + ": Should not contain any elements."); |
141 |
} |
142 |
|
143 |
/** |
144 |
* Tests that the containsAll method returns {@code false}. |
145 |
*/ |
146 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
147 |
public void testContainsAll(String description, NavigableMap<?,?> navigableMap) { |
148 |
TreeMap treeMap = new TreeMap(); |
149 |
treeMap.put("1", 1); |
150 |
treeMap.put("2", 2); |
151 |
treeMap.put("3", 3); |
152 |
|
153 |
assertFalse(navigableMap.equals(treeMap), "Should not contain any elements."); |
154 |
} |
155 |
|
156 |
/** |
157 |
* Tests that the iterator is empty. |
158 |
*/ |
159 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
160 |
public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) { |
161 |
assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty."); |
162 |
assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty."); |
163 |
assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty."); |
164 |
} |
165 |
|
166 |
/** |
167 |
* Tests that the set is empty. |
168 |
*/ |
169 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
170 |
public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) { |
171 |
assertTrue(navigableMap.isEmpty(), "The set is not empty."); |
172 |
} |
173 |
|
174 |
/** |
175 |
* Tests the headMap() method. |
176 |
*/ |
177 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
178 |
public void testHeadMap(String description, NavigableMap navigableMap) { |
179 |
assertThrowsNPE( |
180 |
() -> { NavigableMap ss = navigableMap.headMap(null, false); }, |
181 |
description + ": Must throw NullPointerException for null element"); |
182 |
|
183 |
assertThrowsCCE( |
184 |
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, |
185 |
description + ": Must throw ClassCastException for non-Comparable element"); |
186 |
|
187 |
NavigableMap ss = navigableMap.headMap("1", false); |
188 |
|
189 |
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); |
190 |
} |
191 |
|
192 |
/** |
193 |
* Tests that the size is 0. |
194 |
*/ |
195 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
196 |
public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) { |
197 |
assertTrue(0 == navigableMap.size(), "The size of the set is not 0."); |
198 |
} |
199 |
|
200 |
/** |
201 |
* Tests the subMap() method. |
202 |
*/ |
203 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
204 |
public void testSubMap(String description, NavigableMap navigableMap) { |
205 |
assertThrowsNPE( |
206 |
() -> { |
207 |
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); |
208 |
}, |
209 |
description + ": Must throw NullPointerException for null element"); |
210 |
|
211 |
assertThrowsNPE( |
212 |
() -> { |
213 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); |
214 |
}, |
215 |
description + ": Must throw NullPointerException for null element"); |
216 |
|
217 |
assertThrowsNPE( |
218 |
() -> { |
219 |
SortedMap ss = navigableMap.subMap(null, null); |
220 |
}, |
221 |
description + ": Must throw NullPointerException for null element"); |
222 |
|
223 |
Object obj1 = new Object(); |
224 |
Object obj2 = new Object(); |
225 |
|
226 |
assertThrowsCCE( |
227 |
() -> { |
228 |
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); |
229 |
}, |
230 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
231 |
|
232 |
assertThrowsCCE( |
233 |
() -> { |
234 |
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); |
235 |
}, |
236 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
237 |
|
238 |
assertThrowsCCE( |
239 |
() -> { |
240 |
SortedMap ss = navigableMap.subMap(obj1, obj2); |
241 |
}, |
242 |
description + ": Must throw ClassCastException for parameter which is not Comparable."); |
243 |
|
244 |
// minimal range |
245 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); |
246 |
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true); |
247 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false); |
248 |
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true); |
249 |
|
250 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
251 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
252 |
|
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 |
} |
259 |
|
260 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
261 |
public void testSubMapRanges(String description, NavigableMap navigableMap) { |
262 |
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; |
263 |
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; |
264 |
|
265 |
NavigableMap subMap = navigableMap.subMap(first, true, last, true); |
266 |
|
267 |
// same subset |
268 |
subMap.subMap(first, true, last, true); |
269 |
|
270 |
// slightly smaller |
271 |
NavigableMap ns = subMap.subMap(first, false, last, false); |
272 |
// slight expansion |
273 |
assertThrowsIAE( |
274 |
() -> ns.subMap(first, true, last, true), |
275 |
description + ": Expansion should not be allowed"); |
276 |
|
277 |
// much smaller |
278 |
subMap.subMap(first, false, BigInteger.ONE, false); |
279 |
} |
280 |
|
281 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
282 |
public void testheadMapRanges(String description, NavigableMap navigableMap) { |
283 |
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true); |
284 |
|
285 |
// same subset |
286 |
subMap.headMap(BigInteger.ONE, true); |
287 |
|
288 |
// slightly smaller |
289 |
NavigableMap ns = subMap.headMap(BigInteger.ONE, false); |
290 |
|
291 |
// slight expansion |
292 |
assertThrowsIAE( |
293 |
() -> ns.headMap(BigInteger.ONE, true), |
294 |
description + ": Expansion should not be allowed"); |
295 |
|
296 |
// much smaller |
297 |
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true); |
298 |
} |
299 |
|
300 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
301 |
public void testTailMapRanges(String description, NavigableMap navigableMap) { |
302 |
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true); |
303 |
|
304 |
// same subset |
305 |
subMap.tailMap(BigInteger.ONE, true); |
306 |
|
307 |
// slightly smaller |
308 |
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); |
309 |
|
310 |
// slight expansion |
311 |
assertThrowsIAE( |
312 |
() -> ns.tailMap(BigInteger.ONE, true), |
313 |
description + ": Expansion should not be allowed"); |
314 |
|
315 |
// much smaller |
316 |
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false); |
317 |
} |
318 |
|
319 |
/** |
320 |
* Tests the tailMap() method. |
321 |
*/ |
322 |
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) |
323 |
public void testTailMap(String description, NavigableMap navigableMap) { |
324 |
assertThrowsNPE( |
325 |
() -> navigableMap.tailMap(null), |
326 |
description + ": Must throw NullPointerException for null element"); |
327 |
|
328 |
assertThrowsCCE( |
329 |
() -> navigableMap.tailMap(new Object()), |
330 |
description); |
331 |
|
332 |
NavigableMap ss = navigableMap.tailMap("1", true); |
333 |
|
334 |
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); |
335 |
} |
336 |
|
337 |
@DataProvider(name = "NavigableMap<?,?>", parallel = true) |
338 |
public static Iterator<Object[]> navigableMapsProvider() { |
339 |
return makeNavigableMaps().iterator(); |
340 |
} |
341 |
|
342 |
public static Collection<Object[]> makeNavigableMaps() { |
343 |
return Arrays.asList( |
344 |
new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())}, |
345 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())}, |
346 |
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())}, |
347 |
new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()}, |
348 |
new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()}, |
349 |
new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()} |
350 |
); |
351 |
} |
352 |
} |