1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.openjdk.tests.java.util;
24
25import org.testng.annotations.Test;
26
27import java.util.Arrays;
28import java.util.function.Consumer;
29
30import static org.testng.Assert.fail;
31
32/**
33 * NullArgsTestCase -- Given a Consumer&ltObject[]&gt, and an Object[] array of args, call the block with the args,
34 * assert success, and then call the consumer N times, each time setting one of the args to null, and assert that
35 * all these throw NPE.
36 *
37 * Typically this would be combined with a DataProvider that serves up combinations of things to be tested, as in
38 * IteratorsNullTest.
39 */
40public abstract class NullArgsTestCase {
41    public final String name;
42    public final Consumer<Object[]> sink;
43    public final Object[] args;
44
45    protected NullArgsTestCase(String name, Consumer<Object[]> sink, Object[] args) {
46        this.name = name;
47        this.sink = sink;
48        this.args = args;
49    }
50
51    @Test
52    public void goodNonNull() {
53        sink.accept(args);
54    }
55
56    @Test
57    public void throwWithNull() {
58        for (int i=0; i<args.length; i++) {
59            Object[] temp = Arrays.copyOf(args, args.length);
60            temp[i] = null;
61            try {
62                sink.accept(temp);
63                fail(String.format("Expected NullPointerException for argument %d of test case %s", i, name));
64            }
65            catch (NullPointerException e) {
66                // Success
67            }
68        }
69    }
70}
71