Path: blob/master/test/jdk/tools/launcher/modules/dryrun/DryRunTest.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*/2223/**24* @test25* @bug 815959626* @library /test/lib27* @modules jdk.compiler28* jdk.jartool29* @build DryRunTest jdk.test.lib.process.ProcessTools30* jdk.test.lib.compiler.CompilerUtils31* @run testng DryRunTest32* @summary Test java --dry-run33*/3435import java.io.File;36import java.io.IOException;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.spi.ToolProvider;4142import jdk.test.lib.compiler.CompilerUtils;43import jdk.test.lib.process.ProcessTools;4445import org.testng.annotations.BeforeTest;46import org.testng.annotations.Test;47import static org.testng.Assert.*;4849@Test50public class DryRunTest {5152private static final String TEST_SRC = System.getProperty("test.src");5354private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");55private static final Path MODS_DIR = Paths.get("mods");56private static final Path LIBS_DIR = Paths.get("libs");5758// the module name of the test module59private static final String TEST_MODULE = "test";60private static final String M_MODULE = "m";6162// the module main class63private static final String MAIN_CLASS = "jdk.test.Main";64private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit";656667@BeforeTest68public void compileTestModule() throws Exception {6970// javac -d mods/$TESTMODULE src/$TESTMODULE/**71assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE),72MODS_DIR,73"--module-source-path", SRC_DIR.toString()));7475assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),76MODS_DIR,77"--module-source-path", SRC_DIR.toString()));7879Files.createDirectories(LIBS_DIR);8081// create JAR files with no module-info.class82assertTrue(jar(M_MODULE, "p/Lib.class") == 0);83assertTrue(jar(TEST_MODULE, "jdk/test/Main.class") == 0);84}8586/**87* Execute "java" with the given arguments, returning the exit code.88*/89private int exec(String... args) throws Exception {90return ProcessTools.executeTestJava(args)91.outputTo(System.out)92.errorTo(System.out)93.getExitValue();94}959697/**98* Launch module main99*/100public void testModule() throws Exception {101String dir = MODS_DIR.toString();102String mid = TEST_MODULE + "/" + MAIN_CLASS;103104// no resolution failure105int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid);106assertTrue(exitValue == 0);107}108109/**110* Test dryrun that does not invoke <clinit> of the main class111*/112public void testMainClinit() throws Exception {113String dir = MODS_DIR.toString();114String mid = TEST_MODULE + "/" + MAIN_CLINIT_CLASS;115116int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid);117assertTrue(exitValue == 0);118119// expect the test to fail if main class is initialized120exitValue = exec("--module-path", dir, "-m", mid);121assertTrue(exitValue != 0);122}123124/**125* Test non-existence module in --add-modules126*/127public void testNonExistAddModules() throws Exception {128String dir = MODS_DIR.toString();129String mid = TEST_MODULE + "/" + MAIN_CLASS;130131int exitValue = exec("--dry-run", "--module-path", dir,132"--add-modules", "non.existence",133"-m", mid);134assertTrue(exitValue != 0);135}136137/**138* Launch main class from class path139*/140public void testClassPath() throws Exception {141Path testJar = LIBS_DIR.resolve(TEST_MODULE + ".jar");142String libs = testJar.toString() + File.pathSeparator +143LIBS_DIR.resolve(M_MODULE + ".jar").toString();144145// test pass with m.jar:test.jar146int exitValue = exec("-classpath", libs, MAIN_CLASS);147assertTrue(exitValue == 0);148149// m.jar is not on classpath and fails with p.Lib not found150exitValue = exec("-classpath", testJar.toString(), MAIN_CLASS);151assertTrue(exitValue != 0);152153// dry pass passes since main is not executed154exitValue = exec("--dry-run", "-classpath", testJar.toString(), MAIN_CLASS);155assertTrue(exitValue == 0);156}157158/**159* Test automatic modules160*/161public void testAutomaticModule() throws Exception {162String libs = LIBS_DIR.resolve(M_MODULE + ".jar").toString() +163File.pathSeparator +164LIBS_DIR.resolve(TEST_MODULE + ".jar").toString();165String mid = TEST_MODULE + "/" + MAIN_CLASS;166167// test main method with and without --add-modules mm168int exitValue = exec("--module-path", LIBS_DIR.toString(),169"-m", mid);170assertTrue(exitValue == 0);171}172173/**174* module m not found175*/176public void testMissingModule() throws Exception {177String subdir = MODS_DIR.resolve(TEST_MODULE).toString();178String mid = TEST_MODULE + "/" + MAIN_CLASS;179180// resolution failure181int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid);182assertTrue(exitValue != 0);183}184185private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")186.orElseThrow(() ->187new RuntimeException("jar tool not found")188);189190private static int jar(String name, String entries) throws IOException {191Path jar = LIBS_DIR.resolve(name + ".jar");192193// jar --create ...194String classes = MODS_DIR.resolve(name).toString();195String[] args = {196"--create",197"--file=" + jar,198"-C", classes, entries199};200return JAR_TOOL.run(System.out, System.out, args);201}202}203204205