From 295faa8f795f5824f802220a7887f5175cfaf747 Mon Sep 17 00:00:00 2001 From: Amy Lu Date: Thu, 4 May 2017 20:24:12 +0800 Subject: [PATCH] 8023897: Replace/update/rename executeAndCatch in various tests to assertThrows Reviewed-by: dfuchs, prappo, psandoz, rriggs --- jdk/test/java/util/Arrays/ParallelPrefix.java | 126 +++++------------- .../util/Arrays/StreamAndSpliterator.java | 28 ++-- .../util/BitSet/stream/BitSetStreamTest.java | 5 +- .../util/Collections/EmptyNavigableMap.java | 96 ++++++------- .../util/Collections/EmptyNavigableSet.java | 106 +++++++-------- .../Iterator/PrimitiveIteratorDefaults.java | 43 ++---- jdk/test/java/util/Map/Defaults.java | 80 +++-------- jdk/test/java/util/Random/RandomTest.java | 72 ++++------ .../Spliterator/SpliteratorCollisions.java | 8 +- .../Spliterator/SpliteratorFailFastTest.java | 43 ++---- ...SpliteratorTraversingAndSplittingTest.java | 16 +-- .../java/util/SpliteratorTestHelper.java | 35 ++--- 12 files changed, 236 insertions(+), 422 deletions(-) diff --git a/jdk/test/java/util/Arrays/ParallelPrefix.java b/jdk/test/java/util/Arrays/ParallelPrefix.java index 2b3fb70fca1..7772c6e4c75 100644 --- a/jdk/test/java/util/Arrays/ParallelPrefix.java +++ b/jdk/test/java/util/Arrays/ParallelPrefix.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -179,119 +179,65 @@ public class ParallelPrefix { @Test public void testNPEs() { // null array - assertThrows( () -> Arrays.parallelPrefix((int[]) null, Integer::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((long []) null, Long::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((double []) null, Double::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((String []) null, String::concat), - NullPointerException.class, "should throw NPE"); + assertThrowsNPE(() -> Arrays.parallelPrefix((int[]) null, Integer::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((long []) null, Long::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((double []) null, Double::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((String []) null, String::concat)); // null array w/ range - assertThrows( () -> Arrays.parallelPrefix((int[]) null, 0, 0, Integer::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((long []) null, 0, 0, Long::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((double []) null, 0, 0, Double::max), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix((String []) null, 0, 0, String::concat), - NullPointerException.class, "should throw NPE"); + assertThrowsNPE(() -> Arrays.parallelPrefix((int[]) null, 0, 0, Integer::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((long []) null, 0, 0, Long::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((double []) null, 0, 0, Double::max)); + assertThrowsNPE(() -> Arrays.parallelPrefix((String []) null, 0, 0, String::concat)); // null op - assertThrows( () -> Arrays.parallelPrefix(new int[] {}, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new long[] {}, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new double[] {}, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new String[] {}, null), - NullPointerException.class, "should throw NPE"); + assertThrowsNPE(() -> Arrays.parallelPrefix(new int[] {}, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new long[] {}, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new double[] {}, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new String[] {}, null)); // null op w/ range - assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 0, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 0, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 0, null), - NullPointerException.class, "should throw NPE"); - assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 0, null), - NullPointerException.class, "should throw NPE"); + assertThrowsNPE(() -> Arrays.parallelPrefix(new int[] {}, 0, 0, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new long[] {}, 0, 0, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new double[] {}, 0, 0, null)); + assertThrowsNPE(() -> Arrays.parallelPrefix(new String[] {}, 0, 0, null)); } @Test public void testIAEs() { - assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 1, 0, Integer::max), - IllegalArgumentException.class, "should throw IAE"); - assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 1, 0, Long::max), - IllegalArgumentException.class, "should throw IAE"); - assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 1, 0, Double::max), - IllegalArgumentException.class, "should throw IAE"); - assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 1, 0, String::concat), - IllegalArgumentException.class, "should throw IAE"); + assertThrowsIAE(() -> Arrays.parallelPrefix(new int[] {}, 1, 0, Integer::max)); + assertThrowsIAE(() -> Arrays.parallelPrefix(new long[] {}, 1, 0, Long::max)); + assertThrowsIAE(() -> Arrays.parallelPrefix(new double[] {}, 1, 0, Double::max)); + assertThrowsIAE(() -> Arrays.parallelPrefix(new String[] {}, 1, 0, String::concat)); } @Test - public void testAIOBEs() { + public void testAIOOBEs() { // bad "fromIndex" - assertThrows( () -> Arrays.parallelPrefix(new int[] {}, -1, 0, Integer::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new long[] {}, -1, 0, Long::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new double[] {}, -1, 0, Double::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new String[] {}, -1, 0, String::concat), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new int[] {}, -1, 0, Integer::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new long[] {}, -1, 0, Long::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new double[] {}, -1, 0, Double::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new String[] {}, -1, 0, String::concat)); // bad "toIndex" - assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 1, Integer::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 1, Long::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 1, Double::max), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); - assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 1, String::concat), - ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new int[] {}, 0, 1, Integer::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new long[] {}, 0, 1, Long::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new double[] {}, 0, 1, Double::max)); + assertThrowsAIOOB(() -> Arrays.parallelPrefix(new String[] {}, 0, 1, String::concat)); } // "library" code - public interface Thrower { - - public void run() throws T; + private void assertThrowsNPE(ThrowingRunnable r) { + assertThrows(NullPointerException.class, r); } - - public static void assertThrows(Thrower thrower, Class throwable) { - assertThrows(thrower, throwable, null); + private void assertThrowsIAE(ThrowingRunnable r) { + assertThrows(IllegalArgumentException.class, r); } - public static void assertThrows(Thrower thrower, Class 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 void assertThrows(Class throwable, String message, Thrower... throwers) { - for(Thrower 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); + private void assertThrowsAIOOB(ThrowingRunnable r) { + assertThrows(ArrayIndexOutOfBoundsException.class, r); } static void assertArraysEqual(int[] actual, int[] expected) { diff --git a/jdk/test/java/util/Arrays/StreamAndSpliterator.java b/jdk/test/java/util/Arrays/StreamAndSpliterator.java index b475e7e6f39..47994f14e7a 100644 --- a/jdk/test/java/util/Arrays/StreamAndSpliterator.java +++ b/jdk/test/java/util/Arrays/StreamAndSpliterator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -32,7 +32,9 @@ import org.testng.annotations.Test; import java.util.Arrays; import java.util.Spliterators; -import static org.testng.Assert.assertNotNull; +import org.testng.Assert.ThrowingRunnable; + +import static org.testng.Assert.assertThrows; public class StreamAndSpliterator { @Test @@ -124,25 +126,11 @@ public class StreamAndSpliterator { assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0)); } - void assertThrowsNPE(Runnable r) { - NullPointerException caught = null; - try { - r.run(); - } - catch (NullPointerException e) { - caught = e; - } - assertNotNull(caught, "NullPointerException not thrown"); + void assertThrowsNPE(ThrowingRunnable r) { + assertThrows(NullPointerException.class, r); } - void assertThrowsAIOOB(Runnable r) { - ArrayIndexOutOfBoundsException caught = null; - try { - r.run(); - } - catch (ArrayIndexOutOfBoundsException e) { - caught = e; - } - assertNotNull(caught, "ArrayIndexOutOfBoundsException not thrown"); + void assertThrowsAIOOB(ThrowingRunnable r) { + assertThrows(ArrayIndexOutOfBoundsException.class, r); } } diff --git a/jdk/test/java/util/BitSet/stream/BitSetStreamTest.java b/jdk/test/java/util/BitSet/stream/BitSetStreamTest.java index 871b31fe5ac..e6bf02715fe 100644 --- a/jdk/test/java/util/BitSet/stream/BitSetStreamTest.java +++ b/jdk/test/java/util/BitSet/stream/BitSetStreamTest.java @@ -42,6 +42,7 @@ import static java.util.stream.Collectors.toList; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertThrows; import static org.testng.Assert.assertTrue; /** @@ -167,8 +168,8 @@ public class BitSetStreamTest extends SpliteratorTestHelper { @Test(dataProvider = "BitSet.stream.spliterator") public void testIntNullPointerException(String description, Collection exp, Supplier s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining((IntConsumer) null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance((IntConsumer) null)); + assertThrows(NullPointerException.class, () -> s.get().forEachRemaining((IntConsumer) null)); + assertThrows(NullPointerException.class, () -> s.get().tryAdvance((IntConsumer) null)); } @Test(dataProvider = "BitSet.stream.spliterator") diff --git a/jdk/test/java/util/Collections/EmptyNavigableMap.java b/jdk/test/java/util/Collections/EmptyNavigableMap.java index 359d30f4ab6..e874d32f595 100644 --- a/jdk/test/java/util/Collections/EmptyNavigableMap.java +++ b/jdk/test/java/util/Collections/EmptyNavigableMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -36,10 +36,12 @@ import java.util.Iterator; import java.util.NavigableMap; import java.util.SortedMap; import java.util.TreeMap; + +import org.testng.Assert; +import org.testng.Assert.ThrowingRunnable; import org.testng.annotations.Test; import org.testng.annotations.DataProvider; -import static org.testng.Assert.fail; import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertFalse; @@ -65,26 +67,27 @@ public class EmptyNavigableMap { ((null != message) ? message : "") + " Not empty. "); } - public interface Thrower { - - public void run() throws T; - } - - public static void assertThrows(Thrower thrower, Class throwable) { - assertThrows(thrower, throwable, null); - } - - public static void assertThrows(Thrower thrower, Class throwable, String message) { - Throwable result; + private void assertThrows(Class throwableClass, + ThrowingRunnable runnable, + String message) { try { - thrower.run(); - fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); - return; - } catch (Throwable caught) { - result = caught; + Assert.assertThrows(throwableClass, runnable); + } catch (AssertionError e) { + throw new AssertionError(String.format("%s%n%s", + ((null != message) ? message : ""), e.getMessage()), e); } + } - assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); + 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) { @@ -121,10 +124,9 @@ public class EmptyNavigableMap { */ @Test(dataProvider = "NavigableMap", dataProviderClass = EmptyNavigableMap.class) public void testContainsRequiresComparable(String description, NavigableMap navigableMap) { - assertThrows(() -> { + assertThrowsCCE(() -> { navigableMap.containsKey(new Object()); }, - ClassCastException.class, description + ": Compareable should be required"); } @@ -175,14 +177,12 @@ public class EmptyNavigableMap { */ @Test(dataProvider = "NavigableMap", dataProviderClass = EmptyNavigableMap.class) public void testHeadMap(String description, NavigableMap navigableMap) { - assertThrows( + assertThrowsNPE( () -> { NavigableMap ss = navigableMap.headMap(null, false); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsCCE( () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, - ClassCastException.class, description + ": Must throw ClassCastException for non-Comparable element"); NavigableMap ss = navigableMap.headMap("1", false); @@ -203,50 +203,44 @@ public class EmptyNavigableMap { */ @Test(dataProvider = "NavigableMap", dataProviderClass = EmptyNavigableMap.class) public void testSubMap(String description, NavigableMap navigableMap) { - assertThrows( + assertThrowsNPE( () -> { SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsNPE( () -> { SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsNPE( () -> { SortedMap ss = navigableMap.subMap(null, null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); Object obj1 = new Object(); Object obj2 = new Object(); - assertThrows( + assertThrowsCCE( () -> { SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); - assertThrows( + assertThrowsCCE( () -> { SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); - assertThrows( + assertThrowsCCE( () -> { SortedMap ss = navigableMap.subMap(obj1, obj2); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); // minimal range navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); @@ -257,12 +251,11 @@ public class EmptyNavigableMap { Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; - assertThrows( + assertThrowsIAE( () -> { navigableMap.subMap(last, true, first, false); }, - IllegalArgumentException.class, description - + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); + description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); navigableMap.subMap(first, true, last, false); } @@ -280,10 +273,9 @@ public class EmptyNavigableMap { // slightly smaller NavigableMap ns = subMap.subMap(first, false, last, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.subMap(first, true, last, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -301,10 +293,9 @@ public class EmptyNavigableMap { NavigableMap ns = subMap.headMap(BigInteger.ONE, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.headMap(BigInteger.ONE, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -322,10 +313,9 @@ public class EmptyNavigableMap { NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.tailMap(BigInteger.ONE, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -337,15 +327,15 @@ public class EmptyNavigableMap { */ @Test(dataProvider = "NavigableMap", dataProviderClass = EmptyNavigableMap.class) public void testTailMap(String description, NavigableMap navigableMap) { - assertThrows(() -> { + assertThrowsNPE(() -> { navigableMap.tailMap(null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows(() -> { + assertThrowsCCE(() -> { navigableMap.tailMap(new Object()); - }, ClassCastException.class); + }, + description); NavigableMap ss = navigableMap.tailMap("1", true); diff --git a/jdk/test/java/util/Collections/EmptyNavigableSet.java b/jdk/test/java/util/Collections/EmptyNavigableSet.java index afe216da8d7..7ee13b80c1e 100644 --- a/jdk/test/java/util/Collections/EmptyNavigableSet.java +++ b/jdk/test/java/util/Collections/EmptyNavigableSet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -37,10 +37,12 @@ import java.util.NoSuchElementException; import java.util.NavigableSet; import java.util.SortedSet; import java.util.TreeSet; + +import org.testng.Assert; +import org.testng.Assert.ThrowingRunnable; import org.testng.annotations.Test; import org.testng.annotations.DataProvider; -import static org.testng.Assert.fail; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; @@ -67,26 +69,31 @@ public class EmptyNavigableSet { ((null != message) ? message : "") + " Not empty. "); } - public interface Thrower { - - public void run() throws T; - } - - public static void assertThrows(Thrower thrower, Class throwable) { - assertThrows(thrower, throwable, null); - } - - public static void assertThrows(Thrower thrower, Class throwable, String message) { - Throwable result; + private void assertThrows(Class throwableClass, + ThrowingRunnable runnable, + String message) { try { - thrower.run(); - fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); - return; - } catch (Throwable caught) { - result = caught; + Assert.assertThrows(throwableClass, runnable); + } catch (AssertionError e) { + throw new AssertionError(String.format("%s%n%s", + ((null != message) ? message : ""), e.getMessage()), e); } + } - assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); + 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); + } + + private void assertThrowsNSEE(ThrowingRunnable r, String s) { + assertThrows(NoSuchElementException.class, r, s); } public static final boolean isDescending(SortedSet set) { @@ -123,10 +130,9 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testContainsRequiresComparable(String description, NavigableSet navigableSet) { - assertThrows(() -> { + assertThrowsCCE(() -> { navigableSet.contains(new Object()); }, - ClassCastException.class, description + ": Compareable should be required"); } @@ -176,9 +182,9 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testFirst(String description, NavigableSet navigableSet) { - assertThrows(() -> { + assertThrowsNSEE(() -> { navigableSet.first(); - }, NoSuchElementException.class, description); + }, description); } /** @@ -186,14 +192,12 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testHeadSet(String description, NavigableSet navigableSet) { - assertThrows( + assertThrowsNPE( () -> { NavigableSet ns = navigableSet.headSet(null, false); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsCCE( () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); }, - ClassCastException.class, description + ": Must throw ClassCastException for non-Comparable element"); NavigableSet ns = navigableSet.headSet("1", false); @@ -206,9 +210,9 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testLast(String description, NavigableSet navigableSet) { - assertThrows(() -> { + assertThrowsNSEE(() -> { navigableSet.last(); - }, NoSuchElementException.class, description); + }, description); } /** @@ -224,50 +228,44 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testSubSet(String description, NavigableSet navigableSet) { - assertThrows( + assertThrowsNPE( () -> { SortedSet ss = navigableSet.subSet(null, BigInteger.TEN); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsNPE( () -> { SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows( + assertThrowsNPE( () -> { SortedSet ss = navigableSet.subSet(null, null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); Object obj1 = new Object(); Object obj2 = new Object(); - assertThrows( + assertThrowsCCE( () -> { SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); - assertThrows( + assertThrowsCCE( () -> { SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); - assertThrows( + assertThrowsCCE( () -> { SortedSet ss = navigableSet.subSet(obj1, obj2); }, - ClassCastException.class, description - + ": Must throw ClassCastException for parameter which is not Comparable."); + description + ": Must throw ClassCastException for parameter which is not Comparable."); // minimal range navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false); @@ -278,11 +276,11 @@ public class EmptyNavigableSet { Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO; Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; - assertThrows( + assertThrowsIAE( () -> { navigableSet.subSet(last, true, first, false); }, - IllegalArgumentException.class, description + description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); navigableSet.subSet(first, true, last, false); @@ -301,10 +299,9 @@ public class EmptyNavigableSet { // slightly smaller NavigableSet ns = subSet.subSet(first, false, last, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.subSet(first, true, last, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -322,10 +319,9 @@ public class EmptyNavigableSet { NavigableSet ns = subSet.headSet(BigInteger.ONE, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.headSet(BigInteger.ONE, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -343,10 +339,9 @@ public class EmptyNavigableSet { NavigableSet ns = subSet.tailSet(BigInteger.ONE, false); // slight expansion - assertThrows(() -> { + assertThrowsIAE(() -> { ns.tailSet(BigInteger.ONE, true); }, - IllegalArgumentException.class, description + ": Expansion should not be allowed"); // much smaller @@ -358,15 +353,14 @@ public class EmptyNavigableSet { */ @Test(dataProvider = "NavigableSet", dataProviderClass = EmptyNavigableSet.class) public void testTailSet(String description, NavigableSet navigableSet) { - assertThrows(() -> { + assertThrowsNPE(() -> { navigableSet.tailSet(null); }, - NullPointerException.class, description + ": Must throw NullPointerException for null element"); - assertThrows(() -> { + assertThrowsCCE(() -> { navigableSet.tailSet(new Object()); - }, ClassCastException.class); + }, description); NavigableSet ss = navigableSet.tailSet("1", true); diff --git a/jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java b/jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java index 2880578b194..a94323d7e2c 100644 --- a/jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java +++ b/jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -21,16 +21,16 @@ * questions. */ -import org.testng.annotations.Test; - import java.util.PrimitiveIterator; import java.util.function.Consumer; import java.util.function.DoubleConsumer; import java.util.function.IntConsumer; import java.util.function.LongConsumer; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import org.testng.Assert.ThrowingRunnable; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertThrows; /** * @test @@ -53,8 +53,8 @@ public class PrimitiveIteratorDefaults { } }; - executeAndCatch(() -> i.forEachRemaining((IntConsumer) null)); - executeAndCatch(() -> i.forEachRemaining((Consumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((IntConsumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((Consumer) null)); } public void testLongForEachRemainingWithNull() { @@ -70,8 +70,8 @@ public class PrimitiveIteratorDefaults { } }; - executeAndCatch(() -> i.forEachRemaining((LongConsumer) null)); - executeAndCatch(() -> i.forEachRemaining((Consumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((LongConsumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((Consumer) null)); } public void testDoubleForEachRemainingWithNull() { @@ -87,29 +87,12 @@ public class PrimitiveIteratorDefaults { } }; - executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null)); - executeAndCatch(() -> i.forEachRemaining((Consumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((DoubleConsumer) null)); + assertThrowsNPE(() -> i.forEachRemaining((Consumer) null)); } - private void executeAndCatch(Runnable r) { - executeAndCatch(NullPointerException.class, r); - } - - private void executeAndCatch(Class expected, Runnable r) { - Exception caught = null; - try { - r.run(); - } - catch (Exception e) { - caught = e; - } - - assertNotNull(caught, - String.format("No Exception was thrown, expected an Exception of %s to be thrown", - expected.getName())); - assertTrue(expected.isInstance(caught), - String.format("Exception thrown %s not an instance of %s", - caught.getClass().getName(), expected.getName())); + private void assertThrowsNPE(ThrowingRunnable r) { + assertThrows(NullPointerException.class, r); } } diff --git a/jdk/test/java/util/Map/Defaults.java b/jdk/test/java/util/Map/Defaults.java index 7b2956f8846..02292ad1b04 100644 --- a/jdk/test/java/util/Map/Defaults.java +++ b/jdk/test/java/util/Map/Defaults.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -53,15 +53,19 @@ 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 { @@ -159,14 +163,8 @@ public class Defaults { @Test(dataProvider = "Map rw=true keys=nonNull values=nonNull") public static void testReplaceAllNoNullReplacement(String description, Map map) { - assertThrows( - () -> { map.replaceAll(null); }, - NullPointerException.class, - description); - assertThrows( - () -> { map.replaceAll((k,v) -> null); }, - NullPointerException.class, - description + " should not allow replacement with null value"); + assertThrowsNPE(() -> map.replaceAll(null)); + assertThrowsNPE(() -> map.replaceAll((k,v) -> null)); //should not allow replacement with null value } @Test(dataProvider = "Map rw=true keys=withNull values=withNull") @@ -209,7 +207,7 @@ public class Defaults { public void testReplaceKVNoNulls(String description, Map 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"); + 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"); } @@ -248,8 +246,13 @@ public class Defaults { public void testReplaceKVVNoNulls(String description, Map 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"); + 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"); } @@ -319,9 +322,7 @@ public class Defaults { @Test(dataProvider = "Map rw=true keys=all values=all") public void testComputeIfAbsentNullFunction(String description, Map map) { - assertThrows( () -> { map.computeIfAbsent(KEYS[1], null);}, - NullPointerException.class, - "Should throw NPE"); + assertThrowsNPE(() -> map.computeIfAbsent(KEYS[1], null)); } @Test(dataProvider = "Map rw=true keys=withNull values=withNull") @@ -366,9 +367,7 @@ public class Defaults { @Test(dataProvider = "Map rw=true keys=all values=all") public void testComputeIfPresentNullFunction(String description, Map map) { - assertThrows( () -> { map.computeIfPresent(KEYS[1], null);}, - NullPointerException.class, - "Should throw NPE"); + assertThrowsNPE(() -> map.computeIfPresent(KEYS[1], null)); } @Test(dataProvider = "Map rw=true keys=withNull values=withNull") @@ -459,9 +458,7 @@ public class Defaults { @Test(dataProvider = "Map rw=true keys=all values=all") public void testComputeNullFunction(String description, Map map) { - assertThrows( () -> { map.compute(KEYS[1], null);}, - NullPointerException.class, - "Should throw NPE"); + assertThrowsNPE(() -> map.compute(KEYS[1], null)); } @Test(dataProvider = "MergeCases") @@ -531,9 +528,7 @@ public class Defaults { @Test(dataProvider = "Map rw=true keys=all values=all") public void testMergeNullMerger(String description, Map map) { - assertThrows( () -> { map.merge(KEYS[1], VALUES[1], null);}, - NullPointerException.class, - "Should throw NPE"); + assertThrowsNPE(() -> map.merge(KEYS[1], VALUES[1], null)); } /** A function that flipflops between running two other functions. */ @@ -973,41 +968,8 @@ public class Defaults { return cases; } - public interface Thrower { - - public void run() throws T; - } - - public static void assertThrows(Thrower thrower, Class throwable) { - assertThrows(thrower, throwable, null); - } - - public static void assertThrows(Thrower thrower, Class 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 void assertThrows(Class throwable, String message, Thrower... throwers) { - for (Thrower 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); + public static void assertThrowsNPE(ThrowingRunnable r) { + assertThrows(NullPointerException.class, r); } /** diff --git a/jdk/test/java/util/Random/RandomTest.java b/jdk/test/java/util/Random/RandomTest.java index 3055f5cbbcb..6b16e5b6b7e 100644 --- a/jdk/test/java/util/Random/RandomTest.java +++ b/jdk/test/java/util/Random/RandomTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 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 @@ -21,14 +21,13 @@ * questions. */ -import org.testng.Assert; -import org.testng.annotations.Test; - import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; import java.util.function.BiConsumer; +import org.testng.annotations.Test; + import static org.testng.Assert.*; /** @@ -172,12 +171,12 @@ public class RandomTest { */ public void testBadStreamSize() { Random r = new Random(); - executeAndCatchIAE(() -> r.ints(-1L)); - executeAndCatchIAE(() -> r.ints(-1L, 2, 3)); - executeAndCatchIAE(() -> r.longs(-1L)); - executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L)); - executeAndCatchIAE(() -> r.doubles(-1L)); - executeAndCatchIAE(() -> r.doubles(-1L, .5, .6)); + assertThrowsIAE(() -> r.ints(-1L)); + assertThrowsIAE(() -> r.ints(-1L, 2, 3)); + assertThrowsIAE(() -> r.longs(-1L)); + assertThrowsIAE(() -> r.longs(-1L, -1L, 1L)); + assertThrowsIAE(() -> r.doubles(-1L)); + assertThrowsIAE(() -> r.doubles(-1L, .5, .6)); } /** @@ -186,10 +185,10 @@ public class RandomTest { */ public void testBadStreamBounds() { Random r = new Random(); - executeAndCatchIAE(() -> r.ints(2, 1)); - executeAndCatchIAE(() -> r.ints(10, 42, 42)); - executeAndCatchIAE(() -> r.longs(-1L, -1L)); - executeAndCatchIAE(() -> r.longs(10, 1L, -2L)); + assertThrowsIAE(() -> r.ints(2, 1)); + assertThrowsIAE(() -> r.ints(10, 42, 42)); + assertThrowsIAE(() -> r.longs(-1L, -1L)); + assertThrowsIAE(() -> r.longs(10, 1L, -2L)); testDoubleBadOriginBound((o, b) -> r.doubles(10, o, b)); } @@ -198,45 +197,28 @@ public class RandomTest { static final double FINITE = Math.PI; void testDoubleBadOriginBound(BiConsumer bi) { - executeAndCatchIAE(() -> bi.accept(17.0, 2.0)); - executeAndCatchIAE(() -> bi.accept(0.0, 0.0)); - executeAndCatchIAE(() -> bi.accept(Double.NaN, FINITE)); - executeAndCatchIAE(() -> bi.accept(FINITE, Double.NaN)); - executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)); + assertThrowsIAE(() -> bi.accept(17.0, 2.0)); + assertThrowsIAE(() -> bi.accept(0.0, 0.0)); + assertThrowsIAE(() -> bi.accept(Double.NaN, FINITE)); + assertThrowsIAE(() -> bi.accept(FINITE, Double.NaN)); + assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)); // Returns NaN -// executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, FINITE)); -// executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); +// assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, FINITE)); +// assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); - executeAndCatchIAE(() -> bi.accept(FINITE, Double.NEGATIVE_INFINITY)); + assertThrowsIAE(() -> bi.accept(FINITE, Double.NEGATIVE_INFINITY)); // Returns Double.MAX_VALUE -// executeAndCatchIAE(() -> bi.accept(FINITE, Double.POSITIVE_INFINITY)); +// assertThrowsIAE(() -> bi.accept(FINITE, Double.POSITIVE_INFINITY)); - executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); - executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, FINITE)); - executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); + assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); + assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, FINITE)); + assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); } - private void executeAndCatchIAE(Runnable r) { - executeAndCatch(IllegalArgumentException.class, r); - } - - private void executeAndCatch(Class expected, Runnable r) { - Exception caught = null; - try { - r.run(); - } - catch (Exception e) { - caught = e; - } - - assertNotNull(caught, - String.format("No Exception was thrown, expected an Exception of %s to be thrown", - expected.getName())); - Assert.assertTrue(expected.isInstance(caught), - String.format("Exception thrown %s not an instance of %s", - caught.getClass().getName(), expected.getName())); + private void assertThrowsIAE(ThrowingRunnable r) { + assertThrows(IllegalArgumentException.class, r); } /** diff --git a/jdk/test/java/util/Spliterator/SpliteratorCollisions.java b/jdk/test/java/util/Spliterator/SpliteratorCollisions.java index 43c0fc6da85..da9de48f705 100644 --- a/jdk/test/java/util/Spliterator/SpliteratorCollisions.java +++ b/jdk/test/java/util/Spliterator/SpliteratorCollisions.java @@ -212,16 +212,16 @@ public class SpliteratorCollisions extends SpliteratorTestHelper { void testNullPointerException(String description, Collection exp, Supplier> s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining(null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance(null)); + assertThrowsNPE(() -> s.get().forEachRemaining(null)); + assertThrowsNPE(() -> s.get().tryAdvance(null)); } @Test(dataProvider = "HashableIntSpliteratorWithNull") void testNullPointerExceptionWithNull(String description, Collection exp, Supplier> s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining(null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance(null)); + assertThrowsNPE(() -> s.get().forEachRemaining(null)); + assertThrowsNPE(() -> s.get().tryAdvance(null)); } diff --git a/jdk/test/java/util/Spliterator/SpliteratorFailFastTest.java b/jdk/test/java/util/Spliterator/SpliteratorFailFastTest.java index a0e71d24921..84d6ca9d6ab 100644 --- a/jdk/test/java/util/Spliterator/SpliteratorFailFastTest.java +++ b/jdk/test/java/util/Spliterator/SpliteratorFailFastTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -21,9 +21,6 @@ * questions. */ -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import java.util.ArrayList; import java.util.Arrays; import java.util.ConcurrentModificationException; @@ -42,8 +39,11 @@ import java.util.Vector; import java.util.WeakHashMap; import java.util.function.Supplier; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import org.testng.Assert.ThrowingRunnable; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertThrows; /** * @test @@ -125,7 +125,7 @@ public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelpe }); source.update(); - executeAndCatch(() -> s.tryAdvance(e -> { + assertThrowsCME(() -> s.tryAdvance(e -> { })); } @@ -137,7 +137,7 @@ public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelpe }); source.update(); - executeAndCatch(() -> s.forEachRemaining(e -> { + assertThrowsCME(() -> s.forEachRemaining(e -> { })); } } @@ -147,7 +147,7 @@ public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelpe Source source = ss.get(); Spliterator s = source.spliterator(); - executeAndCatch(() -> s.forEachRemaining(e -> { + assertThrowsCME(() -> s.forEachRemaining(e -> { source.update(); })); } @@ -161,7 +161,7 @@ public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelpe s.estimateSize(); source.update(); - executeAndCatch(() -> s.tryAdvance(e -> { + assertThrowsCME(() -> s.tryAdvance(e -> { })); } @@ -172,30 +172,13 @@ public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelpe s.estimateSize(); source.update(); - executeAndCatch(() -> s.forEachRemaining(e -> { + assertThrowsCME(() -> s.forEachRemaining(e -> { })); } } - private void executeAndCatch(Runnable r) { - executeAndCatch(ConcurrentModificationException.class, r); - } - - private void executeAndCatch(Class expected, Runnable r) { - Exception caught = null; - try { - r.run(); - } - catch (Exception e) { - caught = e; - } - - assertNotNull(caught, - String.format("No Exception was thrown, expected an Exception of %s to be thrown", - expected.getName())); - assertTrue(expected.isInstance(caught), - String.format("Exception thrown %s not an instance of %s", - caught.getClass().getName(), expected.getName())); + private void assertThrowsCME(ThrowingRunnable r) { + assertThrows(ConcurrentModificationException.class, r); } } diff --git a/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java b/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java index 3376a06db28..ca45ab758bc 100644 --- a/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java +++ b/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java @@ -666,8 +666,8 @@ public class SpliteratorTraversingAndSplittingTest extends SpliteratorTestHelper @Test(dataProvider = "Spliterator") public void testNullPointerException(String description, Collection exp, Supplier> s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining(null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance(null)); + assertThrowsNPE(() -> s.get().forEachRemaining(null)); + assertThrowsNPE(() -> s.get().tryAdvance(null)); } @Test(dataProvider = "Spliterator") @@ -866,8 +866,8 @@ public class SpliteratorTraversingAndSplittingTest extends SpliteratorTestHelper @Test(dataProvider = "Spliterator.OfInt") public void testIntNullPointerException(String description, Collection exp, Supplier s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining((IntConsumer) null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance((IntConsumer) null)); + assertThrowsNPE(() -> s.get().forEachRemaining((IntConsumer) null)); + assertThrowsNPE(() -> s.get().tryAdvance((IntConsumer) null)); } @Test(dataProvider = "Spliterator.OfInt") @@ -1009,8 +1009,8 @@ public class SpliteratorTraversingAndSplittingTest extends SpliteratorTestHelper @Test(dataProvider = "Spliterator.OfLong") public void testLongNullPointerException(String description, Collection exp, Supplier s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining((LongConsumer) null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance((LongConsumer) null)); + assertThrowsNPE(() -> s.get().forEachRemaining((LongConsumer) null)); + assertThrowsNPE(() -> s.get().tryAdvance((LongConsumer) null)); } @Test(dataProvider = "Spliterator.OfLong") @@ -1152,8 +1152,8 @@ public class SpliteratorTraversingAndSplittingTest extends SpliteratorTestHelper @Test(dataProvider = "Spliterator.OfDouble") public void testDoubleNullPointerException(String description, Collection exp, Supplier s) { - executeAndCatch(NullPointerException.class, () -> s.get().forEachRemaining((DoubleConsumer) null)); - executeAndCatch(NullPointerException.class, () -> s.get().tryAdvance((DoubleConsumer) null)); + assertThrowsNPE(() -> s.get().forEachRemaining((DoubleConsumer) null)); + assertThrowsNPE(() -> s.get().tryAdvance((DoubleConsumer) null)); } @Test(dataProvider = "Spliterator.OfDouble") diff --git a/jdk/test/java/util/stream/bootlib/java.base/java/util/SpliteratorTestHelper.java b/jdk/test/java/util/stream/bootlib/java.base/java/util/SpliteratorTestHelper.java index 087658431e8..8bbf20d7901 100644 --- a/jdk/test/java/util/stream/bootlib/java.base/java/util/SpliteratorTestHelper.java +++ b/jdk/test/java/util/stream/bootlib/java.base/java/util/SpliteratorTestHelper.java @@ -26,8 +26,6 @@ import java.util.function.*; import java.util.stream.LambdaTestHelpers; import static org.testng.Assert.*; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; /** * Assertion methods for spliterators, to be called from other tests @@ -224,22 +222,22 @@ public class SpliteratorTestHelper { // directly test the primitive methods if (sp instanceof Spliterator.OfInt) { Spliterator.OfInt psp = (Spliterator.OfInt) sp; - executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((IntConsumer) null)); - executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((IntConsumer) null)); + assertThrowsNPE(() -> psp.forEachRemaining((IntConsumer) null)); + assertThrowsNPE(() -> psp.tryAdvance((IntConsumer) null)); } else if (sp instanceof Spliterator.OfLong) { Spliterator.OfLong psp = (Spliterator.OfLong) sp; - executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((LongConsumer) null)); - executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((LongConsumer) null)); + assertThrowsNPE(() -> psp.forEachRemaining((LongConsumer) null)); + assertThrowsNPE(() -> psp.tryAdvance((LongConsumer) null)); } else if (sp instanceof Spliterator.OfDouble) { Spliterator.OfDouble psp = (Spliterator.OfDouble) sp; - executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((DoubleConsumer) null)); - executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((DoubleConsumer) null)); + assertThrowsNPE(() -> psp.forEachRemaining((DoubleConsumer) null)); + assertThrowsNPE(() -> psp.tryAdvance((DoubleConsumer) null)); } else { - executeAndCatch(NullPointerException.class, () -> sp.forEachRemaining(null)); - executeAndCatch(NullPointerException.class, () -> sp.tryAdvance(null)); + assertThrowsNPE(() -> sp.forEachRemaining(null)); + assertThrowsNPE(() -> sp.tryAdvance(null)); } } @@ -653,21 +651,8 @@ public class SpliteratorTestHelper { } } - public static void executeAndCatch(Class expected, Runnable r) { - Exception caught = null; - try { - r.run(); - } - catch (Exception e) { - caught = e; - } - - assertNotNull(caught, - String.format("No Exception was thrown, expected an Exception of %s to be thrown", - expected.getName())); - assertTrue(expected.isInstance(caught), - String.format("Exception thrown %s not an instance of %s", - caught.getClass().getName(), expected.getName())); + public static void assertThrowsNPE(ThrowingRunnable r) { + assertThrows(NullPointerException.class, r); } public static void mixedTraverseAndSplit(Consumer b, Spliterator splTop) {