Path: blob/master/test/jdk/java/lang/ClassLoader/GetResourceNullArg.java
41149 views
/*1* Copyright (c) 2016, 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 java.io.IOException;24import java.net.MalformedURLException;25import java.net.URL;26import java.net.URLClassLoader;27import java.nio.file.Paths;28import java.util.Enumeration;29import java.util.stream.Stream;30import org.testng.annotations.*;3132/*33* @test34* @bug 813683135* @summary Test null argument to ClassLoader.getResourceXXXX()36* @run testng GetResourceNullArg37*/3839public class GetResourceNullArg {40private static class MyClassLoader extends ClassLoader {41public MyClassLoader() {42super(null);43}44@Override45public Class findClass(String name) throws ClassNotFoundException {46throw new ClassNotFoundException("Why are you using this?");47}48}4950@DataProvider51public static ClassLoader[][] provider() {52try {53return new ClassLoader[][] {54{ ClassLoader.getSystemClassLoader() },55{ new MyClassLoader() },56{ new URLClassLoader(new URL[]{ Paths.get(".").toUri().toURL() },57ClassLoader.getSystemClassLoader()) }58};59} catch (MalformedURLException e) { throw new RuntimeException(e); }60}6162@Test(expectedExceptions = NullPointerException.class)63public void classGetResource() {64this.getClass().getResource(null);65}6667@Test(expectedExceptions = NullPointerException.class)68public void classGetResourceAsStream() {69this.getClass().getResourceAsStream(null);70}7172@Test(dataProvider = "provider",73expectedExceptions = NullPointerException.class)74public void loaderGetResource(ClassLoader cl) {75cl.getResource(null);76}7778@Test(dataProvider = "provider",79expectedExceptions = NullPointerException.class)80public static void loaderGetResources(ClassLoader cl) throws IOException {81Enumeration<URL> retVal = cl.getResources(null);82}8384@Test(dataProvider = "provider",85expectedExceptions = NullPointerException.class)86public static void loaderResources(ClassLoader cl) throws IOException {87Stream<URL> retVal = cl.resources(null);88}8990@Test(dataProvider = "provider",91expectedExceptions = NullPointerException.class)92public void loaderGetResourceAsStream(ClassLoader cl) {93cl.getResourceAsStream(null);94}95}969798