1 /* 2 * Copyright (c) 2016, 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 import java.io.BufferedWriter; 25 import java.nio.file.Files; 26 import java.nio.file.Path; 27 import java.nio.file.Paths; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.spi.ToolProvider; 32 33 import jdk.test.lib.compiler.CompilerUtils; 34 import jdk.testlibrary.OutputAnalyzer; 35 import org.testng.annotations.BeforeTest; 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 import static org.testng.Assert.assertTrue; 40 import static jdk.testlibrary.ProcessTools.*; 41 42 /** 43 * @test 44 * @bug 8168205 45 * @summary Test the default class path if -Djava.class.path is set 46 * @library /lib/testlibrary /test/lib 47 * @modules jdk.compiler 48 * jdk.jartool 49 * @build jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.* 50 * @run testng JavaClassPathTest 51 */ 52 53 public class JavaClassPathTest { 54 private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), 55 "src"); 56 private static final Path MODS_DIR = Paths.get("mods"); 57 private static final Path LIB_DIR = Paths.get("lib"); 58 private static final String TEST_MODULE = "m"; 59 private static final String TEST_MAIN = "jdk.test.Main"; 60 61 @BeforeTest 62 public void setup() throws Exception { 63 boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 64 MODS_DIR.resolve(TEST_MODULE)); 65 assertTrue(compiled, "module " + TEST_MODULE + " did not compile"); 66 67 // add the class and a resource to the current working directory 68 Path file = Paths.get("jdk/test/Main.class"); 69 Files.createDirectories(file.getParent()); 70 Files.copy(MODS_DIR.resolve(TEST_MODULE).resolve(file), file); 71 72 Path res = Paths.get("jdk/test/res.properties"); 73 Files.createFile(res); 74 75 ToolProvider jartool = ToolProvider.findFirst("jar").orElseThrow( 76 () -> new RuntimeException("jar tool not found") 77 ); 78 79 Path jarfile = LIB_DIR.resolve("m.jar"); 80 Files.createDirectories(LIB_DIR); 81 assertTrue(jartool.run(System.out, System.err, "cfe", 82 jarfile.toString(), TEST_MAIN, 83 file.toString()) == 0); 84 85 Path manifest = LIB_DIR.resolve("manifest"); 86 try (BufferedWriter writer = Files.newBufferedWriter(manifest)) { 87 writer.write("CLASS-PATH: lib/m.jar"); 88 } 89 jarfile = LIB_DIR.resolve("m1.jar"); 90 assertTrue(jartool.run(System.out, System.err, "cfme", 91 jarfile.toString(), manifest.toString(), TEST_MAIN, 92 file.toString()) == 0); 93 } 94 95 @DataProvider(name = "classpath") 96 public Object[][] classpath() { 97 return new Object[][]{ 98 // true indicates that class path default to current working directory 99 { List.of(), "." }, 100 { List.of("-cp", ""), "" }, 101 { List.of("-cp", "."), "." }, 102 { List.of("-Djava.class.path"), "." }, 103 { List.of("-Djava.class.path="), "" }, 104 { List.of("-Djava.class.path=."), "." }, 105 }; 106 } 107 108 @Test(dataProvider = "classpath") 109 public void testUnnamedModule(List<String> options, String expected) 110 throws Throwable 111 { 112 List<String> args = new ArrayList<>(options); 113 args.add(TEST_MAIN); 114 args.add(Boolean.toString(true)); 115 args.add(expected); 116 117 assertTrue(execute(args).getExitValue() == 0); 118 } 119 120 @DataProvider(name = "moduleAndClassPath") 121 public Object[][] moduleAndClassPath() { 122 return new Object[][]{ 123 // true indicates that class path default to current working directory 124 { "", "" }, 125 { "-Djava.class.path", "" }, 126 { "-Djava.class.path=", "" }, 127 }; 128 } 129 130 @Test(dataProvider = "moduleAndClassPath") 131 public void testNamedModule(String option, String expected) throws Throwable { 132 List<String> args = new ArrayList<>(); 133 if (!option.isEmpty()) { 134 args.add(option); 135 } 136 args.add("--module-path"); 137 args.add(MODS_DIR.toString()); 138 args.add("-m"); 139 args.add(TEST_MODULE + "/" + TEST_MAIN); 140 // not default to CWD 141 args.add(Boolean.toString(false)); 142 args.add(expected); 143 144 145 assertTrue(execute(args).getExitValue() == 0); 146 } 147 148 @Test 149 public void testClassPath() throws Throwable { 150 List<String> args = new ArrayList<>(); 151 args.add("-Djava.class.path=."); 152 args.add("--module-path"); 153 args.add(MODS_DIR.toString()); 154 args.add("-m"); 155 args.add(TEST_MODULE + "/" + TEST_MAIN); 156 args.add(Boolean.toString(true)); 157 args.add("."); 158 159 assertTrue(execute(args).getExitValue() == 0); 160 } 161 162 @Test 163 public void testJAR() throws Throwable { 164 String jarfile = LIB_DIR.resolve("m.jar").toString(); 165 List<String> args = new ArrayList<>(); 166 args.add("-jar"); 167 args.add(jarfile); 168 args.add(Boolean.toString(false)); 169 args.add(jarfile); 170 171 assertTrue(execute(args).getExitValue() == 0); 172 } 173 174 /* 175 * Test CLASS-PATH attribute in manifest 176 */ 177 @Test 178 public void testClassPathAttribute() throws Throwable { 179 String jarfile = LIB_DIR.resolve("m1.jar").toString(); 180 181 List<String> args = new ArrayList<>(); 182 args.add("-jar"); 183 args.add(jarfile); 184 args.add(Boolean.toString(false)); 185 args.add(jarfile); 186 187 assertTrue(execute(args).getExitValue() == 0); 188 189 args.clear(); 190 args.add("-cp"); 191 args.add(jarfile); 192 args.add(TEST_MAIN); 193 args.add(Boolean.toString(false)); 194 args.add(jarfile); 195 196 assertTrue(execute(args).getExitValue() == 0); 197 } 198 199 private OutputAnalyzer execute(List<String> options) throws Throwable { 200 ProcessBuilder pb = createJavaProcessBuilder( 201 options.stream() 202 .map(this::autoQuote) 203 .toArray(String[]::new) 204 ); 205 206 Map<String,String> env = pb.environment(); 207 // remove CLASSPATH environment variable 208 String value = env.remove("CLASSPATH"); 209 return executeCommand(pb) 210 .outputTo(System.out) 211 .errorTo(System.out); 212 } 213 214 private static final boolean IS_WINDOWS 215 = System.getProperty("os.name").startsWith("Windows"); 216 217 /* 218 * Autoquote empty string argument on Windows 219 */ 220 private String autoQuote(String arg) { 221 if (IS_WINDOWS && arg.isEmpty()) { 222 return "\"\""; 223 } 224 return arg; 225 } 226 }