1 /* 2 * Copyright (c) 2014, 2017, 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 6760902 27 * @library /lib/testlibrary 28 * @library /test/lib 29 * @build jdk.test.lib.process.ProcessTools 30 * @run testng GetResource 31 * @summary Empty path on bootclasspath is not default to current working 32 * directory for both class lookup and resource lookup whereas 33 * empty path on classpath is default to current working directory. 34 */ 35 36 import java.io.File; 37 import java.io.IOException; 38 import java.net.URL; 39 import java.nio.file.Files; 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.Map; 45 import java.util.stream.Collectors; 46 import java.util.stream.Stream; 47 48 import jdk.testlibrary.JDKToolFinder; 49 import static jdk.test.lib.process.ProcessTools.*; 50 51 import org.testng.annotations.BeforeTest; 52 import org.testng.annotations.DataProvider; 53 import org.testng.annotations.Test; 54 55 public class GetResource { 56 private static final Path CWD = Paths.get(System.getProperty("user.dir")); 57 private static final String DIR_A = "a"; 58 private static final String DIR_B = "b"; 59 60 private static final String RESOURCE_NAME = "test.properties"; 61 private static final String GETRESOURCE_CLASS = "GetResource.class"; 62 63 public static void main(String... args) { 64 String expect = args[0] + "/" + RESOURCE_NAME; 65 URL url = GetResource.class.getResource(RESOURCE_NAME); 66 System.out.println("getResource found: " + url); 67 if (!url.toString().endsWith(expect)) { 68 throw new RuntimeException(url + " != expected resource " + expect); 69 } 70 71 url = ClassLoader.getSystemResource(RESOURCE_NAME); 72 System.out.println("getSystemResource found: " + url); 73 if (!url.toString().endsWith(expect)) { 74 throw new RuntimeException(url + " != expected resource " + expect); 75 } 76 } 77 78 @BeforeTest 79 public void setup() throws IOException { 80 // setup two directories "a" and "b" 81 // each directory contains both test.properties and this test class 82 Path testSrc = Paths.get(System.getProperty("test.src")); 83 Path testClasses = Paths.get(System.getProperty("test.classes")); 84 85 Files.createDirectories(Paths.get(DIR_A)); 86 Files.createDirectories(Paths.get(DIR_B)); 87 88 Files.copy(testSrc.resolve(RESOURCE_NAME), 89 Paths.get(DIR_A, RESOURCE_NAME)); 90 Files.copy(testSrc.resolve(RESOURCE_NAME), 91 Paths.get(DIR_B, RESOURCE_NAME)); 92 93 Files.copy(testClasses.resolve(GETRESOURCE_CLASS), 94 Paths.get(DIR_A, GETRESOURCE_CLASS)); 95 Files.copy(testClasses.resolve(GETRESOURCE_CLASS), 96 Paths.get(DIR_B, GETRESOURCE_CLASS)); 97 } 98 99 private String concat(String... dirs) { 100 return Stream.of(dirs).collect(Collectors.joining(File.pathSeparator)); 101 } 102 103 @DataProvider 104 public Object[][] options() { 105 return new Object[][] { 106 new Object[] { List.of("-Xbootclasspath/a:a"), "a"}, 107 new Object[] { List.of("-Xbootclasspath/a:b"), "b"}, 108 new Object[] { List.of("-Xbootclasspath/a:" + concat("a", "b")), "a"}, 109 new Object[] { List.of("-Xbootclasspath/a:" + concat("b", "a")), "b"}, 110 111 new Object[] { List.of("-cp", "a"), "a"}, 112 new Object[] { List.of("-cp", "b"), "b"}, 113 new Object[] { List.of("-cp", concat("a", "b")), "a"}, 114 new Object[] { List.of("-cp", concat("b", "a")), "b"}, 115 }; 116 } 117 118 @Test(dataProvider = "options") 119 public void test(List<String> options, String expected) throws Throwable { 120 runTest(CWD, options, expected); 121 } 122 123 @DataProvider 124 public Object[][] dirA() { 125 String dirB = ".." + File.separator + "b"; 126 return new Object[][] { 127 new Object[] { List.of("-Xbootclasspath/a:."), "a"}, 128 129 new Object[] { List.of("-Xbootclasspath/a:" + dirB), "b"}, 130 // empty path in first element 131 new Object[] { List.of("-Xbootclasspath/a:" + File.pathSeparator + dirB), "b"}, 132 133 new Object[] { List.of("-cp", File.pathSeparator), "a"}, 134 new Object[] { List.of("-cp", dirB), "b"}, 135 new Object[] { List.of("-cp", File.pathSeparator + dirB), "a"}, 136 }; 137 } 138 139 @Test(dataProvider = "dirA") 140 public void testCurrentDirA(List<String> options, String expected) throws Throwable { 141 // current working directory is "a" 142 runTest(CWD.resolve(DIR_A), options, expected); 143 } 144 145 private void runTest(Path dir, List<String> options, String expected) 146 throws Throwable 147 { 148 String javapath = JDKToolFinder.getJDKTool("java"); 149 150 List<String> cmdLine = new ArrayList<>(); 151 cmdLine.add(javapath); 152 options.forEach(cmdLine::add); 153 154 cmdLine.add("GetResource"); 155 cmdLine.add(expected); 156 157 System.out.println("Command line: " + cmdLine); 158 ProcessBuilder pb = 159 new ProcessBuilder(cmdLine.stream().toArray(String[]::new)); 160 161 // change working directory 162 pb.directory(dir.toFile()); 163 164 // remove CLASSPATH environment variable 165 Map<String,String> env = pb.environment(); 166 String value = env.remove("CLASSPATH"); 167 168 executeCommand(pb).shouldHaveExitValue(0); 169 } 170 171 }