Path: blob/master/test/jdk/tools/launcher/modules/addreads/AddReadsTestWarningError.java
41155 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 --add-reads27* @library /test/lib28* @modules jdk.compiler29* @build jdk.test.lib.compiler.ModuleInfoMaker30* @build jdk.test.lib.compiler.CompilerUtils31* @build AddReadsTestWarningError32* @run testng AddReadsTestWarningError33*/3435import java.io.BufferedOutputStream;36import java.io.ByteArrayOutputStream;37import java.io.PrintStream;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.Arrays;41import java.util.stream.Stream;4243import jdk.test.lib.compiler.ModuleInfoMaker;44import jdk.test.lib.process.OutputAnalyzer;45import static jdk.test.lib.process.ProcessTools.*;4647import org.testng.annotations.BeforeTest;48import org.testng.annotations.DataProvider;49import org.testng.annotations.Test;50import static org.testng.Assert.*;5152@Test53public class AddReadsTestWarningError {5455private static final Path MODS_DIR = Paths.get("mods");56private static final Path SRC_DIR = Paths.get("src");57private static final String M1_MAIN = "m1/p1.C1";58private static final String M4_MAIN = "m4/p4.C4";5960@BeforeTest61public void setup() throws Exception {62ModuleInfoMaker builder = new ModuleInfoMaker(SRC_DIR);63builder.writeJavaFiles("m1",64"module m1 { requires m4; }",65"package p1; public class C1 { " +66" public static void main(String... args) {" +67" p2.C2 c2 = new p2.C2();" +68" p3.C3 c3 = new p3.C3();" +69" }" +70"}"71);7273builder.writeJavaFiles("m2",74"module m2 { exports p2; }",75"package p2; public class C2 { }"76);7778builder.writeJavaFiles("m3",79"module m3 { exports p3; }",80"package p3; public class C3 { }"81);8283builder.writeJavaFiles("m4",84"module m4 { requires m2; requires m3; }",85"package p4; public class C4 { " +86" public static void main(String... args) {}" +87"}"88);8990builder.compile("m2", MODS_DIR);91builder.compile("m3", MODS_DIR);92builder.compile("m4", MODS_DIR);93builder.compile("m1", MODS_DIR, "--add-reads", "m1=m2,m3");94}959697@DataProvider(name = "goodcases")98public Object[][] goodCases() {99return new Object[][]{100// empty items101{ "m1=,m2,m3", null },102{ "m1=m2,,m3", null },103{ "m1=m2,m3,", null },104105// duplicates106{ "m1=m2,m2,m3,,", null },107108};109}110111112@Test(dataProvider = "goodcases")113public void test(String value, String ignore) throws Exception {114ByteArrayOutputStream baos = new ByteArrayOutputStream();115PrintStream ps = new PrintStream(new BufferedOutputStream(baos));116OutputAnalyzer outputAnalyzer =117executeTestJava("--add-reads", value,118"--module-path", MODS_DIR.toString(),119"-m", M1_MAIN)120.outputTo(ps)121.errorTo(ps);122123assertTrue(outputAnalyzer.getExitValue() == 0);124125System.out.println(baos.toString());126String[] output = baos.toString().split("\\R");127assertFalse(Arrays.stream(output)128.filter(s -> !s.matches("WARNING: Module name .* may soon be illegal"))129.filter(s -> s.startsWith("WARNING:"))130.findAny().isPresent());131}132133134@DataProvider(name = "illFormedAddReads")135public Object[][] illFormedAddReads() {136return new Object[][]{137{ "m1", "Unable to parse --add-reads <module>=<value>: m1" },138139// missing source part140{ "=m2", "Unable to parse --add-reads <module>=<value>: =m2" },141142// empty list, missing target143{ "m1=", "Unable to parse --add-reads <module>=<value>: m1=" },144145// empty list146{ "m1=,,", "Target must be specified: --add-reads m1=,," },147};148}149150151@Test(dataProvider = "illFormedAddReads")152public void testIllFormedAddReads(String value, String msg) throws Exception {153int exitValue =154executeTestJava("--add-reads", value,155"--module-path", MODS_DIR.toString(),156"-m", M4_MAIN)157.outputTo(System.out)158.errorTo(System.out)159.shouldContain(msg)160.getExitValue();161162assertTrue(exitValue != 0);163}164165166@DataProvider(name = "unknownNames")167public Object[][] unknownNames() {168return new Object[][]{169170// source not found171{"DoesNotExist=m2", "WARNING: Unknown module: DoesNotExist specified to --add-reads"},172173// target not found174{"m2=DoesNotExist", "WARNING: Unknown module: DoesNotExist specified to --add-reads"},175176// bad names177{"m*=m2", "WARNING: Unknown module: m* specified to --add-reads"},178{"m2=m!", "WARNING: Unknown module: m! specified to --add-reads"},179180};181}182183@Test(dataProvider = "unknownNames")184public void testUnknownNames(String value, String msg) throws Exception {185int exitValue =186executeTestJava("--add-reads", value,187"--module-path", MODS_DIR.toString(),188"-m", M4_MAIN)189.outputTo(System.out)190.errorTo(System.out)191.shouldContain(msg)192.getExitValue();193194assertTrue(exitValue == 0);195}196197198@DataProvider(name = "missingArguments")199public Object[][] missingArguments() {200return new Object[][]{201{ new String[] {"--add-reads" },202"Error: --add-reads requires modules to be specified"},203204{ new String[] { "--add-reads=" },205"Error: --add-reads= requires modules to be specified"},206207{ new String[] { "--add-reads", "" },208"Error: --add-reads requires modules to be specified"},209};210}211212@Test(dataProvider = "missingArguments")213public void testEmptyArgument(String[] options, String msg) throws Exception {214String[] args = Stream.concat(Arrays.stream(options), Stream.of("-version"))215.toArray(String[]::new);216int exitValue = executeTestJava(args)217.outputTo(System.out)218.errorTo(System.out)219.shouldContain(msg)220.getExitValue();221222assertTrue(exitValue != 0);223}224}225226227