Path: blob/master/test/jdk/tools/launcher/modules/addmods/AddModsTest.java
41155 views
/*1* Copyright (c) 2014, 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* @library /test/lib26* @modules jdk.jlink/jdk.tools.jmod27* jdk.compiler28* @build AddModsTest jdk.test.lib.compiler.CompilerUtils29* @run testng AddModsTest30* @summary Basic test for java --add-modules31*/3233import java.io.File;34import java.nio.file.Path;35import java.nio.file.Paths;3637import jdk.test.lib.compiler.CompilerUtils;38import static jdk.test.lib.process.ProcessTools.*;3940import org.testng.annotations.BeforeTest;41import org.testng.annotations.Test;42import static org.testng.Assert.*;434445@Test46public class AddModsTest {4748private static final String TEST_SRC = System.getProperty("test.src");4950private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");51private static final Path MODS1_DIR = Paths.get("mods1");52private static final Path MODS2_DIR = Paths.get("mods2");5354// test module / main class55private static final String TEST_MODULE = "test";56private static final String TEST_MAIN_CLASS = "test.Main";57private static final String TEST_MID = TEST_MODULE + "/" + TEST_MAIN_CLASS;5859// logger module60private static final String LOGGER_MODULE = "logger";616263@BeforeTest64public void compile() throws Exception {65// javac -d mods1/test src/test/**66boolean compiled = CompilerUtils.compile(67SRC_DIR.resolve(TEST_MODULE),68MODS1_DIR.resolve(TEST_MODULE)69);70assertTrue(compiled, "test did not compile");7172// javac -d mods1/logger src/logger/**73compiled= CompilerUtils.compile(74SRC_DIR.resolve(LOGGER_MODULE),75MODS2_DIR.resolve(LOGGER_MODULE)76);77assertTrue(compiled, "test did not compile");78}798081/**82* Basic test of --add-modules ALL-DEFAULT. Module java.sql should be83* resolved and the types in that module should be visible.84*/85public void testAddDefaultModules1() throws Exception {8687// java --add-modules ALL-DEFAULT --module-path mods1 -m test ...88int exitValue89= executeTestJava("--module-path", MODS1_DIR.toString(),90"--add-modules", "ALL-DEFAULT",91"-m", TEST_MID,92"java.sql.Connection")93.outputTo(System.out)94.errorTo(System.out)95.getExitValue();9697assertTrue(exitValue == 0);98}99100101/**102* Run test on class path to load a type in a module on the application103* module path, uses {@code --add-modules logger}.104*/105public void testRunWithAddMods() throws Exception {106107// java --module-path mods --add-modules logger -cp classes test.Main108String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();109String modulepath = MODS2_DIR.toString();110int exitValue111= executeTestJava("--module-path", modulepath,112"--add-modules", LOGGER_MODULE,113"-cp", classpath,114TEST_MAIN_CLASS,115"logger.Logger")116.outputTo(System.out)117.errorTo(System.out)118.getExitValue();119120assertTrue(exitValue == 0);121}122123/**124* Run application on class path that makes use of module on the125* application module path. Does not use --add-modules and so should126* fail at run-time.127*/128public void testRunMissingAddMods() throws Exception {129130// java --module-path mods -cp classes test.Main131String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();132String modulepath = MODS1_DIR.toString();133int exitValue134= executeTestJava("--module-path", modulepath,135"-cp", classpath,136TEST_MAIN_CLASS,137"logger.Logger")138.outputTo(System.out)139.errorTo(System.out)140.shouldContain("ClassNotFoundException")141.getExitValue();142143assertTrue(exitValue != 0);144}145146147/**148* Run test on class path to load a type in a module on the application149* module path, uses {@code --add-modules ALL-MODULE-PATH}.150*/151public void testAddAllModulePath() throws Exception {152153// java --module-path mods --add-modules ALL-MODULE-PATH -cp classes test.Main154String classpath = MODS1_DIR.resolve(TEST_MODULE).toString();155String modulepath = MODS1_DIR.toString();156int exitValue157= executeTestJava("--module-path", modulepath,158"--add-modules", "ALL-MODULE-PATH",159"-cp", classpath,160TEST_MAIN_CLASS)161.outputTo(System.out)162.errorTo(System.out)163.getExitValue();164165assertTrue(exitValue == 0);166}167168169/**170* Test {@code --add-modules ALL-MODULE-PATH} without {@code --module-path}.171*/172public void testAddAllModulePathWithNoModulePath() throws Exception {173174// java --add-modules ALL-MODULE-PATH -version175int exitValue176= executeTestJava("--add-modules", "ALL-MODULE-PATH",177"-version")178.outputTo(System.out)179.errorTo(System.out)180.getExitValue();181182assertTrue(exitValue == 0);183}184185186/**187* Tests {@code --add-modules} be specified more than once.188*/189public void testWithMultipleAddModules() throws Exception {190191String modulepath = MODS1_DIR.toString() + File.pathSeparator +192MODS2_DIR.toString();193int exitValue194= executeTestJava("--module-path", modulepath,195"--add-modules", LOGGER_MODULE,196"--add-modules", TEST_MODULE,197"-m", TEST_MID,198"logger.Logger")199.outputTo(System.out)200.errorTo(System.out)201.getExitValue();202203assertTrue(exitValue == 0);204}205206207/**208* Attempt to run with a bad module name specified to --add-modules209*/210public void testRunWithBadAddMods() throws Exception {211212// java --module-path mods --add-modules DoesNotExist -m test ...213int exitValue214= executeTestJava("--module-path", MODS1_DIR.toString(),215"--add-modules", "DoesNotExist",216"-m", TEST_MID)217.outputTo(System.out)218.errorTo(System.out)219.shouldContain("DoesNotExist")220.getExitValue();221222assertTrue(exitValue != 0);223}224225}226227228