Path: blob/master/test/jdk/tools/launcher/modules/patch/basic/PatchTestWarningError.java
41159 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 816883626* @summary Basic argument validation for --patch-module27* @library /test/lib28* @modules jdk.compiler29* @build PatchTestWarningError30* jdk.test.lib.compiler.CompilerUtils31* jdk.test.lib.util.JarUtils32* @run testng PatchTestWarningError33*/3435import java.io.File;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.stream.Collectors;40import java.util.stream.Stream;4142import jdk.test.lib.compiler.CompilerUtils;43import jdk.test.lib.util.JarUtils;44import static jdk.test.lib.process.ProcessTools.*;4546import org.testng.annotations.BeforeTest;47import org.testng.annotations.DataProvider;48import org.testng.annotations.Test;49import static org.testng.Assert.*;505152/**53* This test54* See PatchTestWarningError for test description.55*/5657@Test58public class PatchTestWarningError {5960// top-level source directory61private static final String TEST_SRC = System.getProperty("test.src");6263// source/destination tree for the test module64private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");65private static final Path MODS_DIR = Paths.get("mods");6667// source/destination tree for patch tree 168private static final Path SRC1_DIR = Paths.get(TEST_SRC, "src1");69private static final Path PATCHES1_DIR = Paths.get("patches1");7071// source/destination tree for patch tree 272private static final Path SRC2_DIR = Paths.get(TEST_SRC, "src2");73private static final Path PATCHES2_DIR = Paths.get("patches2");7475// patch path for java.base76private static final String PATCHES_PATH =77PATCHES1_DIR.resolve("java.base") + File.pathSeparator +78PATCHES2_DIR.resolve("java.base");7980// the classes overridden or added with --patch-module81private static final String[] CLASSES = {8283// java.base = boot loader84"java.base/java.text.Annotation", // override class85"java.base/java.text.AnnotationBuddy", // add class to package86"java.base/java.lang2.Object", // new package8788};899091@BeforeTest92public void setup() throws Exception {9394// javac -d mods/test src/test/**95boolean compiled= CompilerUtils.compile(SRC_DIR.resolve("test"),96MODS_DIR.resolve("test"));97assertTrue(compiled, "classes did not compile");9899// javac --patch-module $MODULE=patches1/$MODULE -d patches1/$MODULE patches1/$MODULE/**100Path src = SRC1_DIR.resolve("java.base");101Path output = PATCHES1_DIR.resolve(src.getFileName());102Files.createDirectories(output);103String mn = src.getFileName().toString();104compiled = CompilerUtils.compile(src, output,105"--patch-module", mn + "=" + src.toString());106assertTrue(compiled, "classes did not compile");107108// javac --patch-module $MODULE=patches2/$MODULE -d patches2/$MODULE patches2/$MODULE/**109src = SRC2_DIR.resolve("java.base");110output = PATCHES2_DIR.resolve(src.getFileName());111Files.createDirectories(output);112mn = src.getFileName().toString();113compiled = CompilerUtils.compile(src, output,114"--patch-module", mn + "=" + src.toString());115assertTrue(compiled, "classes did not compile");116117}118119/**120* Test with --patch-module options patching the same module121*/122public void testDuplicateModule() throws Exception {123int exitValue =124executeTestJava("--patch-module", "java.base=" + PATCHES1_DIR.resolve("java.base"),125"--patch-module", "java.base=" + PATCHES2_DIR.resolve("java.base"),126"--module-path", MODS_DIR.toString(),127"-m", "test/jdk.test.Main")128.outputTo(System.out)129.errorTo(System.out)130// error output by VM131.shouldContain("Cannot specify java.base more than once to --patch-module")132.getExitValue();133134assertTrue(exitValue != 0);135}136137@DataProvider(name = "emptyItem")138public Object[][] emptyItems() {139String patch1 = PATCHES1_DIR.resolve("java.base").toString();140String patch2 = PATCHES2_DIR.resolve("java.base").toString();141String pathSep = File.pathSeparator;142return new Object[][]{143144{ "java.base="+ pathSep + patch1 + pathSep + patch2, null },145{ "java.base="+ patch1 + pathSep + pathSep + patch2, null },146{ "java.base="+ patch1 + pathSep + patch2 + pathSep + pathSep, null },147};148}149150/**151* Empty item in a non-empty path list152*/153@Test(dataProvider = "emptyItem")154public void testEmptyItem(String value, String msg) throws Exception {155// the argument to the test is the list of classes overridden or added156String arg = Stream.of(CLASSES).collect(Collectors.joining(","));157158int exitValue =159executeTestJava("--patch-module", value,160"--add-exports", "java.base/java.lang2=test",161"--module-path", MODS_DIR.toString(),162"-m", "test/jdk.test.Main", arg)163.outputTo(System.out)164.errorTo(System.out)165.getExitValue();166167assertTrue(exitValue == 0);168}169170/**171* Test bad module name that should emit a warning172*/173public void testBadName() throws Exception {174// the argument to the test is the list of classes overridden or added175String arg = Stream.of(CLASSES).collect(Collectors.joining(","));176177int exitValue =178executeTestJava("--patch-module", "DoesNotExist=tmp",179"--patch-module", "java.base=" + PATCHES_PATH,180"--add-exports", "java.base/java.lang2=test",181"--module-path", MODS_DIR.toString(),182"-m", "test/jdk.test.Main", arg)183.outputTo(System.out)184.errorTo(System.out)185.shouldContain("WARNING: Unknown module: DoesNotExist specified to --patch-module")186.getExitValue();187188assertTrue(exitValue == 0);189}190191@DataProvider(name = "badArguments")192public Object[][] badArguments() {193return new Object[][]{194195// source not found196{ "=tmp", "Unable to parse --patch-module <module>=<value>: =tmp" },197198// target not found: check by VM199{ "java.base", "Missing '=' in --patch-module specification" },200{ "foo", "Missing '=' in --patch-module specification" },201202// target not found203{ "java.base=", "Unable to parse --patch-module <module>=<value>: java.base=" },204{ "java.base=" + File.pathSeparator,205"Target must be specified: --patch-module java.base=" + File.pathSeparator }206};207}208209/**210* Test ill-formed argument to --patch-module211*/212@Test(dataProvider = "badArguments")213public void testBadArgument(String value, String msg) throws Exception {214int exitValue =215executeTestJava("--patch-module", value,216"--module-path", MODS_DIR.toString(),217"-m", "test/jdk.test.Main")218.outputTo(System.out)219.errorTo(System.out)220.shouldContain(msg)221.getExitValue();222223assertTrue(exitValue != 0);224}225}226227228