< prev index next >
test/java/util/Iterator/PrimitiveIteratorDefaults.java
Print this page
@@ -1,7 +1,7 @@
/*
- * 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -19,20 +19,20 @@
* 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.
*/
-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
* @run testng PrimitiveIteratorDefaults
* @summary test default methods on PrimitiveIterator
@@ -51,12 +51,12 @@
public boolean hasNext() {
return false;
}
};
- executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));
- executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));
+ assertThrowsNPE(() -> i.forEachRemaining((IntConsumer) null));
+ assertThrowsNPE(() -> i.forEachRemaining((Consumer<Integer>) null));
}
public void testLongForEachRemainingWithNull() {
PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
@Override
@@ -68,12 +68,12 @@
public boolean hasNext() {
return false;
}
};
- executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
- executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
+ assertThrowsNPE(() -> i.forEachRemaining((LongConsumer) null));
+ assertThrowsNPE(() -> i.forEachRemaining((Consumer<Long>) null));
}
public void testDoubleForEachRemainingWithNull() {
PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
@Override
@@ -85,31 +85,14 @@
public boolean hasNext() {
return false;
}
};
- executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
- executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
- }
-
- private void executeAndCatch(Runnable r) {
- executeAndCatch(NullPointerException.class, r);
- }
-
- private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {
- Exception caught = null;
- try {
- r.run();
- }
- catch (Exception e) {
- caught = e;
+ assertThrowsNPE(() -> i.forEachRemaining((DoubleConsumer) null));
+ assertThrowsNPE(() -> i.forEachRemaining((Consumer<Double>) null));
}
- 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);
}
}
< prev index next >