Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8262277: URLClassLoader.getResource throws undocumented IllegalArgume…
Browse files Browse the repository at this point in the history
…ntException

Reviewed-by: alanb, bchristi, psadhukhan
  • Loading branch information
candrews authored and Brent Christian committed Mar 15, 2021
1 parent 4f1cda4 commit 0c718ab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -600,7 +600,7 @@ URL findResource(final String name, boolean check) {
try {
url = new URL(base, ParseUtil.encodePath(name, false));
} catch (MalformedURLException e) {
throw new IllegalArgumentException("name");
return null;
}

try {
Expand Down Expand Up @@ -636,7 +636,7 @@ Resource getResource(final String name, boolean check) {
try {
url = new URL(base, ParseUtil.encodePath(name, false));
} catch (MalformedURLException e) {
throw new IllegalArgumentException("name");
return null;
}
final URLConnection uc;
try {
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8262277
* @summary Test if URLClassLoader throws IllegalArgumentException when getting
* or finding resources.
*/

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;

public class FindResourceDoesNotThrowException {
public static void main(String[] args) throws Exception {
// URL ends in slash, does not start with file: or jar:
URL url = new URL("https://127.0.0.1/");
// resource name is not a valid url component
String resource = "c:/windows";
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url})) {
if (urlClassLoader.findResource(resource) != null) {
throw new RuntimeException("findResource should return null");
}
if (urlClassLoader.getResource(resource) != null) {
throw new RuntimeException("getResource should return null");
}
if (urlClassLoader.findResources(resource).hasMoreElements()) {
throw new RuntimeException("findResources should return an empty enumeration");
}
if (urlClassLoader.getResources(resource).hasMoreElements()) {
throw new RuntimeException("getResources should return an empty enumeration");
}
}
}
private FindResourceDoesNotThrowException() {}
}

0 comments on commit 0c718ab

Please sign in to comment.