Skip to content
Permalink
Browse files
8262277: URLClassLoader.getResource throws undocumented IllegalArgume…
…ntException

Reviewed-by: alanb, bchristi, psadhukhan
  • Loading branch information
candrews authored and Brent Christian committed Mar 15, 2021
1 parent 4f1cda4 commit 0c718ab2a0a9dd163976efa539862ef49a56e532
@@ -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
@@ -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 {
@@ -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 {
@@ -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() {}
}

1 comment on commit 0c718ab

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 0c718ab Mar 15, 2021

Please sign in to comment.