1 /*
   2  * Copyright (c) 2014, 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  * @library /lib/testlibrary
  27  * @modules jdk.compiler
  28  * @build AddExportsTest CompilerUtils jdk.testlibrary.*
  29  * @run testng AddExportsTest
  30  * @summary Basic tests for java -XaddExports
  31  */
  32 
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 
  36 import static jdk.testlibrary.ProcessTools.*;
  37 
  38 import org.testng.annotations.BeforeTest;
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 import static org.testng.Assert.*;
  42 
  43 
  44 @Test
  45 public class AddExportsTest {
  46 
  47     private static final String TEST_SRC = System.getProperty("test.src");
  48 
  49     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  50     private static final Path MODS_DIR = Paths.get("mods");
  51     private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");
  52 
  53     // test module m1 that uses Unsafe
  54     private static final String TEST1_MODULE = "m1";
  55     private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";
  56 
  57     // test module m2 uses java.transaction internals
  58     private static final String TEST2_MODULE = "m2";
  59     private static final String TEST2_MAIN_CLASS = "jdk.test2.Main";
  60 
  61     // test module m3 uses m4 internals
  62     private static final String TEST3_MODULE = "m3";
  63     private static final String TEST3_MAIN_CLASS = "jdk.test3.Main";
  64     private static final String TEST4_MODULE = "m4";
  65 
  66 
  67     @BeforeTest
  68     public void compileTestModules() throws Exception {
  69 
  70         // javac -d mods/m1 src/m1/**
  71         boolean compiled = CompilerUtils.compile(
  72                 SRC_DIR.resolve(TEST1_MODULE),
  73                 MODS_DIR.resolve(TEST1_MODULE),
  74                 "-XaddExports:java.base/jdk.internal.misc=m1");
  75         assertTrue(compiled, "module " + TEST1_MODULE + " did not compile");
  76 
  77         // javac -d upgrademods/java.transaction src/java.transaction/**
  78         compiled = CompilerUtils.compile(
  79                 SRC_DIR.resolve("java.transaction"),
  80                 UPGRADE_MODS_DIRS.resolve("java.transaction"));
  81         assertTrue(compiled, "module java.transaction did not compile");
  82 
  83         // javac -upgrademodulepath upgrademods -d mods/m2 src/m2/**
  84         compiled = CompilerUtils.compile(
  85                 SRC_DIR.resolve(TEST2_MODULE),
  86                 MODS_DIR.resolve(TEST2_MODULE),
  87                 "-upgrademodulepath", UPGRADE_MODS_DIRS.toString(),
  88                 "-XaddExports:java.transaction/javax.transaction.internal=m2");
  89         assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");
  90 
  91         // javac -d mods/m3 src/m3/**
  92         compiled = CompilerUtils.compile(
  93                 SRC_DIR.resolve(TEST3_MODULE),
  94                 MODS_DIR.resolve(TEST3_MODULE));
  95         assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");
  96 
  97         // javac -d mods/m4 src/m4/**
  98         compiled = CompilerUtils.compile(
  99                 SRC_DIR.resolve(TEST4_MODULE),
 100                 MODS_DIR.resolve(TEST4_MODULE));
 101         assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");
 102     }
 103 
 104     /**
 105      * Sanity check with -version
 106      */
 107     public void testSanity() throws Exception {
 108 
 109         int exitValue
 110             =  executeTestJava("-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 111                                "-version")
 112                 .outputTo(System.out)
 113                 .errorTo(System.out)
 114                 .getExitValue();
 115 
 116         assertTrue(exitValue == 0);
 117     }
 118 
 119 
 120     /**
 121      * Run class path application that uses jdk.internal.misc.Unsafe
 122      */
 123     public void testUnnamedModule() throws Exception {
 124 
 125         // java -XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED \
 126         //      -cp mods/$TESTMODULE jdk.test.UsesUnsafe
 127 
 128         String classpath = MODS_DIR.resolve(TEST1_MODULE).toString();
 129         int exitValue
 130             = executeTestJava("-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED",
 131                               "-cp", classpath,
 132                               TEST1_MAIN_CLASS)
 133                 .outputTo(System.out)
 134                 .errorTo(System.out)
 135                 .getExitValue();
 136 
 137         assertTrue(exitValue == 0);
 138     }
 139 
 140 
 141     /**
 142      * Run named module that uses jdk.internal.misc.Unsafe
 143      */
 144     public void testNamedModule() throws Exception {
 145 
 146         //  java -XaddExports:java.base/jdk.internal.misc=test \
 147         //       -mp mods -m $TESTMODULE/$MAIN_CLASS
 148 
 149         String mid = TEST1_MODULE + "/" + TEST1_MAIN_CLASS;
 150         int exitValue =
 151             executeTestJava("-XaddExports:java.base/jdk.internal.misc=" + TEST1_MODULE,
 152                             "-mp", MODS_DIR.toString(),
 153                             "-m", mid)
 154                 .outputTo(System.out)
 155                 .errorTo(System.out)
 156                 .getExitValue();
 157 
 158         assertTrue(exitValue == 0);
 159     }
 160 
 161 
 162     /**
 163      * Test -XaddExports with upgraded module
 164      */
 165     public void testWithUpgradedModule() throws Exception {
 166 
 167         // java -XaddExports:java.transaction/javax.transaction.internal=m2
 168         //      -upgrademodulepath upgrademods -mp mods -m ...
 169         String mid = TEST2_MODULE + "/" + TEST2_MAIN_CLASS;
 170         int exitValue = executeTestJava(
 171                 "-XaddExports:java.transaction/javax.transaction.internal=m2",
 172                 "-upgrademodulepath", UPGRADE_MODS_DIRS.toString(),
 173                 "-mp", MODS_DIR.toString(),
 174                 "-m", mid)
 175                 .outputTo(System.out)
 176                 .errorTo(System.out)
 177                 .getExitValue();
 178 
 179         assertTrue(exitValue == 0);
 180     }
 181 
 182 
 183     /**
 184      * Test -XaddExports with module that is added to the set of root modules
 185      * with -addmods.
 186      */
 187     public void testWithAddMods() throws Exception {
 188 
 189         // java -XaddExports:m4/jdk.test4=m3 -mp mods -m ...
 190         String mid = TEST3_MODULE + "/" + TEST3_MAIN_CLASS;
 191         int exitValue = executeTestJava(
 192                 "-XaddExports:m4/jdk.test4=m3",
 193                 "-mp", MODS_DIR.toString(),
 194                 "-addmods", TEST4_MODULE,
 195                 "-m", mid)
 196                 .outputTo(System.out)
 197                 .errorTo(System.out)
 198                 .getExitValue();
 199 
 200         assertTrue(exitValue == 0);
 201     }
 202 
 203 
 204     /**
 205      * -XaddExports can only be specified once
 206      */
 207     public void testWithDuplicateOption() throws Exception {
 208 
 209         int exitValue
 210             =  executeTestJava("-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 211                                "-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 212                                "-version")
 213                 .outputTo(System.out)
 214                 .errorTo(System.out)
 215                 .shouldContain("specified more than once")
 216                 .getExitValue();
 217 
 218         assertTrue(exitValue != 0);
 219     }
 220 
 221 
 222     /**
 223      * Exercise -XaddExports with bad values
 224      */
 225     @Test(dataProvider = "badvalues")
 226     public void testWithBadValue(String value, String ignore) throws Exception {
 227 
 228         //  -XaddExports:$VALUE -version
 229         int exitValue =
 230             executeTestJava("-XaddExports:" + value,
 231                             "-version")
 232                 .outputTo(System.out)
 233                 .errorTo(System.out)
 234                 .getExitValue();
 235 
 236         assertTrue(exitValue != 0);
 237     }
 238 
 239     @DataProvider(name = "badvalues")
 240     public Object[][] badValues() {
 241         return new Object[][]{
 242 
 243             { "java.base/jdk.internal.misc",            null }, // missing target
 244             { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
 245             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module
 246             { "java.base/sun.monkey=ALL-UNNAMED",       null }, // unknown package
 247             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module/package
 248             { "java.base=ALL-UNNAMED",                  null }, // missing package
 249             { "java.base/=ALL-UNNAMED",                 null }  // missing package
 250 
 251         };
 252     }
 253 }