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