1/*
2 * Copyright (c) 2016, 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 */
23
24import java.io.IOException;
25import java.net.MalformedURLException;
26import java.net.URL;
27import java.net.URLClassLoader;
28import java.nio.file.Paths;
29import java.util.Enumeration;
30import java.util.stream.Stream;
31import org.testng.annotations.*;
32
33/*
34 * @test
35 * @bug 8136831
36 * @summary Test null argument to ClassLoader.getResourceXXXX()
37 * @run testng GetResourceNullArg
38 */
39
40public class GetResourceNullArg {
41    private static class MyClassLoader extends ClassLoader {
42        public MyClassLoader() {
43            super(null);
44        }
45        @Override
46        public Class findClass(String name) throws ClassNotFoundException {
47            throw new ClassNotFoundException("Why are you using this?");
48        }
49    }
50
51    @DataProvider
52    public static ClassLoader[][] provider() {
53        try {
54            return new ClassLoader[][] {
55                { ClassLoader.getSystemClassLoader() },
56                { new MyClassLoader() },
57                { new URLClassLoader(new URL[]{ Paths.get(".").toUri().toURL() },
58                                     ClassLoader.getSystemClassLoader()) }
59            };
60        } catch (MalformedURLException e) { throw new RuntimeException(e); }
61    }
62
63    @Test(expectedExceptions = NullPointerException.class)
64    public void classGetResource() {
65        this.getClass().getResource(null);
66    }
67
68    @Test(expectedExceptions = NullPointerException.class)
69    public void classGetResourceAsStream() {
70        this.getClass().getResourceAsStream(null);
71    }
72
73    @Test(dataProvider = "provider",
74          expectedExceptions = NullPointerException.class)
75    public void loaderGetResource(ClassLoader cl) {
76        cl.getResource(null);
77    }
78
79    @Test(dataProvider = "provider",
80          expectedExceptions = NullPointerException.class)
81    public static void loaderGetResources(ClassLoader cl) throws IOException {
82        Enumeration<URL> retVal = cl.getResources(null);
83    }
84
85    @Test(dataProvider = "provider",
86          expectedExceptions = NullPointerException.class)
87    public static void loaderResources(ClassLoader cl) throws IOException {
88        Stream<URL> retVal = cl.resources(null);
89    }
90
91    @Test(dataProvider = "provider",
92          expectedExceptions = NullPointerException.class)
93    public void loaderGetResourceAsStream(ClassLoader cl) {
94        cl.getResourceAsStream(null);
95    }
96}
97