1 /* 2 * Copyright (c) 2016, 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.nio.file.Files; 25 import java.nio.file.Path; 26 import java.nio.file.Paths; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Map; 30 31 import jdk.testlibrary.JDKToolFinder; 32 import jdk.testlibrary.OutputAnalyzer; 33 import jdk.testlibrary.ProcessTools; 34 import org.testng.annotations.BeforeTest; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import static org.testng.Assert.assertTrue; 39 40 /** 41 * @test 42 * @bug 8168205 43 * @summary Test the default class path if -Djava.class.path is set 44 * @library /lib/testlibrary 45 * @modules jdk.compiler 46 * @build CompilerUtils jdk.testlibrary.* 47 * @run testng JavaClassPathTest 48 */ 49 50 public class JavaClassPathTest { 51 private static String JAVA_TOOL = JDKToolFinder.getJDKTool("java"); 52 53 private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), 54 "src"); 55 private static final Path MODS_DIR = Paths.get("mods"); 56 private static final String TEST_MODULE = "m"; 57 private static final String TEST_MAIN = "jdk.test.Main"; 58 59 @BeforeTest 60 public void setup() throws Exception { 61 boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 62 MODS_DIR.resolve(TEST_MODULE)); 63 assertTrue(compiled, "module " + TEST_MODULE + " did not compile"); 64 65 // add the class and a resource to the current working directory 66 Path file = Paths.get("jdk/test/Main.class"); 67 Files.createDirectories(file.getParent()); 68 Files.copy(MODS_DIR.resolve(TEST_MODULE).resolve(file), file); 69 70 Path res = Paths.get("jdk/test/res.properties"); 71 Files.createFile(res); 72 } 73 74 @DataProvider(name = "classpath") 75 public Object[][] classpath() { 76 return new Object[][]{ 77 // true indicates that class path default to current working directory 78 { "", true }, 79 { "-Djava.class.path", true }, 80 { "-Djava.class.path=", true }, 81 { "-Djava.class.path=.", true }, 82 }; 83 } 84 85 @Test(dataProvider = "classpath") 86 public void testUnnamedModule(String option, boolean expected) throws Throwable { 87 88 List<String> cmds = new ArrayList<>(); 89 cmds.add(JAVA_TOOL); 90 if (!option.isEmpty()) { 91 cmds.add(option); 92 } 93 cmds.add(TEST_MAIN); 94 cmds.add(Boolean.toString(expected)); 95 96 assertTrue(execute(cmds).getExitValue() == 0); 97 } 98 99 @DataProvider(name = "moduleAndClassPath") 100 public Object[][] moduleAndClassPath() { 101 return new Object[][]{ 102 // true indicates that class path default to current working directory 103 { "", false }, 104 { "-Djava.class.path", false }, 105 { "-Djava.class.path=", false }, 106 { "-Djava.class.path=.", true }, 107 }; 108 } 109 110 @Test(dataProvider = "moduleAndClassPath") 111 public void testNamedModule(String option, boolean expected) throws Throwable { 112 String javapath = JDKToolFinder.getJDKTool("java"); 113 114 List<String> cmds = new ArrayList<>(); 115 cmds.add(javapath); 116 if (!option.isEmpty()) { 117 cmds.add(option); 118 } 119 cmds.add("--module-path"); 120 cmds.add(MODS_DIR.toString()); 121 cmds.add("-m"); 122 cmds.add(TEST_MODULE + "/" + TEST_MAIN); 123 cmds.add(Boolean.toString(expected)); 124 125 assertTrue(execute(cmds).getExitValue() == 0); 126 } 127 128 private OutputAnalyzer execute(List<String> cmds) throws Throwable { 129 ProcessBuilder pb = new ProcessBuilder(cmds); 130 Map<String,String> env = pb.environment(); 131 // remove CLASSPATH environment variable 132 String value = env.remove("CLASSPATH"); 133 return ProcessTools.executeCommand(pb) 134 .outputTo(System.out) 135 .errorTo(System.out); 136 } 137 138 }