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