1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8168836
  27  * @summary  Basic argument validation for --add-reads
  28  * @library /lib/testlibrary /test/lib
  29  * @modules jdk.compiler
  30  * @build jdk.test.lib.compiler.ModuleInfoMaker
  31  * @build jdk.test.lib.compiler.CompilerUtils
  32  * @build jdk.testlibrary.*
  33  * @build AddReadsTestWarningError
  34  * @run testng AddReadsTestWarningError
  35  */
  36 
  37 import java.io.BufferedOutputStream;
  38 import java.io.ByteArrayOutputStream;
  39 import java.io.PrintStream;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.Arrays;
  43 import java.util.stream.Stream;
  44 
  45 import jdk.test.lib.compiler.ModuleInfoMaker;
  46 import jdk.testlibrary.OutputAnalyzer;
  47 import static jdk.testlibrary.ProcessTools.*;
  48 
  49 import org.testng.annotations.BeforeTest;
  50 import org.testng.annotations.DataProvider;
  51 import org.testng.annotations.Test;
  52 import static org.testng.Assert.*;
  53 
  54 @Test
  55 public class AddReadsTestWarningError {
  56 
  57     private static final Path MODS_DIR = Paths.get("mods");
  58     private static final Path SRC_DIR = Paths.get("src");
  59     private static final String M1_MAIN = "m1/p1.C1";
  60     private static final String M4_MAIN = "m4/p4.C4";
  61 
  62     @BeforeTest
  63     public void setup() throws Exception {
  64         ModuleInfoMaker builder = new ModuleInfoMaker(SRC_DIR);
  65         builder.writeJavaFiles("m1",
  66             "module m1 { requires m4; }",
  67             "package p1; public class C1 { " +
  68             "    public static void main(String... args) {" +
  69             "        p2.C2 c2 = new p2.C2();" +
  70             "        p3.C3 c3 = new p3.C3();" +
  71             "    }" +
  72             "}"
  73         );
  74 
  75         builder.writeJavaFiles("m2",
  76             "module m2 { exports p2; }",
  77             "package p2; public class C2 { }"
  78         );
  79 
  80         builder.writeJavaFiles("m3",
  81             "module m3 { exports p3; }",
  82             "package p3; public class C3 { }"
  83         );
  84 
  85         builder.writeJavaFiles("m4",
  86             "module m4 { requires m2; requires m3; }",
  87             "package p4; public class C4 { " +
  88             "    public static void main(String... args) {}" +
  89             "}"
  90         );
  91 
  92         builder.compile("m2", MODS_DIR);
  93         builder.compile("m3", MODS_DIR);
  94         builder.compile("m4", MODS_DIR);
  95         builder.compile("m1", MODS_DIR, "--add-reads", "m1=m2,m3");
  96     }
  97 
  98 
  99     @DataProvider(name = "goodcases")
 100     public Object[][] goodCases() {
 101         return new Object[][]{
 102             // empty items
 103             { "m1=,m2,m3",       null },
 104             { "m1=m2,,m3",       null },
 105             { "m1=m2,m3,",       null },
 106 
 107             // duplicates
 108             { "m1=m2,m2,m3,,",  null },
 109 
 110         };
 111     }
 112 
 113 
 114     @Test(dataProvider = "goodcases")
 115     public void test(String value, String ignore) throws Exception {
 116         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 117         PrintStream ps = new PrintStream(new BufferedOutputStream(baos));
 118         OutputAnalyzer outputAnalyzer =
 119             executeTestJava("--add-reads", value,
 120                             "--module-path", MODS_DIR.toString(),
 121                             "-m", M1_MAIN)
 122                 .outputTo(ps)
 123                 .errorTo(ps);
 124 
 125         assertTrue(outputAnalyzer.getExitValue() == 0);
 126 
 127         System.out.println(baos.toString());
 128         String[] output = baos.toString().split("\\R");
 129         assertFalse(Arrays.stream(output)
 130                           .filter(s -> !s.matches("WARNING: Module name .* may soon be illegal"))
 131                           .filter(s -> s.startsWith("WARNING:"))
 132                           .findAny().isPresent());
 133     }
 134 
 135 
 136     @DataProvider(name = "illFormedAddReads")
 137     public Object[][] illFormedAddReads() {
 138         return new Object[][]{
 139             { "m1",         "Unable to parse --add-reads <module>=<value>: m1" },
 140 
 141             // missing source part
 142             { "=m2",        "Unable to parse --add-reads <module>=<value>: =m2" },
 143 
 144             // empty list, missing target
 145             { "m1=",        "Unable to parse --add-reads <module>=<value>: m1=" },
 146 
 147             // empty list
 148             { "m1=,,",      "Target must be specified: --add-reads m1=,," },
 149         };
 150     }
 151 
 152 
 153     @Test(dataProvider = "illFormedAddReads")
 154     public void testIllFormedAddReads(String value, String msg) throws Exception {
 155         int exitValue =
 156             executeTestJava("--add-reads", value,
 157                             "--module-path", MODS_DIR.toString(),
 158                             "-m", M4_MAIN)
 159                 .outputTo(System.out)
 160                 .errorTo(System.out)
 161                 .shouldContain(msg)
 162                 .getExitValue();
 163 
 164         assertTrue(exitValue != 0);
 165     }
 166 
 167 
 168     @DataProvider(name = "unknownNames")
 169     public Object[][] unknownNames() {
 170         return new Object[][]{
 171 
 172             // source not found
 173             {"DoesNotExist=m2",    "WARNING: Unknown module: DoesNotExist specified to --add-reads"},
 174 
 175             // target not found
 176             {"m2=DoesNotExist",    "WARNING: Unknown module: DoesNotExist specified to --add-reads"},
 177 
 178             // bad names
 179             {"m*=m2",              "WARNING: Unknown module: m* specified to --add-reads"},
 180             {"m2=m!",              "WARNING: Unknown module: m! specified to --add-reads"},
 181 
 182         };
 183     }
 184 
 185     @Test(dataProvider = "unknownNames")
 186     public void testUnknownNames(String value, String msg) throws Exception {
 187         int exitValue =
 188             executeTestJava("--add-reads", value,
 189                             "--module-path", MODS_DIR.toString(),
 190                             "-m", M4_MAIN)
 191                 .outputTo(System.out)
 192                 .errorTo(System.out)
 193                 .shouldContain(msg)
 194                 .getExitValue();
 195 
 196         assertTrue(exitValue == 0);
 197     }
 198 
 199 
 200     @DataProvider(name = "missingArguments")
 201     public Object[][] missingArguments() {
 202         return new Object[][]{
 203             { new String[] {"--add-reads" },
 204                 "Error: --add-reads requires modules to be specified"},
 205 
 206             { new String[] { "--add-reads=" },
 207                 "Error: --add-reads= requires modules to be specified"},
 208 
 209             { new String[] { "--add-reads", "" },
 210                 "Error: --add-reads requires modules to be specified"},
 211         };
 212     }
 213 
 214     @Test(dataProvider = "missingArguments")
 215     public void testEmptyArgument(String[] options, String msg) throws Exception {
 216         String[] args = Stream.concat(Arrays.stream(options), Stream.of("-version"))
 217                               .toArray(String[]::new);
 218         int exitValue = executeTestJava(args)
 219             .outputTo(System.out)
 220             .errorTo(System.out)
 221             .shouldContain(msg)
 222             .getExitValue();
 223 
 224         assertTrue(exitValue != 0);
 225     }
 226 }