bpb | fadd492 | 2013-10-22 11:25:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 7179567 |
| 27 | * @summary Test that URLClassLoader public constructors and factory methods |
| 28 | * throw NullPointerException when appropriate. |
| 29 | * |
| 30 | * Tests whether URLClassLoader public constructors and factory methods throw |
| 31 | * appropriate NullPointerExceptions for 1) a null URL array parameter, and |
| 32 | * 2) a non-null URL array containing a null element. |
| 33 | */ |
| 34 | |
| 35 | import java.io.File; |
| 36 | import java.io.IOException; |
| 37 | import java.net.URL; |
| 38 | import java.net.URLClassLoader; |
| 39 | import java.util.jar.JarFile; |
| 40 | |
| 41 | public class NullURLTest { |
| 42 | JarFile jarFile; |
| 43 | |
| 44 | public static void main(String[] args) throws Throwable { |
| 45 | new NullURLTest(); |
| 46 | } |
| 47 | |
| 48 | NullURLTest() throws Throwable { |
| 49 | File local = new File(System.getProperty("test.src", "."), "jars"); |
| 50 | String path = "jar:file:" |
| 51 | + local.getPath() |
| 52 | + "/class_path_test.jar!/Foo.class"; |
| 53 | |
| 54 | URL validURL = new URL(path); |
| 55 | URL[] validURLArray = new URL[] { validURL, validURL }; |
| 56 | URL[] invalidURLArray = new URL[] { validURL, null }; |
| 57 | |
| 58 | int failures = 0; |
| 59 | URLClassLoader loader; |
| 60 | |
| 61 | try { |
| 62 | loader = new URLClassLoader(validURLArray); |
| 63 | } catch (Throwable t) { |
| 64 | System.err.println("URLClassLoader(validURLArray) threw " + t); |
| 65 | failures++; |
| 66 | } |
| 67 | try { |
| 68 | loader = new URLClassLoader(null); |
| 69 | System.err.println("URLClassLoader(null) did not throw NPE"); |
| 70 | failures++; |
| 71 | } catch (NullPointerException e) { |
| 72 | // expected |
| 73 | } |
| 74 | // This section should be uncommented if 8026517 is fixed. |
| 75 | // try { |
| 76 | // loader = new URLClassLoader(invalidURLArray); |
| 77 | // System.err.println("URLClassLoader(invalidURLArray) did not throw NPE"); |
| 78 | // failures++; |
| 79 | // } catch (NullPointerException e) { |
| 80 | // // expected |
| 81 | // } |
| 82 | |
| 83 | try { |
| 84 | loader = new URLClassLoader(validURLArray, null); |
| 85 | } catch (Throwable t) { |
| 86 | System.err.println("URLClassLoader(validURLArray, null) threw " + t); |
| 87 | failures++; |
| 88 | } |
| 89 | try { |
| 90 | loader = new URLClassLoader(null, null); |
| 91 | System.err.println("URLClassLoader(null, null) did not throw NPE"); |
| 92 | failures++; |
| 93 | } catch (NullPointerException e) { |
| 94 | // expected |
| 95 | } |
| 96 | // This section should be uncommented if 8026517 is fixed. |
| 97 | // try { |
| 98 | // loader = new URLClassLoader(invalidURLArray, null); |
| 99 | // System.err.println("URLClassLoader(invalidURLArray, null) did not throw NPE"); |
| 100 | // failures++; |
| 101 | // } catch (NullPointerException e) { |
| 102 | // // expected |
| 103 | // } |
| 104 | |
| 105 | try { |
| 106 | loader = new URLClassLoader(validURLArray, null, null); |
| 107 | } catch (Throwable t) { |
| 108 | System.err.println("URLClassLoader(validURLArray, null, null) threw " + t); |
| 109 | failures++; |
| 110 | } |
| 111 | try { |
| 112 | loader = new URLClassLoader(null, null, null); |
| 113 | System.err.println("URLClassLoader(null, null, null) did not throw NPE"); |
| 114 | failures++; |
| 115 | } catch (NullPointerException e) { |
| 116 | // expected |
| 117 | } |
| 118 | // This section should be uncommented if 8026517 is fixed. |
| 119 | // try { |
| 120 | // loader = new URLClassLoader(invalidURLArray, null, null); |
| 121 | // System.err.println("URLClassLoader(invalidURLArray, null, null) did not throw NPE"); |
| 122 | // failures++; |
| 123 | // } catch (NullPointerException e) { |
| 124 | // // expected |
| 125 | // } |
| 126 | |
| 127 | try { |
| 128 | loader = URLClassLoader.newInstance(validURLArray); |
| 129 | } catch (Throwable t) { |
| 130 | System.err.println("URLClassLoader.newInstance(validURLArray) threw " + t); |
| 131 | failures++; |
| 132 | } |
| 133 | try { |
| 134 | loader = URLClassLoader.newInstance(null); |
| 135 | System.err.println("URLClassLoader.newInstance(null) did not throw NPE"); |
| 136 | failures++; |
| 137 | } catch (NullPointerException e) { |
| 138 | // expected |
| 139 | } |
| 140 | // This section should be uncommented if 8026517 is fixed. |
| 141 | // try { |
| 142 | // loader = URLClassLoader.newInstance(invalidURLArray); |
| 143 | // System.err.println("URLClassLoader.newInstance(invalidURLArray) did not throw NPE"); |
| 144 | // failures++; |
| 145 | // } catch (NullPointerException e) { |
| 146 | // // expected |
| 147 | // } |
| 148 | |
| 149 | try { |
| 150 | loader = URLClassLoader.newInstance(validURLArray, null); |
| 151 | } catch (Throwable t) { |
| 152 | System.err.println("URLClassLoader.newInstance(validURLArray, null) threw " + t); |
| 153 | failures++; |
| 154 | } |
| 155 | try { |
| 156 | loader = URLClassLoader.newInstance(null, null); |
| 157 | System.err.println("URLClassLoader.newInstance(null, null) did not throw NPE"); |
| 158 | failures++; |
| 159 | } catch (NullPointerException e) { |
| 160 | // expected |
| 161 | } |
| 162 | // This section should be uncommented if 8026517 is fixed. |
| 163 | // try { |
| 164 | // loader = URLClassLoader.newInstance(invalidURLArray, null); |
| 165 | // System.err.println("URLClassLoader.newInstance(invalidURLArray, null) did not throw NPE"); |
| 166 | // failures++; |
| 167 | // } catch (NullPointerException e) { |
| 168 | // // expected |
| 169 | // } |
| 170 | |
| 171 | if (failures != 0) { |
| 172 | throw new Exception("URLClassLoader NullURLTest had "+failures+" failures!"); |
| 173 | } |
| 174 | } |
| 175 | } |