Path: blob/master/test/jdk/tools/launcher/modules/classpath/JavaClassPathTest.java
41153 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedWriter;24import java.nio.file.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.util.ArrayList;28import java.util.List;29import java.util.Map;30import java.util.spi.ToolProvider;31import java.util.stream.Stream;3233import jdk.test.lib.JDKToolFinder;34import jdk.test.lib.Platform;35import jdk.test.lib.compiler.CompilerUtils;36import jdk.test.lib.process.OutputAnalyzer;37import org.testng.annotations.BeforeTest;38import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;4041import static org.testng.Assert.assertTrue;42import static jdk.test.lib.process.ProcessTools.*;4344/**45* @test46* @bug 816820547* @summary Test the default class path if -Djava.class.path is set48* @library /test/lib49* @modules jdk.compiler50* jdk.jartool51* @build jdk.test.lib.compiler.CompilerUtils52* @run testng JavaClassPathTest53*/5455public class JavaClassPathTest {56private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"),57"src");58private static final Path MODS_DIR = Paths.get("mods");59private static final Path LIB_DIR = Paths.get("lib");60private static final String TEST_MODULE = "m";61private static final String TEST_MAIN = "jdk.test.Main";6263@BeforeTest64public void setup() throws Exception {65boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),66MODS_DIR.resolve(TEST_MODULE));67assertTrue(compiled, "module " + TEST_MODULE + " did not compile");6869// add the class and a resource to the current working directory70Path file = Paths.get("jdk/test/Main.class");71Files.createDirectories(file.getParent());72Files.copy(MODS_DIR.resolve(TEST_MODULE).resolve(file), file);7374Path res = Paths.get("jdk/test/res.properties");75Files.createFile(res);7677ToolProvider jartool = ToolProvider.findFirst("jar").orElseThrow(78() -> new RuntimeException("jar tool not found")79);8081Path jarfile = LIB_DIR.resolve("m.jar");82Files.createDirectories(LIB_DIR);83assertTrue(jartool.run(System.out, System.err, "cfe",84jarfile.toString(), TEST_MAIN,85file.toString()) == 0);8687Path manifest = LIB_DIR.resolve("manifest");88try (BufferedWriter writer = Files.newBufferedWriter(manifest)) {89writer.write("CLASS-PATH: lib/m.jar");90}91jarfile = LIB_DIR.resolve("m1.jar");92assertTrue(jartool.run(System.out, System.err, "cfme",93jarfile.toString(), manifest.toString(), TEST_MAIN,94file.toString()) == 0);95}9697@DataProvider(name = "classpath")98public Object[][] classpath() {99return new Object[][]{100// true indicates that class path default to current working directory101{ List.of(), "." },102{ List.of("-cp", ""), "" },103{ List.of("-cp", "."), "." },104{ List.of("-Djava.class.path"), "." },105{ List.of("-Djava.class.path="), "" },106{ List.of("-Djava.class.path=."), "." },107};108}109110@Test(dataProvider = "classpath")111public void testUnnamedModule(List<String> options, String expected)112throws Throwable113{114List<String> args = new ArrayList<>(options);115args.add(TEST_MAIN);116args.add(Boolean.toString(true));117args.add(expected);118119assertTrue(execute(args).getExitValue() == 0);120}121122@DataProvider(name = "moduleAndClassPath")123public Object[][] moduleAndClassPath() {124return new Object[][]{125// true indicates that class path default to current working directory126{ "", "" },127{ "-Djava.class.path", "" },128{ "-Djava.class.path=", "" },129};130}131132@Test(dataProvider = "moduleAndClassPath")133public void testNamedModule(String option, String expected) throws Throwable {134List<String> args = new ArrayList<>();135if (!option.isEmpty()) {136args.add(option);137}138args.add("--module-path");139args.add(MODS_DIR.toString());140args.add("-m");141args.add(TEST_MODULE + "/" + TEST_MAIN);142// not default to CWD143args.add(Boolean.toString(false));144args.add(expected);145146147assertTrue(execute(args).getExitValue() == 0);148}149150@Test151public void testClassPath() throws Throwable {152List<String> args = new ArrayList<>();153args.add("-Djava.class.path=.");154args.add("--module-path");155args.add(MODS_DIR.toString());156args.add("-m");157args.add(TEST_MODULE + "/" + TEST_MAIN);158args.add(Boolean.toString(true));159args.add(".");160161assertTrue(execute(args).getExitValue() == 0);162}163164@Test165public void testJAR() throws Throwable {166String jarfile = LIB_DIR.resolve("m.jar").toString();167List<String> args = new ArrayList<>();168args.add("-jar");169args.add(jarfile);170args.add(Boolean.toString(false));171args.add(jarfile);172173assertTrue(execute(args).getExitValue() == 0);174}175176/*177* Test CLASS-PATH attribute in manifest178*/179@Test180public void testClassPathAttribute() throws Throwable {181String jarfile = LIB_DIR.resolve("m1.jar").toString();182183List<String> args = new ArrayList<>();184args.add("-jar");185args.add(jarfile);186args.add(Boolean.toString(false));187args.add(jarfile);188189assertTrue(execute(args).getExitValue() == 0);190191args.clear();192args.add("-cp");193args.add(jarfile);194args.add(TEST_MAIN);195args.add(Boolean.toString(false));196args.add(jarfile);197198assertTrue(execute(args).getExitValue() == 0);199}200201private OutputAnalyzer execute(List<String> options) throws Throwable {202// can't use ProcessTools.createJavaProcessBuilder as it always adds -cp203ProcessBuilder pb = new ProcessBuilder(204Stream.concat(Stream.of(JDKToolFinder.getTestJDKTool("java")),205options.stream()206.map(this::autoQuote))207.toArray(String[]::new)208);209210Map<String,String> env = pb.environment();211// remove CLASSPATH environment variable212env.remove("CLASSPATH");213return executeCommand(pb)214.outputTo(System.out)215.errorTo(System.out);216}217218/*219* Autoquote empty string argument on Windows220*/221private String autoQuote(String arg) {222if (Platform.isWindows() && arg.isEmpty()) {223return "\"\"";224}225return arg;226}227}228229230