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
  29  * @modules jdk.compiler
  30  * @build AddReadsTestWarningError CompilerUtils ModuleInfoMaker
  31  * @build jdk.testlibrary.*
  32  * @run testng AddReadsTestWarningError
  33  */
  34 
  35 import java.io.BufferedOutputStream;
  36 import java.io.ByteArrayOutputStream;
  37 import java.io.PrintStream;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 import java.util.stream.Stream;
  42 
  43 import jdk.testlibrary.OutputAnalyzer;
  44 import static jdk.testlibrary.ProcessTools.*;
  45 
  46 import org.testng.annotations.BeforeTest;
  47 import org.testng.annotations.DataProvider;
  48 import org.testng.annotations.Test;
  49 import static org.testng.Assert.*;
  50 
  51 
  52 @Test
  53 public class AddReadsTestWarningError {
  54 
  55     private static final Path MODS_DIR = Paths.get("mods");
  56     private static final Path SRC_DIR = Paths.get("src");
  57     private static final String M1_MAIN = "m1/p1.C1";
  58     private static final String M4_MAIN = "m4/p4.C4";
  59 
  60     @BeforeTest
  61     public void setup() throws Exception {
  62         ModuleInfoMaker builder = new ModuleInfoMaker(SRC_DIR);
  63         builder.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         );
  72 
  73         builder.writeJavaFiles("m2",
  74             "module m2 { exports p2; }",
  75             "package p2; public class C2 { }"
  76         );
  77 
  78         builder.writeJavaFiles("m3",
  79             "module m3 { exports p3; }",
  80             "package p3; public class C3 { }"
  81         );
  82 
  83         builder.writeJavaFiles("m4",
  84             "module m4 { requires m2; requires m3; }",
  85             "package p4; public class C4 { " +
  86             "    public static void main(String... args) {}" +
  87             "}"
  88         );
  89 
  90         builder.compile("m2", MODS_DIR);
  91         builder.compile("m3", MODS_DIR);
  92         builder.compile("m4", MODS_DIR);
  93         builder.compile("m1", MODS_DIR, "--add-reads", "m1=m2,m3");
  94     }
  95 
  96 
  97     @DataProvider(name = "goodcases")
  98     public Object[][] goodCases() {
  99         return new Object[][]{
 100             // empty items
 101             { "m1=,m2,m3",       null },
 102             { "m1=m2,,m3",       null },
 103             { "m1=m2,m3,",       null },
 104 
 105             // duplicates
 106             { "m1=m2,m2,m3,,",  null },
 107 
 108         };
 109     }
 110 
 111 
 112     @Test(dataProvider = "goodcases")
 113     public void test(String value, String ignore) throws Exception {
 114         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 115         PrintStream ps = new PrintStream(new BufferedOutputStream(baos));
 116         OutputAnalyzer outputAnalyzer =
 117             executeTestJava("--add-reads", value,
 118                             "--module-path", MODS_DIR.toString(),
 119                             "-m", M1_MAIN)
 120                 .outputTo(ps)
 121                 .errorTo(ps);
 122 
 123         assertTrue(outputAnalyzer.getExitValue() == 0);
 124 
 125         System.out.println(baos.toString());
 126         String[] output = baos.toString().split("\\R");
 127         assertFalse(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     }
 132 
 133 
 134     @DataProvider(name = "illFormedAddReads")
 135     public Object[][] illFormedAddReads() {
 136         return new Object[][]{
 137             { "m1",         "Unable to parse --add-reads <module>=<value>: m1" },
 138 
 139             // missing source part
 140             { "=m2",        "Unable to parse --add-reads <module>=<value>: =m2" },
 141 
 142             // empty list, missing target
 143             { "m1=",        "Unable to parse --add-reads <module>=<value>: m1=" },
 144 
 145             // empty list
 146             { "m1=,,",      "Target must be specified: --add-reads m1=,," },
 147         };
 148     }
 149 
 150 
 151     @Test(dataProvider = "illFormedAddReads")
 152     public void testIllFormedAddReads(String value, String msg) throws Exception {
 153         int exitValue =
 154             executeTestJava("--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();
 161 
 162         assertTrue(exitValue != 0);
 163     }
 164 
 165 
 166     @DataProvider(name = "unknownNames")
 167     public Object[][] unknownNames() {
 168         return new Object[][]{
 169 
 170             // source not found
 171             {"DoesNotExist=m2",    "WARNING: Unknown module: DoesNotExist specified to --add-reads"},
 172 
 173             // target not found
 174             {"m2=DoesNotExist",    "WARNING: Unknown module: DoesNotExist specified to --add-reads"},
 175 
 176             // bad names
 177             {"m*=m2",              "WARNING: Unknown module: m* specified to --add-reads"},
 178             {"m2=m!",              "WARNING: Unknown module: m! specified to --add-reads"},
 179 
 180         };
 181     }
 182 
 183     @Test(dataProvider = "unknownNames")
 184     public void testUnknownNames(String value, String msg) throws Exception {
 185         int exitValue =
 186             executeTestJava("--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();
 193 
 194         assertTrue(exitValue == 0);
 195     }
 196 
 197 
 198     @DataProvider(name = "missingArguments")
 199     public Object[][] missingArguments() {
 200         return new Object[][]{
 201             { new String[] {"--add-reads" },
 202                 "Error: --add-reads requires modules to be specified"},
 203 
 204             { new String[] { "--add-reads=" },
 205                 "Error: --add-reads= requires modules to be specified"},
 206 
 207             { new String[] { "--add-reads", "" },
 208                 "Error: --add-reads requires modules to be specified"},
 209         };
 210     }
 211 
 212     @Test(dataProvider = "missingArguments")
 213     public void testEmptyArgument(String[] options, String msg) throws Exception {
 214         String[] args = Stream.concat(Arrays.stream(options), Stream.of("-version"))
 215                               .toArray(String[]::new);
 216         int exitValue = executeTestJava(args)
 217             .outputTo(System.out)
 218             .errorTo(System.out)
 219             .shouldContain(msg)
 220             .getExitValue();
 221 
 222         assertTrue(exitValue != 0);
 223     }
 224 }