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