< prev index next >
test/java/util/Map/Defaults.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2013, 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.
--- 1,7 ----
/*
! * Copyright (c) 2013, 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.
*** 51,69 ****
--- 51,73 ----
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
+ import org.testng.Assert.ThrowingRunnable;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
+
import static java.util.Objects.requireNonNull;
+
import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
+ import static org.testng.Assert.assertThrows;
public class Defaults {
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=withNull values=withNull")
public void testGetOrDefaultNulls(String description, Map<IntegerEnum, String> map) {
*** 157,174 ****
assertTrue(map.values().containsAll(EACH_REPLACE), description + " : " + EACH_REPLACE + " != " + map.values());
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public static void testReplaceAllNoNullReplacement(String description, Map<IntegerEnum, String> map) {
! assertThrows(
! () -> { map.replaceAll(null); },
! NullPointerException.class,
! description);
! assertThrows(
! () -> { map.replaceAll((k,v) -> null); },
! NullPointerException.class,
! description + " should not allow replacement with null value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public static void testRemoveNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
--- 161,172 ----
assertTrue(map.values().containsAll(EACH_REPLACE), description + " : " + EACH_REPLACE + " != " + map.values());
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public static void testReplaceAllNoNullReplacement(String description, Map<IntegerEnum, String> map) {
! assertThrowsNPE(() -> map.replaceAll(null));
! assertThrowsNPE(() -> { map.replaceAll((k,v) -> null); });
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public static void testRemoveNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
*** 207,217 ****
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
! assertThrows( () -> {map.replace(FIRST_KEY, null);}, NullPointerException.class, description + ": should throw NPE");
assertSame(map.replace(FIRST_KEY, EXTRA_VALUE), FIRST_VALUE, description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
--- 205,215 ----
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
! assertThrowsNPE(() -> map.replace(FIRST_KEY, null));
assertSame(map.replace(FIRST_KEY, EXTRA_VALUE), FIRST_VALUE, description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
*** 246,257 ****
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
! assertThrows( () -> {map.replace(FIRST_KEY, FIRST_VALUE, null);}, NullPointerException.class, description + ": should throw NPE");
! assertThrows( () -> {if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) throw new NullPointerException("default returns false rather than throwing");}, NullPointerException.class, description + ": should throw NPE");
assertTrue(map.replace(FIRST_KEY, FIRST_VALUE, EXTRA_VALUE), description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
--- 244,260 ----
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
! assertThrowsNPE(() -> map.replace(FIRST_KEY, FIRST_VALUE, null));
! assertThrowsNPE(
! () -> {
! if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) {
! throw new NullPointerException("default returns false rather than throwing");
! }
! });
assertTrue(map.replace(FIRST_KEY, FIRST_VALUE, EXTRA_VALUE), description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
*** 317,329 ****
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrows( () -> { map.computeIfAbsent(KEYS[1], null);},
! NullPointerException.class,
! "Should throw NPE");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), description + ": null key absent");
--- 320,330 ----
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrowsNPE(() -> map.computeIfAbsent(KEYS[1], null));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), description + ": null key absent");
*** 364,376 ****
assertSame(map.get(EXTRA_KEY), null);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfPresentNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrows( () -> { map.computeIfPresent(KEYS[1], null);},
! NullPointerException.class,
! "Should throw NPE");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
--- 365,375 ----
assertSame(map.get(EXTRA_KEY), null);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfPresentNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrowsNPE(() -> map.computeIfPresent(KEYS[1], null));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
*** 457,469 ****
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrows( () -> { map.compute(KEYS[1], null);},
! NullPointerException.class,
! "Should throw NPE");
}
@Test(dataProvider = "MergeCases")
private void testMerge(String description, Map<IntegerEnum, String> map, Merging.Value oldValue, Merging.Value newValue, Merging.Merger merger, Merging.Value put, Merging.Value result) {
// add and check initial conditions.
--- 456,466 ----
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeNullFunction(String description, Map<IntegerEnum, String> map) {
! assertThrowsNPE(() -> map.compute(KEYS[1], null));
}
@Test(dataProvider = "MergeCases")
private void testMerge(String description, Map<IntegerEnum, String> map, Merging.Value oldValue, Merging.Value newValue, Merging.Merger merger, Merging.Value put, Merging.Value result) {
// add and check initial conditions.
*** 529,541 ****
}
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testMergeNullMerger(String description, Map<IntegerEnum, String> map) {
! assertThrows( () -> { map.merge(KEYS[1], VALUES[1], null);},
! NullPointerException.class,
! "Should throw NPE");
}
/** A function that flipflops between running two other functions. */
static <T,U,V> BiFunction<T,U,V> twoStep(AtomicBoolean b,
BiFunction<T,U,V> first,
--- 526,536 ----
}
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testMergeNullMerger(String description, Map<IntegerEnum, String> map) {
! assertThrowsNPE(() -> map.merge(KEYS[1], VALUES[1], null));
}
/** A function that flipflops between running two other functions. */
static <T,U,V> BiFunction<T,U,V> twoStep(AtomicBoolean b,
BiFunction<T,U,V> first,
*** 971,1015 ****
}
return cases;
}
! public interface Thrower<T extends Throwable> {
!
! public void run() throws T;
! }
!
! public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
! assertThrows(thrower, throwable, null);
! }
!
! public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
! Throwable thrown;
! try {
! thrower.run();
! thrown = null;
! } catch (Throwable caught) {
! thrown = caught;
! }
!
! assertInstance(thrown, throwable,
! ((null != message) ? message : "") +
! " Failed to throw " + throwable.getCanonicalName());
! }
!
! public static <T extends Throwable> void assertThrows(Class<T> throwable, String message, Thrower<T>... throwers) {
! for (Thrower<T> thrower : throwers) {
! assertThrows(thrower, throwable, message);
! }
! }
!
! public static void assertInstance(Object actual, Class<?> expected) {
! assertInstance(expected.isInstance(actual), null);
! }
!
! public static void assertInstance(Object actual, Class<?> expected, String message) {
! assertTrue(expected.isInstance(actual), message);
}
/**
* A simple mutable map implementation that provides only default
* implementations of all methods. ie. none of the Map interface default
--- 966,977 ----
}
return cases;
}
! public static void assertThrowsNPE(ThrowingRunnable r) {
! assertThrows(NullPointerException.class, r);
}
/**
* A simple mutable map implementation that provides only default
* implementations of all methods. ie. none of the Map interface default
< prev index next >