Path: blob/master/test/jdk/java/lang/ClassLoader/getResource/GetResource.java
41153 views
/*1* Copyright (c) 2014, 2018, 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 676090226* @library /test/lib27* @build jdk.test.lib.process.ProcessTools28* @run testng GetResource29* @summary Empty path on bootclasspath is not default to current working30* directory for both class lookup and resource lookup whereas31* empty path on classpath is default to current working directory.32*/3334import java.io.File;35import java.io.IOException;36import java.net.URL;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.ArrayList;41import java.util.List;42import java.util.Map;43import java.util.stream.Collectors;44import java.util.stream.Stream;4546import jdk.test.lib.JDKToolFinder;47import static jdk.test.lib.process.ProcessTools.*;4849import org.testng.annotations.BeforeTest;50import org.testng.annotations.DataProvider;51import org.testng.annotations.Test;5253public class GetResource {54private static final Path CWD = Paths.get(System.getProperty("user.dir"));55private static final String DIR_A = "a";56private static final String DIR_B = "b";5758private static final String RESOURCE_NAME = "test.properties";59private static final String GETRESOURCE_CLASS = "GetResource.class";6061public static void main(String... args) {62String expect = args[0] + "/" + RESOURCE_NAME;63URL url = GetResource.class.getResource(RESOURCE_NAME);64System.out.println("getResource found: " + url);65if (!url.toString().endsWith(expect)) {66throw new RuntimeException(url + " != expected resource " + expect);67}6869url = ClassLoader.getSystemResource(RESOURCE_NAME);70System.out.println("getSystemResource found: " + url);71if (!url.toString().endsWith(expect)) {72throw new RuntimeException(url + " != expected resource " + expect);73}74}7576@BeforeTest77public void setup() throws IOException {78// setup two directories "a" and "b"79// each directory contains both test.properties and this test class80Path testSrc = Paths.get(System.getProperty("test.src"));81Path testClasses = Paths.get(System.getProperty("test.classes"));8283Files.createDirectories(Paths.get(DIR_A));84Files.createDirectories(Paths.get(DIR_B));8586Files.copy(testSrc.resolve(RESOURCE_NAME),87Paths.get(DIR_A, RESOURCE_NAME));88Files.copy(testSrc.resolve(RESOURCE_NAME),89Paths.get(DIR_B, RESOURCE_NAME));9091Files.copy(testClasses.resolve(GETRESOURCE_CLASS),92Paths.get(DIR_A, GETRESOURCE_CLASS));93Files.copy(testClasses.resolve(GETRESOURCE_CLASS),94Paths.get(DIR_B, GETRESOURCE_CLASS));95}9697private String concat(String... dirs) {98return Stream.of(dirs).collect(Collectors.joining(File.pathSeparator));99}100101@DataProvider102public Object[][] options() {103return new Object[][] {104new Object[] { List.of("-Xbootclasspath/a:a"), "a"},105new Object[] { List.of("-Xbootclasspath/a:b"), "b"},106new Object[] { List.of("-Xbootclasspath/a:" + concat("a", "b")), "a"},107new Object[] { List.of("-Xbootclasspath/a:" + concat("b", "a")), "b"},108109new Object[] { List.of("-cp", "a"), "a"},110new Object[] { List.of("-cp", "b"), "b"},111new Object[] { List.of("-cp", concat("a", "b")), "a"},112new Object[] { List.of("-cp", concat("b", "a")), "b"},113};114}115116@Test(dataProvider = "options")117public void test(List<String> options, String expected) throws Throwable {118runTest(CWD, options, expected);119}120121@DataProvider122public Object[][] dirA() {123String dirB = ".." + File.separator + "b";124return new Object[][] {125new Object[] { List.of("-Xbootclasspath/a:."), "a"},126127new Object[] { List.of("-Xbootclasspath/a:" + dirB), "b"},128// empty path in first element129new Object[] { List.of("-Xbootclasspath/a:" + File.pathSeparator + dirB), "b"},130131new Object[] { List.of("-cp", File.pathSeparator), "a"},132new Object[] { List.of("-cp", dirB), "b"},133new Object[] { List.of("-cp", File.pathSeparator + dirB), "a"},134};135}136137@Test(dataProvider = "dirA")138public void testCurrentDirA(List<String> options, String expected) throws Throwable {139// current working directory is "a"140runTest(CWD.resolve(DIR_A), options, expected);141}142143private void runTest(Path dir, List<String> options, String expected)144throws Throwable145{146String javapath = JDKToolFinder.getJDKTool("java");147148List<String> cmdLine = new ArrayList<>();149cmdLine.add(javapath);150options.forEach(cmdLine::add);151152cmdLine.add("GetResource");153cmdLine.add(expected);154155System.out.println("Command line: " + cmdLine);156ProcessBuilder pb =157new ProcessBuilder(cmdLine.stream().toArray(String[]::new));158159// change working directory160pb.directory(dir.toFile());161162// remove CLASSPATH environment variable163Map<String,String> env = pb.environment();164String value = env.remove("CLASSPATH");165166executeCommand(pb).shouldHaveExitValue(0);167}168169}170171172