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.* jdk.testlibrary.tasks.JavaTask
  29  * @run testng AddExportsTest
  30  * @summary Basic tests for java --add-exports
  31  */
  32 
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 
  36 import static jdk.testlibrary.ProcessTools.*;
  37 import jdk.testlibrary.tasks.JavaTask;
  38 import jdk.testlibrary.tasks.Task;
  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 @Test
  47 public class AddExportsTest {
  48 
  49     private static final String TEST_SRC = System.getProperty("test.src");
  50 
  51     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  52     private static final Path MODS_DIR = Paths.get("mods");
  53     private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");
  54 
  55     // test module m1 that uses Unsafe
  56     private static final String TEST1_MODULE = "m1";
  57     private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";
  58 
  59     // test module m2 uses java.transaction internals
  60     private static final String TEST2_MODULE = "m2";
  61     private static final String TEST2_MAIN_CLASS = "jdk.test2.Main";
  62 
  63     // test module m3 uses m4 internals
  64     private static final String TEST3_MODULE = "m3";
  65     private static final String TEST3_MAIN_CLASS = "jdk.test3.Main";
  66     private static final String TEST4_MODULE = "m4";
  67 
  68 
  69     @BeforeTest
  70     public void compileTestModules() throws Exception {
  71 
  72         // javac -d mods/m1 src/m1/**
  73         boolean compiled = CompilerUtils.compile(
  74                 SRC_DIR.resolve(TEST1_MODULE),
  75                 MODS_DIR.resolve(TEST1_MODULE),
  76                 "--add-exports", "java.base/jdk.internal.misc=m1");
  77         assertTrue(compiled, "module " + TEST1_MODULE + " did not compile");
  78 
  79         // javac -d upgrademods/java.transaction src/java.transaction/**
  80         compiled = CompilerUtils.compile(
  81                 SRC_DIR.resolve("java.transaction"),
  82                 UPGRADE_MODS_DIRS.resolve("java.transaction"));
  83         assertTrue(compiled, "module java.transaction did not compile");
  84 
  85         // javac --upgrade-module-path upgrademods -d mods/m2 src/m2/**
  86         compiled = CompilerUtils.compile(
  87                 SRC_DIR.resolve(TEST2_MODULE),
  88                 MODS_DIR.resolve(TEST2_MODULE),
  89                 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(),
  90                 "--add-exports", "java.transaction/javax.transaction.internal=m2");
  91         assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");
  92 
  93         // javac -d mods/m3 src/m3/**
  94         compiled = CompilerUtils.compile(
  95                 SRC_DIR.resolve(TEST3_MODULE),
  96                 MODS_DIR.resolve(TEST3_MODULE));
  97         assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");
  98 
  99         // javac -d mods/m4 src/m4/**
 100         compiled = CompilerUtils.compile(
 101                 SRC_DIR.resolve(TEST4_MODULE),
 102                 MODS_DIR.resolve(TEST4_MODULE));
 103         assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");
 104     }
 105 
 106     /**
 107      * Sanity check with -version
 108      */
 109     public void testSanity() throws Exception {
 110         new JavaTask()
 111             .addExports("java.base/jdk.internal.reflect=ALL-UNNAMED")
 112             .vmOptions("-version")
 113             .run();





 114     }
 115 
 116 
 117     /**
 118      * Run class path application that uses jdk.internal.misc.Unsafe
 119      */
 120     public void testUnnamedModule() throws Exception {
 121 
 122         // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
 123         //      -cp mods/$TESTMODULE jdk.test.UsesUnsafe
 124 
 125         new JavaTask()
 126             .addExports("java.base/jdk.internal.misc=ALL-UNNAMED")
 127             .vmOptions("-version")
 128             .classpath(MODS_DIR.resolve(TEST1_MODULE).toString())
 129             .className(TEST1_MAIN_CLASS)
 130             .run();




 131     }
 132 
 133 
 134     /**
 135      * Run named module that uses jdk.internal.misc.Unsafe
 136      */
 137     public void testNamedModule() throws Exception {
 138 
 139         //  java --add-exports java.base/jdk.internal.misc=test \
 140         //       --module-path mods -m $TESTMODULE/$MAIN_CLASS
 141 
 142         new JavaTask()
 143             .addExports("java.base/jdk.internal.misc=" + TEST1_MODULE)
 144             .modulepath(MODS_DIR.toString())
 145             .moduleName(TEST1_MODULE).className(TEST1_MAIN_CLASS)
 146             .run();





 147     }
 148 
 149 
 150     /**
 151      * Test --add-exports with upgraded module
 152      */
 153     public void testWithUpgradedModule() throws Exception {
 154 
 155         // java --add-exports java.transaction/javax.transaction.internal=m2
 156         //      --upgrade-module-path upgrademods --module-path mods -m ...
 157         new JavaTask().ignoreStandardModuleOptions()
 158             .addExports("java.transaction/javax.transaction.internal=m2")
 159             .vmOptions("--upgrade-module-path", UPGRADE_MODS_DIRS.toString())
 160             .modulepath(MODS_DIR.toString())
 161             .moduleName(TEST2_MODULE).className(TEST2_MAIN_CLASS)
 162             .run();





 163     }
 164 
 165 
 166     /**
 167      * Test --add-exports with module that is added to the set of root modules
 168      * with --add-modules.
 169      */
 170     public void testWithAddMods() throws Exception {
 171 
 172         // java --add-exports m4/jdk.test4=m3 --module-path mods -m ...
 173         new JavaTask().ignoreStandardModuleOptions()
 174             .addExports("m4/jdk.test4=m3").addModules(TEST4_MODULE)
 175             .modulepath(MODS_DIR.toString())
 176             .moduleName(TEST3_MODULE).className(TEST3_MAIN_CLASS)
 177             .run();






 178     }
 179 
 180 
 181     /**
 182      * --add-exports can only be specified once
 183      */
 184     public void testWithDuplicateOption() throws Exception {
 185         new JavaTask()
 186             .vmOptions(
 187                 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
 188                 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
 189                 "-version")
 190             .run(Task.Expect.FAIL);





 191     }
 192 
 193 
 194     /**
 195      * Exercise --add-exports with bad values
 196      */
 197     @Test(dataProvider = "badvalues")
 198     public void testWithBadValue(String value, String ignore) throws Exception {

 199         //  --add-exports $VALUE -version
 200         new JavaTask()
 201             .addExports(value)
 202             .vmOptions("-version")
 203             .run(Task.Expect.FAIL);




 204     }
 205 
 206     @DataProvider(name = "badvalues")
 207     public Object[][] badValues() {
 208         return new Object[][]{
 209 
 210             { "java.base/jdk.internal.misc",            null }, // missing target
 211             { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
 212             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module
 213             { "java.base/sun.monkey=ALL-UNNAMED",       null }, // unknown package
 214             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module/package
 215             { "java.base=ALL-UNNAMED",                  null }, // missing package
 216             { "java.base/=ALL-UNNAMED",                 null }  // missing package
 217 
 218         };
 219     }
 220 }
--- EOF ---