1 |
|
/* |
2 |
< |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2013, 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 |
53 |
|
import java.util.function.Function; |
54 |
|
import java.util.function.Supplier; |
55 |
|
|
56 |
+ |
import org.testng.Assert.ThrowingRunnable; |
57 |
|
import org.testng.annotations.Test; |
58 |
|
import org.testng.annotations.DataProvider; |
59 |
+ |
|
60 |
|
import static java.util.Objects.requireNonNull; |
61 |
+ |
|
62 |
|
import static org.testng.Assert.fail; |
63 |
|
import static org.testng.Assert.assertEquals; |
64 |
|
import static org.testng.Assert.assertTrue; |
65 |
|
import static org.testng.Assert.assertFalse; |
66 |
|
import static org.testng.Assert.assertNull; |
67 |
|
import static org.testng.Assert.assertSame; |
68 |
+ |
import static org.testng.Assert.assertThrows; |
69 |
|
|
70 |
|
public class Defaults { |
71 |
|
|
163 |
|
|
164 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull") |
165 |
|
public static void testReplaceAllNoNullReplacement(String description, Map<IntegerEnum, String> map) { |
166 |
< |
assertThrows( |
167 |
< |
() -> { map.replaceAll(null); }, |
164 |
< |
NullPointerException.class, |
165 |
< |
description); |
166 |
< |
assertThrows( |
167 |
< |
() -> { map.replaceAll((k,v) -> null); }, |
168 |
< |
NullPointerException.class, |
169 |
< |
description + " should not allow replacement with null value"); |
166 |
> |
assertThrowsNPE(() -> map.replaceAll(null)); |
167 |
> |
assertThrowsNPE(() -> map.replaceAll((k,v) -> null)); //should not allow replacement with null value |
168 |
|
} |
169 |
|
|
170 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
207 |
|
public void testReplaceKVNoNulls(String description, Map<IntegerEnum, String> map) { |
208 |
|
assertTrue(map.containsKey(FIRST_KEY), "expected key missing"); |
209 |
|
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value"); |
210 |
< |
assertThrows( () -> {map.replace(FIRST_KEY, null);}, NullPointerException.class, description + ": should throw NPE"); |
210 |
> |
assertThrowsNPE(() -> map.replace(FIRST_KEY, null)); |
211 |
|
assertSame(map.replace(FIRST_KEY, EXTRA_VALUE), FIRST_VALUE, description + ": replaced wrong value"); |
212 |
|
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value"); |
213 |
|
} |
246 |
|
public void testReplaceKVVNoNulls(String description, Map<IntegerEnum, String> map) { |
247 |
|
assertTrue(map.containsKey(FIRST_KEY), "expected key missing"); |
248 |
|
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value"); |
249 |
< |
assertThrows( () -> {map.replace(FIRST_KEY, FIRST_VALUE, null);}, NullPointerException.class, description + ": should throw NPE"); |
250 |
< |
assertThrows( () -> {if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) throw new NullPointerException("default returns false rather than throwing");}, NullPointerException.class, description + ": should throw NPE"); |
249 |
> |
assertThrowsNPE(() -> map.replace(FIRST_KEY, FIRST_VALUE, null)); |
250 |
> |
assertThrowsNPE( |
251 |
> |
() -> { |
252 |
> |
if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) { |
253 |
> |
throw new NullPointerException("default returns false rather than throwing"); |
254 |
> |
} |
255 |
> |
}); |
256 |
|
assertTrue(map.replace(FIRST_KEY, FIRST_VALUE, EXTRA_VALUE), description + ": replaced wrong value"); |
257 |
|
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value"); |
258 |
|
} |
322 |
|
|
323 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
324 |
|
public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) { |
325 |
< |
assertThrows( () -> { map.computeIfAbsent(KEYS[1], null);}, |
323 |
< |
NullPointerException.class, |
324 |
< |
"Should throw NPE"); |
325 |
> |
assertThrowsNPE(() -> map.computeIfAbsent(KEYS[1], null)); |
326 |
|
} |
327 |
|
|
328 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
367 |
|
|
368 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
369 |
|
public void testComputeIfPresentNullFunction(String description, Map<IntegerEnum, String> map) { |
370 |
< |
assertThrows( () -> { map.computeIfPresent(KEYS[1], null);}, |
370 |
< |
NullPointerException.class, |
371 |
< |
"Should throw NPE"); |
370 |
> |
assertThrowsNPE(() -> map.computeIfPresent(KEYS[1], null)); |
371 |
|
} |
372 |
|
|
373 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
458 |
|
|
459 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
460 |
|
public void testComputeNullFunction(String description, Map<IntegerEnum, String> map) { |
461 |
< |
assertThrows( () -> { map.compute(KEYS[1], null);}, |
463 |
< |
NullPointerException.class, |
464 |
< |
"Should throw NPE"); |
461 |
> |
assertThrowsNPE(() -> map.compute(KEYS[1], null)); |
462 |
|
} |
463 |
|
|
464 |
|
@Test(dataProvider = "MergeCases") |
528 |
|
|
529 |
|
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
530 |
|
public void testMergeNullMerger(String description, Map<IntegerEnum, String> map) { |
531 |
< |
assertThrows( () -> { map.merge(KEYS[1], VALUES[1], null);}, |
535 |
< |
NullPointerException.class, |
536 |
< |
"Should throw NPE"); |
531 |
> |
assertThrowsNPE(() -> map.merge(KEYS[1], VALUES[1], null)); |
532 |
|
} |
533 |
|
|
534 |
|
/** A function that flipflops between running two other functions. */ |
968 |
|
return cases; |
969 |
|
} |
970 |
|
|
971 |
< |
public interface Thrower<T extends Throwable> { |
972 |
< |
|
978 |
< |
public void run() throws T; |
979 |
< |
} |
980 |
< |
|
981 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
982 |
< |
assertThrows(thrower, throwable, null); |
983 |
< |
} |
984 |
< |
|
985 |
< |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
986 |
< |
Throwable thrown; |
987 |
< |
try { |
988 |
< |
thrower.run(); |
989 |
< |
thrown = null; |
990 |
< |
} catch (Throwable caught) { |
991 |
< |
thrown = caught; |
992 |
< |
} |
993 |
< |
|
994 |
< |
assertInstance(thrown, throwable, |
995 |
< |
((null != message) ? message : "") + |
996 |
< |
" Failed to throw " + throwable.getCanonicalName()); |
997 |
< |
} |
998 |
< |
|
999 |
< |
public static <T extends Throwable> void assertThrows(Class<T> throwable, String message, Thrower<T>... throwers) { |
1000 |
< |
for (Thrower<T> thrower : throwers) { |
1001 |
< |
assertThrows(thrower, throwable, message); |
1002 |
< |
} |
1003 |
< |
} |
1004 |
< |
|
1005 |
< |
public static void assertInstance(Object actual, Class<?> expected) { |
1006 |
< |
assertInstance(expected.isInstance(actual), null); |
1007 |
< |
} |
1008 |
< |
|
1009 |
< |
public static void assertInstance(Object actual, Class<?> expected, String message) { |
1010 |
< |
assertTrue(expected.isInstance(actual), message); |
971 |
> |
public static void assertThrowsNPE(ThrowingRunnable r) { |
972 |
> |
assertThrows(NullPointerException.class, r); |
973 |
|
} |
974 |
|
|
975 |
|
/** |