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