Path: blob/master/test/jdk/java/net/URLClassLoader/NullURLTest.java
41149 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*/2223/*24* @test25* @bug 717956726* @summary Test that URLClassLoader public constructors and factory methods27* throw NullPointerException when appropriate.28*29* Tests whether URLClassLoader public constructors and factory methods throw30* appropriate NullPointerExceptions for 1) a null URL array parameter, and31* 2) a non-null URL array containing a null element.32*/3334import java.io.File;35import java.io.IOException;36import java.net.URL;37import java.net.URLClassLoader;38import java.util.jar.JarFile;3940public class NullURLTest {41JarFile jarFile;4243public static void main(String[] args) throws Throwable {44new NullURLTest();45}4647NullURLTest() throws Throwable {48File local = new File(System.getProperty("test.src", "."), "jars");49String path = "jar:file:"50+ local.getPath()51+ "/class_path_test.jar!/Foo.class";5253URL validURL = new URL(path);54URL[] validURLArray = new URL[] { validURL, validURL };55URL[] invalidURLArray = new URL[] { validURL, null };5657int failures = 0;58URLClassLoader loader;5960try {61loader = new URLClassLoader(validURLArray);62} catch (Throwable t) {63System.err.println("URLClassLoader(validURLArray) threw " + t);64failures++;65}66try {67loader = new URLClassLoader(null);68System.err.println("URLClassLoader(null) did not throw NPE");69failures++;70} catch (NullPointerException e) {71// expected72}7374try {75loader = new URLClassLoader(invalidURLArray);76System.err.println("URLClassLoader(invalidURLArray) did not throw NPE");77failures++;78} catch (NullPointerException e) {79// expected80}8182try {83loader = new URLClassLoader(validURLArray, null);84} catch (Throwable t) {85System.err.println("URLClassLoader(validURLArray, null) threw " + t);86failures++;87}88try {89loader = new URLClassLoader(null, null);90System.err.println("URLClassLoader(null, null) did not throw NPE");91failures++;92} catch (NullPointerException e) {93// expected94}9596try {97loader = new URLClassLoader(invalidURLArray, null);98System.err.println("URLClassLoader(invalidURLArray, null) did not throw NPE");99failures++;100} catch (NullPointerException e) {101// expected102}103104try {105loader = new URLClassLoader(validURLArray, null, null);106} catch (Throwable t) {107System.err.println("URLClassLoader(validURLArray, null, null) threw " + t);108failures++;109}110try {111loader = new URLClassLoader((URL[])null, null, null);112System.err.println("URLClassLoader(null, null, null) did not throw NPE");113failures++;114} catch (NullPointerException e) {115// expected116}117118try {119loader = new URLClassLoader(invalidURLArray, null, null);120System.err.println("URLClassLoader(invalidURLArray, null, null) did not throw NPE");121failures++;122} catch (NullPointerException e) {123// expected124}125126try {127loader = URLClassLoader.newInstance(validURLArray);128} catch (Throwable t) {129System.err.println("URLClassLoader.newInstance(validURLArray) threw " + t);130failures++;131}132try {133loader = URLClassLoader.newInstance(null);134System.err.println("URLClassLoader.newInstance(null) did not throw NPE");135failures++;136} catch (NullPointerException e) {137// expected138}139140try {141loader = URLClassLoader.newInstance(invalidURLArray);142System.err.println("URLClassLoader.newInstance(invalidURLArray) did not throw NPE");143failures++;144} catch (NullPointerException e) {145// expected146}147148try {149loader = URLClassLoader.newInstance(validURLArray, null);150} catch (Throwable t) {151System.err.println("URLClassLoader.newInstance(validURLArray, null) threw " + t);152failures++;153}154try {155loader = URLClassLoader.newInstance(null, null);156System.err.println("URLClassLoader.newInstance(null, null) did not throw NPE");157failures++;158} catch (NullPointerException e) {159// expected160}161162try {163loader = URLClassLoader.newInstance(invalidURLArray, null);164System.err.println("URLClassLoader.newInstance(invalidURLArray, null) did not throw NPE");165failures++;166} catch (NullPointerException e) {167// expected168}169170if (failures != 0) {171throw new Exception("URLClassLoader NullURLTest had "+failures+" failures!");172}173}174}175176177