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