undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,156 @@
/*
* Copyright (c) 2014, 2023, 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 6760902
* @library /test/lib
* @build jdk.test.lib.process.ProcessTools
* @run testng GetResource
* @summary Empty path on bootclasspath is not default to current working
* directory for both class lookup and resource lookup whereas
* empty path on classpath is default to current working directory.
*/
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static jdk.test.lib.process.ProcessTools.*;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class GetResource {
private static final Path CWD = Paths.get(System.getProperty("user.dir"));
private static final String DIR_A = "a";
private static final String DIR_B = "b";
private static final String RESOURCE_NAME = "test.properties";
private static final String GETRESOURCE_CLASS = "GetResource.class";
public static void main(String... args) {
String expect = args[0] + "/" + RESOURCE_NAME;
URL url = GetResource.class.getResource(RESOURCE_NAME);
System.out.println("getResource found: " + url);
if (!url.toString().endsWith(expect)) {
throw new RuntimeException(url + " != expected resource " + expect);
}
url = ClassLoader.getSystemResource(RESOURCE_NAME);
System.out.println("getSystemResource found: " + url);
if (!url.toString().endsWith(expect)) {
throw new RuntimeException(url + " != expected resource " + expect);
}
}
@BeforeTest
public void setup() throws IOException {
// setup two directories "a" and "b"
// each directory contains both test.properties and this test class
Path testSrc = Paths.get(System.getProperty("test.src"));
Path testClasses = Paths.get(System.getProperty("test.classes"));
Files.createDirectories(Paths.get(DIR_A));
Files.createDirectories(Paths.get(DIR_B));
Files.copy(testSrc.resolve(RESOURCE_NAME),
Paths.get(DIR_A, RESOURCE_NAME));
Files.copy(testSrc.resolve(RESOURCE_NAME),
Paths.get(DIR_B, RESOURCE_NAME));
Files.copy(testClasses.resolve(GETRESOURCE_CLASS),
Paths.get(DIR_A, GETRESOURCE_CLASS));
Files.copy(testClasses.resolve(GETRESOURCE_CLASS),
Paths.get(DIR_B, GETRESOURCE_CLASS));
}
private String concat(String... dirs) {
return Stream.of(dirs).collect(Collectors.joining(File.pathSeparator));
}
@DataProvider
public Object[][] options() {
return new Object[][] {
new Object[] { List.of("-Xbootclasspath/a:a"), "a"},
new Object[] { List.of("-Xbootclasspath/a:b"), "b"},
new Object[] { List.of("-Xbootclasspath/a:" + concat("a", "b")), "a"},
new Object[] { List.of("-Xbootclasspath/a:" + concat("b", "a")), "b"},
new Object[] { List.of("-cp", "a"), "a"},
new Object[] { List.of("-cp", "b"), "b"},
new Object[] { List.of("-cp", concat("a", "b")), "a"},
new Object[] { List.of("-cp", concat("b", "a")), "b"},
};
}
@Test(dataProvider = "options")
public void test(List<String> options, String expected) throws Throwable {
runTest(CWD, options, expected);
}
@DataProvider
public Object[][] dirA() {
String dirB = ".." + File.separator + "b";
return new Object[][] {
new Object[] { List.of("-Xbootclasspath/a:."), "a"},
new Object[] { List.of("-Xbootclasspath/a:" + dirB), "b"},
// empty path in first element
new Object[] { List.of("-Xbootclasspath/a:" + File.pathSeparator + dirB), "b"},
new Object[] { List.of("-cp", File.pathSeparator), "a"},
new Object[] { List.of("-cp", dirB), "b"},
new Object[] { List.of("-cp", File.pathSeparator + dirB), "a"},
};
}
@Test(dataProvider = "dirA")
public void testCurrentDirA(List<String> options, String expected) throws Throwable {
// current working directory is "a"
runTest(CWD.resolve(DIR_A), options, expected);
}
private void runTest(Path dir, List<String> options, String expected)
throws Throwable
{
List<String> cmdLine = new ArrayList<>();
options.forEach(cmdLine::add);
cmdLine.add("GetResource");
cmdLine.add(expected);
ProcessBuilder pb = createTestJavaProcessBuilder(cmdLine);
pb.directory(dir.toFile()); // change working directory
pb.environment().remove("CLASSPATH"); // remove CLASSPATH environment variable
executeCommand(pb).shouldHaveExitValue(0);
}
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2017, 2018, 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
* @library /test/lib
* @build Driver Main
* jdk.test.lib.util.JarUtils
* @run main Driver
* @summary Test ClassLoader.getResourceXXX to locate resources in an automatic
* module
*/
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.util.JarUtils;
/**
* The driver creates a JAR file containing p/Foo.class, p/foo.properties,
* and p/resources/bar.properties. This ensures there are is a resource in
* a module package and a resource that is not in the module package. The
* test is then launched to locate every resource in the JAR file.
*/
public class Driver {
private static final String TEST_CLASSES = System.getProperty("test.classes");
public static void main(String[] args) throws Exception {
// create content for JAR file
Path dir = Files.createTempDirectory("classes");
Path p = Files.createDirectory(dir.resolve("p"));
Files.createFile(p.resolve("Foo.class"));
Files.createFile(p.resolve("foo.properties"));
Path resources = Files.createDirectory(p.resolve("resources"));
Files.createFile(resources.resolve("bar.properties"));
// create the JAR file, including a manifest
Path jarFile = Paths.get("library-1.0.jar");
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
JarUtils.createJarFile(jarFile, man, dir, p);
// get the module name
ModuleFinder finder = ModuleFinder.of(jarFile);
ModuleReference mref = finder.findAll().stream().findAny().orElse(null);
if (mref == null)
throw new RuntimeException("Module not found!!!");
String name = mref.descriptor().name();
// launch the test with the JAR file on the module path
if (ProcessTools.executeTestJava("-p", jarFile.toString(),
"--add-modules", name,
"-cp", TEST_CLASSES,
"Main", name)
.outputTo(System.out)
.errorTo(System.out)
.getExitValue() != 0)
throw new RuntimeException("Test failed - see output");
}
}

View file

@ -0,0 +1,95 @@
/*
* Copyright (c) 2017, 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.
*/
import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.net.URL;
import java.util.Enumeration;
import java.util.stream.Stream;
/**
* Usage: Main $MODULE
*
* Finds $MODULE in the boot layer and then tests that it can locate every
* resource in the module content (JAR file).
*/
public class Main {
static void testFind(String name) throws Exception {
// getResource
URL url = Main.class.getClassLoader().getResource(name);
if (url == null)
throw new RuntimeException("Unable to locate: " + name);
System.out.println(name + " => " + url);
// getResources
Enumeration<URL> urls = Main.class.getClassLoader().getResources(name);
if (!urls.hasMoreElements())
throw new RuntimeException("Unable to locate: " + name);
URL first = urls.nextElement();
if (!first.toURI().equals(url.toURI()))
throw new RuntimeException("found " + first + " ???");
// getResourceAsStream
if (!url.toString().endsWith("/")) {
InputStream in = Main.class.getClassLoader().getResourceAsStream(name);
if (in == null)
throw new RuntimeException("Unable to locate: " + name);
in.close();
}
}
static void testFindUnchecked(String name) {
try {
testFind(name);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
String mn = args[0];
ModuleReference mref = ModuleLayer.boot()
.configuration()
.findModule(mn)
.map(ResolvedModule::reference)
.orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));
try (ModuleReader reader = mref.open();
Stream<String> stream = reader.list()) {
stream.forEach(name -> {
testFindUnchecked(name);
// if the resource is a directory then find without trailing slash
if (name.endsWith("/")) {
testFindUnchecked(name.substring(0, name.length() - 1));
}
});
}
}
}

