Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java
38812 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.Test;2425import java.util.PrimitiveIterator;26import java.util.function.Consumer;27import java.util.function.DoubleConsumer;28import java.util.function.IntConsumer;29import java.util.function.LongConsumer;3031import static org.testng.Assert.assertNotNull;32import static org.testng.Assert.assertTrue;3334/**35* @test36* @run testng PrimitiveIteratorDefaults37* @summary test default methods on PrimitiveIterator38*/39@Test40public class PrimitiveIteratorDefaults {4142public void testIntForEachRemainingWithNull() {43PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {44@Override45public int nextInt() {46return 0;47}4849@Override50public boolean hasNext() {51return false;52}53};5455executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));56executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));57}5859public void testLongForEachRemainingWithNull() {60PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {61@Override62public long nextLong() {63return 0;64}6566@Override67public boolean hasNext() {68return false;69}70};7172executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));73executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));74}7576public void testDoubleForEachRemainingWithNull() {77PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {78@Override79public double nextDouble() {80return 0;81}8283@Override84public boolean hasNext() {85return false;86}87};8889executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));90executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));91}9293private void executeAndCatch(Runnable r) {94executeAndCatch(NullPointerException.class, r);95}9697private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {98Exception caught = null;99try {100r.run();101}102catch (Exception e) {103caught = e;104}105106assertNotNull(caught,107String.format("No Exception was thrown, expected an Exception of %s to be thrown",108expected.getName()));109assertTrue(expected.isInstance(caught),110String.format("Exception thrown %s not an instance of %s",111caught.getClass().getName(), expected.getName()));112}113114}115116117