jdk/test/java/util/Iterator/PrimitiveIteratorDefaults.java
author kvn
Thu, 05 Dec 2013 15:13:12 -0800
changeset 22858 f4a6f0eba875
parent 17930 8e80bd9fda30
child 45006 1c15ce5f636c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17930
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     1
/*
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     4
 *
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     8
 *
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    13
 * accompanied this code).
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    14
 *
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    18
 *
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    21
 * questions.
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    22
 */
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    23
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    24
import org.testng.annotations.Test;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    25
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    26
import java.util.PrimitiveIterator;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    27
import java.util.function.Consumer;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    28
import java.util.function.DoubleConsumer;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    29
import java.util.function.IntConsumer;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    30
import java.util.function.LongConsumer;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    31
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    32
import static org.testng.Assert.assertNotNull;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    33
import static org.testng.Assert.assertTrue;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    34
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    35
/**
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    36
 * @test
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    37
 * @run testng PrimitiveIteratorDefaults
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    38
 * @summary test default methods on PrimitiveIterator
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    39
 */
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    40
@Test
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    41
public class PrimitiveIteratorDefaults {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    42
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    43
    public void testIntForEachRemainingWithNull() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    44
        PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    45
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    46
            public int nextInt() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    47
                return 0;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    48
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    49
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    50
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    51
            public boolean hasNext() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    52
                return false;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    53
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    54
        };
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    55
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    56
        executeAndCatch(() -> i.forEachRemaining((IntConsumer) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    57
        executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    58
    }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    59
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    60
    public void testLongForEachRemainingWithNull() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    61
        PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    62
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    63
            public long nextLong() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    64
                return 0;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    65
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    66
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    67
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    68
            public boolean hasNext() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    69
                return false;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    70
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    71
        };
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    72
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    73
        executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    74
        executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    75
    }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    76
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    77
    public void testDoubleForEachRemainingWithNull() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    78
        PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    79
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    80
            public double nextDouble() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    81
                return 0;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    82
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    83
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    84
            @Override
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    85
            public boolean hasNext() {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    86
                return false;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    87
            }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    88
        };
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    89
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    90
        executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    91
        executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    92
    }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    93
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    94
    private void executeAndCatch(Runnable r) {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    95
        executeAndCatch(NullPointerException.class, r);
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    96
    }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    97
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    98
    private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
    99
        Exception caught = null;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   100
        try {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   101
            r.run();
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   102
        }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   103
        catch (Exception e) {
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   104
            caught = e;
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   105
        }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   106
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   107
        assertNotNull(caught,
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   108
                      String.format("No Exception was thrown, expected an Exception of %s to be thrown",
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   109
                                    expected.getName()));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   110
        assertTrue(expected.isInstance(caught),
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   111
                   String.format("Exception thrown %s not an instance of %s",
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   112
                                 caught.getClass().getName(), expected.getName()));
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   113
    }
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   114
8e80bd9fda30 8015008: Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE
psandoz
parents:
diff changeset
   115
}