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-exports
  28  * @library /lib/testlibrary
  29  * @modules jdk.compiler
  30  * @build AddExportsTestWarningError CompilerUtils ModuleInfoMaker
  31  * @build jdk.testlibrary.*
  32  * @run testng AddExportsTestWarningError
  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 AddExportsTestWarningError {
  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 M3_MAIN = "m3/p3.C3";
  59 
  60     @BeforeTest
  61     public void setup() throws Exception {
  62         ModuleInfoMaker builder = new ModuleInfoMaker(SRC_DIR);
  63         builder.writeJavaFiles("m1",
  64             "module m1 { }",
  65             "package p1; public class C1 { " +
  66                 "    public static void main(String... args) {}" +
  67                 "}");
  68 
  69         builder.writeJavaFiles("m2",
  70             "module m2 { requires m1; exports p2; }",
  71             "package p2; public class C2 {  private p1.C1 c1; }");
  72 
  73         builder.writeJavaFiles("m3",
  74             "module m3 { requires m2; }",
  75             "package p3; class C3 { " +
  76                 "    p1.C1 c; " +
  77                 "    public static void main(String... args) { new p2.C2(); }" +
  78                 "}");
  79 
  80         builder.compile("m1", MODS_DIR);
  81         builder.compile("m2", MODS_DIR, "--add-exports", "m1/p1=m2");
  82         builder.compile("m3", MODS_DIR, "--add-exports", "m1/p1=m3");
  83     }
  84 
  85 
  86     @DataProvider(name = "goodcases")
  87     public Object[][] goodCases() {
  88         return new Object[][]{
  89 
  90             // empty items
  91             { "m1/p1=,m2,m3",       null },
  92             { "m1/p1=m2,,m3",       null },
  93             { "m1/p1=m2,m3,",       null },
  94 
  95             // duplicates
  96             { "m1/p1=m2,m2,m3,,",   null },
  97 
  98         };
  99     }
 100 
 101 
 102     @Test(dataProvider = "goodcases")
 103     public void test(String value, String ignore) throws Exception {
 104         testNoWarning(value);
 105     }
 106 
 107 
 108     @DataProvider(name = "illFormedAddExports")
 109     public Object[][] illFormedAddExports() {
 110         return new Object[][]{
 111             { "m1",         "Unable to parse --add-exports <module>=<value>: m1"},
 112 
 113             // missing source part
 114             { "=m2",        "Unable to parse --add-exports <module>=<value>: =m2"},
 115             { "/=m2",       "Unable to parse --add-exports <module>/<package>: /" },
 116             { "m1=m2",      "Unable to parse --add-exports <module>/<package>: m1" },
 117             { "/p1=m2",     "Unable to parse --add-exports <module>/<package>: /p1" },
 118             { "m1p1=m2",    "Unable to parse --add-exports <module>/<package>: m1p1" },
 119 
 120             // empty list, missing target
 121             { "m1/p1=",     "Unable to parse --add-exports <module>=<value>: m1/p1=" },
 122             { "m1/p1=,,",   "Target must be specified: --add-exports m1/p1=,," },
 123         };
 124     }
 125 
 126     @Test(dataProvider = "illFormedAddExports")
 127     public void testIllFormedAddExports(String value, String msg) throws Exception {
 128         testError(value, msg);
 129     }
 130 
 131 
 132     @DataProvider(name = "unknownNames")
 133     public Object[][] unknownNames() {
 134         return new Object[][]{
 135 
 136             // source not found
 137             {"DoesNotExist/p=m1",  "WARNING: Unknown module: DoesNotExist specified in --add-exports"},
 138             {"m1/DoesNotExist=m2", "WARNING: package DoesNotExist not in m1"},
 139 
 140             // target not found
 141             {"m1/p1=DoesNotExist", "WARNING: Unknown module: DoesNotExist specified in --add-exports"},
 142 
 143             // bad names
 144             {"m*/p1=m2",           "WARNING: Unknown module: m* specified in --add-exports"},
 145             {"m1/p!=m2",           "WARNING: package p! not in m1"},
 146             {"m1/p1=m!",           "WARNING: Unknown module: m! specified in --add-exports"},
 147 
 148         };
 149     }
 150 
 151 
 152     @Test(dataProvider = "unknownNames")
 153     public void testUnknownNames(String value, String msg) throws Exception {
 154         testWarning(value, msg);
 155     }
 156 
 157 
 158     @DataProvider(name = "missingArguments")
 159     public Object[][] missingArguments() {
 160         return new Object[][]{
 161             { new String[] { "--add-exports" },
 162                 "Error: --add-exports requires modules to be specified"},
 163 
 164             { new String[] { "--add-exports=" },
 165                 "Error: --add-exports= requires modules to be specified" },
 166 
 167             { new String[] { "--add-exports", "" },
 168                 "Error: --add-exports requires modules to be specified"}
 169 
 170         };
 171     }
 172 
 173 
 174     @Test(dataProvider = "missingArguments")
 175     public void testMissingArguments(String[] options, String msg) throws Exception {
 176         String[] args = Stream.concat(Arrays.stream(options),
 177                                       Stream.of("-version"))
 178                               .toArray(String[]::new);
 179         int exitValue = executeTestJava(args)
 180             .outputTo(System.out)
 181             .errorTo(System.out)
 182             .shouldContain(msg)
 183             .getExitValue();
 184 
 185         assertTrue(exitValue != 0);
 186     }
 187 
 188      private void testWarning(String value, String msg) throws Exception {
 189         int exitValue =
 190             executeTestJava("--add-exports", value,
 191                             "--module-path", MODS_DIR.toString(),
 192                             "-m", M1_MAIN)
 193                 .outputTo(System.out)
 194                 .errorTo(System.out)
 195                 .shouldContain(msg)
 196                 .getExitValue();
 197 
 198         assertTrue(exitValue == 0);
 199     }
 200 
 201     private void testError(String value, String msg) throws Exception {
 202         int exitValue =
 203             executeTestJava("--add-exports", value,
 204                             "--module-path", MODS_DIR.toString(),
 205                             "-m", M1_MAIN)
 206                 .outputTo(System.out)
 207                 .errorTo(System.out)
 208                 .shouldContain(msg)
 209                 .getExitValue();
 210 
 211         assertTrue(exitValue != 0);
 212     }
 213 
 214     private void testNoWarning(String value) throws Exception {
 215         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 216         PrintStream ps = new PrintStream(new BufferedOutputStream(baos));
 217         OutputAnalyzer outputAnalyzer =
 218             executeTestJava("--add-exports", value,
 219                             "--module-path", MODS_DIR.toString(),
 220                             "-m", M3_MAIN)
 221                 .outputTo(ps)
 222                 .errorTo(ps);
 223 
 224         assertTrue(outputAnalyzer.getExitValue() == 0);
 225 
 226         System.out.println(baos.toString());
 227         String[] output = baos.toString().split("\\R");
 228         assertFalse(Arrays.stream(output)
 229                           .filter(s -> !s.matches("WARNING: Module name .* may soon be illegal"))
 230                           .filter(s -> s.startsWith("WARNING:"))
 231                           .findAny().isPresent());
 232 
 233     }
 234 }