1 /*
   2  * Copyright (c) 2015, 2017, 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  * @library /lib/testlibrary /test/lib
  27  * @modules jdk.compiler
  28  * @build AddReadsTest jdk.test.lib.compiler.CompilerUtils JarUtils jdk.testlibrary.*
  29  * @run testng AddReadsTest
  30  * @summary Basic tests for java --add-reads
  31  */
  32 
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 
  36 import jdk.test.lib.compiler.CompilerUtils;
  37 import jdk.testlibrary.OutputAnalyzer;
  38 import static jdk.testlibrary.ProcessTools.*;
  39 
  40 import org.testng.annotations.BeforeTest;
  41 import org.testng.annotations.DataProvider;
  42 import org.testng.annotations.Test;
  43 import static org.testng.Assert.*;
  44 
  45 /**
  46  * The tests consists of two modules: m1 and junit.
  47  * Code in module m1 calls into code in module junit but the module-info.java
  48  * does not have a 'requires'. Instead a read edge is added via the command
  49  * line option --add-reads.
  50  */
  51 
  52 @Test
  53 public class AddReadsTest {
  54 
  55     private static final String TEST_SRC = System.getProperty("test.src");
  56 
  57     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  58     private static final Path CLASSES_DIR = Paths.get("classes");
  59     private static final Path MODS_DIR = Paths.get("mods");
  60 
  61     private static final String MAIN = "m1/p.Main";
  62 
  63 
  64     @BeforeTest
  65     public void setup() throws Exception {
  66 
  67         // javac -d classes src/junit/**
  68         assertTrue(CompilerUtils
  69             .compile(SRC_DIR.resolve("junit"), CLASSES_DIR));
  70 
  71         // javac -d mods/m1 -cp classes/ src/m1/**
  72         assertTrue(CompilerUtils
  73             .compile(SRC_DIR.resolve("m1"),
  74                     MODS_DIR.resolve("m1"),
  75                     "-cp", CLASSES_DIR.toString(),
  76                     "--add-reads", "m1=ALL-UNNAMED"));
  77 
  78         // jar cf mods/junit.jar -C classes .
  79         JarUtils.createJarFile(MODS_DIR.resolve("junit.jar"), CLASSES_DIR);
  80 
  81     }
  82 
  83     private OutputAnalyzer run(String... options) throws Exception {
  84         return executeTestJava(options)
  85             .outputTo(System.out)
  86             .errorTo(System.out);
  87     }
  88 
  89 
  90     /**
  91      * Run with junit as a module on the module path.
  92      */
  93     public void testJUnitOnModulePath() throws Exception {
  94 
  95         // java --module-path mods --add-modules junit --add-reads m1=junit -m ..
  96         int exitValue
  97             = run("--module-path", MODS_DIR.toString(),
  98                   "--add-modules", "junit",
  99                   "--add-reads", "m1=junit",
 100                   "-m", MAIN)
 101                 .getExitValue();
 102 
 103         assertTrue(exitValue == 0);
 104     }
 105 
 106 
 107     /**
 108      * Exercise --add-reads m1=ALL-UNNAMED by running with junit on the
 109      * class path.
 110      */
 111     public void testJUnitOnClassPath() throws Exception {
 112 
 113         // java --module-path mods -cp mods/junit.jar --add-reads m1=ALL-UNNAMED -m ..
 114         String cp = MODS_DIR.resolve("junit.jar").toString();
 115         int exitValue
 116             = run("--module-path", MODS_DIR.toString(),
 117                   "-cp", cp,
 118                   "--add-reads", "m1=ALL-UNNAMED",
 119                   "-m", MAIN)
 120                 .getExitValue();
 121 
 122         assertTrue(exitValue == 0);
 123     }
 124 
 125 
 126     /**
 127      * Run with junit as a module on the module path but without --add-reads.
 128      */
 129     public void testJUnitOnModulePathMissingAddReads() throws Exception {
 130         // java --module-path mods --add-modules junit --module ..
 131         int exitValue
 132             = run("--module-path", MODS_DIR.toString(),
 133                   "--add-modules", "junit",
 134                   "--module", MAIN)
 135                 .shouldContain("IllegalAccessError")
 136                 .getExitValue();
 137 
 138         assertTrue(exitValue != 0);
 139     }
 140 
 141 
 142     /**
 143      * Run with junit on the class path but without --add-reads.
 144      */
 145     public void testJUnitOnClassPathMissingAddReads() throws Exception {
 146         // java --module-path mods -cp mods/junit.jar -m ..
 147         String cp = MODS_DIR.resolve("junit.jar").toString();
 148         int exitValue
 149             = run("--module-path", MODS_DIR.toString(),
 150                   "-cp", cp,
 151                   "-m", MAIN)
 152                 .shouldContain("IllegalAccessError")
 153                 .getExitValue();
 154 
 155         assertTrue(exitValue != 0);
 156     }
 157 
 158 
 159     /**
 160      * Exercise --add-reads with a more than one source module.
 161      */
 162     public void testJUnitWithMultiValueOption() throws Exception {
 163 
 164         int exitValue
 165             = run("--module-path", MODS_DIR.toString(),
 166                   "--add-modules", "java.xml,junit",
 167                   "--add-reads", "m1=java.xml,junit",
 168                   "--module", MAIN)
 169                 .getExitValue();
 170 
 171         assertTrue(exitValue == 0);
 172     }
 173 
 174 
 175     /**
 176      * Exercise --add-reads where the target module is specified more than once
 177      */
 178     public void testWithTargetSpecifiedManyTimes() throws Exception {
 179 
 180         int exitValue
 181             = run("--module-path", MODS_DIR.toString(),
 182                   "--add-modules", "java.xml,junit",
 183                   "--add-reads", "m1=java.xml",
 184                   "--add-reads", "m1=junit",
 185                   "-m", MAIN)
 186                  .getExitValue();
 187 
 188         assertTrue(exitValue == 0);
 189     }
 190 
 191 
 192     /**
 193      * Exercise --add-reads with missing source
 194      */
 195     public void testWithMissingSource() throws Exception {
 196 
 197         //  --add-exports $VALUE -version
 198         assertTrue(run("--add-reads", "java.base", "-version").getExitValue() != 0);
 199     }
 200 
 201 
 202     /**
 203      * Exercise --add-reads with unknown source/target module.
 204      * Warning is emitted.
 205      */
 206     @Test(dataProvider = "badvalues")
 207     public void testWithBadValue(String value, String ignore) throws Exception {
 208 
 209         //  --add-exports $VALUE -version
 210         int exitValue = run("--add-reads", value, "-version")
 211                             .stderrShouldMatch("WARNING: Unknown module: .*.monkey")
 212                             .outputTo(System.out)
 213                             .errorTo(System.out)
 214                             .getExitValue();
 215 
 216         assertTrue(exitValue == 0);
 217     }
 218 
 219     @DataProvider(name = "badvalues")
 220     public Object[][] badValues() {
 221         return new Object[][]{
 222 
 223             { "java.monkey=java.base",      null }, // unknown module
 224             { "java.base=sun.monkey",       null }, // unknown source
 225 
 226         };
 227     }
 228 }