View file

@ -0,0 +1,365 @@
/*
* Copyright (c) 2015, 2024, 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.
*/
import java.io.InputStream;
import java.lang.module.Configuration;
import java.lang.module.ResolvedModule;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* Basic test of ClassLoader getResource and getResourceAsStream when
* invoked from code in named modules.
*/
public class Main {
static final String NAME = "myresource";
public static void main(String[] args) throws IOException {
// create resources in m1
createResource("m1", Paths.get("."), "m1");
createResource("m1", Paths.get("p1"), "m1/p1");
createResource("m1", Paths.get("p1", "impl"), "m1/p1.impl");
createResource("m1", Paths.get("p1", "resources"), "m1/p1.resources");
// create resources in m2
createResource("m2", Paths.get("."), "m2");
createResource("m2", Paths.get("p2"), "m2/p2");
createResource("m2", Paths.get("p2", "impl"), "m2/p2.impl");
createResource("m2", Paths.get("p2", "resources"), "m2/p2.resources");
// invoke ClassLoader getResource from the unnamed module
ClassLoader thisLoader = Main.class.getClassLoader();
URL url = thisLoader.getResource(NAME);
assertNotNull(url);
url = thisLoader.getResource("p1/" + NAME);
assertNull(url);
url = thisLoader.getResource("p1/impl/" + NAME);
assertNull(url);
url = thisLoader.getResource("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = thisLoader.getResource("p2/" + NAME);
assertNull(url);
url = thisLoader.getResource("p2/impl/" + NAME);
assertNull(url);
url = thisLoader.getResource("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResource from module m1
url = p1.Main.getResourceInClassLoader(NAME);
assertNotNull(url);
url = p1.Main.getResourceInClassLoader("p1/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p1/impl/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = p1.Main.getResourceInClassLoader("p2/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p2/impl/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResource from module m2
url = p2.Main.getResourceInClassLoader(NAME);
assertNotNull(url);
url = p2.Main.getResourceInClassLoader("p1/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p1/impl/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = p2.Main.getResourceInClassLoader("p2/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p2/impl/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResources from the unnamed module
Enumeration<URL> urls = thisLoader.getResources(NAME);
List<String> resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = thisLoader.getResources("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = thisLoader.getResources("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResources from m1
urls = p1.Main.getResourcesInClassLoader(NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = p1.Main.getResourcesInClassLoader("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = p1.Main.getResourcesInClassLoader("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResources from m2
urls = p2.Main.getResourcesInClassLoader(NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = p2.Main.getResourcesInClassLoader("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = p2.Main.getResourcesInClassLoader("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResourceAsStream from the unnamed module
InputStream in = thisLoader.getResourceAsStream(NAME);
assertNotNull(in);
in = thisLoader.getResourceAsStream("p1/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p1/impl/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = thisLoader.getResourceAsStream("p2/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p2/impl/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p2/resources/" + NAME);
assertNull(in);
// invoke ClassLoader getResource from modules m1
in = p1.Main.getResourceAsStreamInClassLoader(NAME);
assertNotNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = p1.Main.getResourceAsStreamInClassLoader("p2/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);
assertNull(in);
// invoke ClassLoader getResource from modules m2
in = p2.Main.getResourceAsStreamInClassLoader(NAME);
assertNotNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = p2.Main.getResourceAsStreamInClassLoader("p2/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);
assertNull(in);
System.out.println("Success!");
}
/**
* Create a resource in the sub-directory of the given exploded module
*/
static void createResource(String mn, Path subdir, String msg) throws IOException {
Path dir = directoryFor(mn).resolve(subdir);
Path file = dir.resolve(NAME);
Files.write(file, msg.getBytes("UTF-8"));
}
/**
* Returns the directory for the given module (by name).
*/
static Path directoryFor(String mn) {
Configuration cf = ModuleLayer.boot().configuration();
ResolvedModule resolvedModule = cf.findModule(mn).orElse(null);
if (resolvedModule == null)
throw new RuntimeException("not found: " + mn);
Path dir = Paths.get(resolvedModule.reference().location().get());
if (!Files.isDirectory(dir))
throw new RuntimeException("not a directory: " + dir);
return dir;
}
static String readAllAsString(InputStream in) throws IOException {
if (in == null)
return null;
try (in) {
return new String(in.readAllBytes(), "UTF-8");
}
}
static String readAllAsString(URL url) throws IOException {
if (url == null)
return null;
InputStream in = url.openStream();
return readAllAsString(url.openStream());
}
static List<String> readAllAsStrings(Enumeration<URL> urls) throws IOException {
List<String> result = new ArrayList<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
result.add(readAllAsString(url));
}
return result;
}
static void assertTrue(boolean condition) {
if (!condition) throw new RuntimeException();
}
static void assertFalse(boolean condition) {
if (condition) throw new RuntimeException();
}
static void assertNull(Object o) {
assertTrue(o == null);
}
static void assertNotNull(Object o) {
assertTrue(o != null);
}
static void assertEquals(Object actual, Object expected) {
if (expected == null) {
assertNull(actual);
} else {
assertTrue(expected.equals(actual));
}
}
static void assertNotEquals(Object actual, Object expected) {
if (expected == null) {
assertNotNull(actual);
} else {
assertTrue(!expected.equals(actual));
}
}
}

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2015, 2024, 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.
*/
import java.nio.file.Path;
import java.nio.file.Paths;
import static jdk.test.lib.process.ProcessTools.executeTestJava;
import jdk.test.lib.compiler.CompilerUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* @test
* @bug 8087335
* @library /test/lib
* @modules jdk.compiler
* @build ResourcesTest jdk.test.lib.compiler.CompilerUtils
* @run testng ResourcesTest
* @summary Driver for basic test of ClassLoader getResource and getResourceAsStream
*/
@Test
public class ResourcesTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
private static final Path CLASSES_DIR = Paths.get("classes");
private static final Path MODS_DIR = Paths.get("mods");
/**
* Compiles the modules used by the test and the test Main
*/
@BeforeTest
public void compileAll() throws Exception {
boolean compiled;
// javac --module-source-path mods -d mods src/**
compiled = CompilerUtils
.compile(SRC_DIR,
MODS_DIR,
"--module-source-path", SRC_DIR.toString());
assertTrue(compiled);
// javac --module-path mods -d classes Main.java
compiled = CompilerUtils
.compile(Paths.get(TEST_SRC, "Main.java"),
CLASSES_DIR,
"--module-path", MODS_DIR.toString(),
"--add-modules", "m1,m2");
assertTrue(compiled);
}
/**
* Run the test
*/
public void runTest() throws Exception {
int exitValue
= executeTestJava("--module-path", MODS_DIR.toString(),
"--add-modules", "m1,m2",
"-cp", CLASSES_DIR.toString(),
"Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015, 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.
*/
module m1 {
exports p1;
opens p1.resources;
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2015, 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.
*/
package p1;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
public class Main {
private Main() { }
public static URL getResourceInClassLoader(String name) {
return Main.class.getClassLoader().getResource(name);
}
public static Enumeration<URL> getResourcesInClassLoader(String name) throws IOException {
return Main.class.getClassLoader().getResources(name);
}
public static InputStream getResourceAsStreamInClassLoader(String name) {
return Main.class.getClassLoader().getResourceAsStream(name);
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, 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.
*/
package p1.impl;
public class Type { }

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, 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.
*/
package p1.resources;
public class Type { }

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2015, 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.
*/
module m2 {
exports p2;
exports p2.impl to m1;
opens p2.resources to m1;
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2015, 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.
*/
package p2;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
public class Main {
private Main() { }
public static URL getResourceInClassLoader(String name) {
return Main.class.getClassLoader().getResource(name);
}
public static Enumeration<URL> getResourcesInClassLoader(String name) throws IOException {
return Main.class.getClassLoader().getResources(name);
}
public static InputStream getResourceAsStreamInClassLoader(String name) {
return Main.class.getClassLoader().getResourceAsStream(name);
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, 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.
*/
package p2.impl;
public class Type { }

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, 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.
*/
package p2.resources;
public class Type { }

View file

@ -0,0 +1 @@
# empty resource