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.tasks.*
  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 jdk.testlibrary.tasks.JavaTask;
  37 import jdk.testlibrary.tasks.Task;
  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 @Test
  46 public class AddExportsTest {
  47 
  48     private static final String TEST_SRC = System.getProperty("test.src");
  49 
  50     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  51     private static final Path MODS_DIR = Paths.get("mods");
  52     private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");
  53 
  54     // test module m1 that uses Unsafe
  55     private static final String TEST1_MODULE = "m1";
  56     private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";
  57 
  58     // test module m2 uses java.transaction internals
  59     private static final String TEST2_MODULE = "m2";
  60     private static final String TEST2_MAIN_CLASS = "jdk.test2.Main";
  61 
  62     // test module m3 uses m4 internals
  63     private static final String TEST3_MODULE = "m3";
  64     private static final String TEST3_MAIN_CLASS = "jdk.test3.Main";
  65     private static final String TEST4_MODULE = "m4";
  66 
  67 
  68     @BeforeTest
  69     public void compileTestModules() throws Exception {
  70 
  71         // javac -d mods/m1 src/m1/**
  72         boolean compiled = CompilerUtils.compile(
  73                 SRC_DIR.resolve(TEST1_MODULE),
  74                 MODS_DIR.resolve(TEST1_MODULE),
  75                 "--add-exports", "java.base/jdk.internal.misc=m1");
  76         assertTrue(compiled, "module " + TEST1_MODULE + " did not compile");
  77 
  78         // javac -d upgrademods/java.transaction src/java.transaction/**
  79         compiled = CompilerUtils.compile(
  80                 SRC_DIR.resolve("java.transaction"),
  81                 UPGRADE_MODS_DIRS.resolve("java.transaction"));
  82         assertTrue(compiled, "module java.transaction did not compile");
  83 
  84         // javac --upgrade-module-path upgrademods -d mods/m2 src/m2/**
  85         compiled = CompilerUtils.compile(
  86                 SRC_DIR.resolve(TEST2_MODULE),
  87                 MODS_DIR.resolve(TEST2_MODULE),
  88                 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(),
  89                 "--add-exports", "java.transaction/javax.transaction.internal=m2");
  90         assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");
  91 
  92         // javac -d mods/m3 src/m3/**
  93         compiled = CompilerUtils.compile(
  94                 SRC_DIR.resolve(TEST3_MODULE),
  95                 MODS_DIR.resolve(TEST3_MODULE));
  96         assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");
  97 
  98         // javac -d mods/m4 src/m4/**
  99         compiled = CompilerUtils.compile(
 100                 SRC_DIR.resolve(TEST4_MODULE),
 101                 MODS_DIR.resolve(TEST4_MODULE));
 102         assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");
 103     }
 104 
 105     /**
 106      * Sanity check with -version
 107      */
 108     public void testSanity() {
 109         new JavaTask()
 110             .addExports("java.base", "jdk.internal.reflect")
 111             .vmOptions("-version")
 112             .run();
 113     }
 114 
 115 
 116     /**
 117      * Run class path application that uses jdk.internal.misc.Unsafe
 118      */
 119     public void testUnnamedModule() {
 120 
 121         // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
 122         //      -cp mods/$TESTMODULE jdk.test.UsesUnsafe
 123 
 124         new JavaTask()
 125             .addExports("java.base", "jdk.internal.misc")
 126             .vmOptions("-version")
 127             .classPath(MODS_DIR.resolve(TEST1_MODULE))
 128             .mainClass(TEST1_MAIN_CLASS)
 129             .run();
 130     }
 131 
 132 
 133     /**
 134      * Run named module that uses jdk.internal.misc.Unsafe
 135      */
 136     public void testNamedModule() {
 137 
 138         //  java --add-exports java.base/jdk.internal.misc=test \
 139         //       --module-path mods -m $TESTMODULE/$MAIN_CLASS
 140 
 141         new JavaTask()
 142             .addExports("java.base", "jdk.internal.misc", TEST1_MODULE)
 143             .modulePath(MODS_DIR)
 144             .module(TEST1_MODULE, TEST1_MAIN_CLASS)
 145             .run();
 146     }
 147 
 148 
 149     /**
 150      * Test --add-exports with upgraded module
 151      */
 152     public void testWithUpgradedModule() {
 153 
 154         // java --add-exports java.transaction/javax.transaction.internal=m2
 155         //      --upgrade-module-path upgrademods --module-path mods -m ...
 156         new JavaTask().ignoreStandardModuleOptions()
 157             .addExports("java.transaction", "javax.transaction.internal", TEST2_MODULE)
 158             .upgradeModulePath(UPGRADE_MODS_DIRS)
 159             .modulePath(MODS_DIR.toString())
 160             .module(TEST2_MODULE, TEST2_MAIN_CLASS)
 161             .run();
 162     }
 163 
 164 
 165     /**
 166      * Test --add-exports with module that is added to the set of root modules
 167      * with --add-modules.
 168      */
 169     public void testWithAddMods() {
 170 
 171         // java --add-exports m4/jdk.test4=m3 --module-path mods -m ...
 172         new JavaTask().ignoreStandardModuleOptions()
 173             .addExports(TEST4_MODULE, "jdk.test4", TEST3_MODULE)
 174             .addModules(TEST4_MODULE)
 175             .modulePath(MODS_DIR)
 176             .module(TEST3_MODULE, TEST3_MAIN_CLASS)
 177             .run();
 178     }
 179 
 180 
 181     /**
 182      * --add-exports can only be specified once
 183      */
 184     public void testWithDuplicateOption() {
 185         new JavaTask()
 186             .addExports("java.base", "jdk.internal.reflect")
 187             .addExports("java.base", "jdk.internal.reflect")
 188             .vmOptions("-version")
 189             .run(Task.Expect.FAIL);
 190     }
 191 
 192 
 193     /**
 194      * Exercise --add-exports with bad values
 195      */
 196     @Test(dataProvider = "badvalues")
 197     public void testWithBadValue(String value, String ignore) {
 198         //  --add-exports $VALUE -version
 199         new JavaTask()
 200             .vmOptions(value, "-version")
 201             .run(Task.Expect.FAIL);
 202     }
 203 
 204     @DataProvider(name = "badvalues")
 205     public Object[][] badValues() {
 206         return new Object[][]{
 207 
 208             { "java.base/jdk.internal.misc",            null }, // missing target
 209             { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
 210             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module
 211             { "java.base/sun.monkey=ALL-UNNAMED",       null }, // unknown package
 212             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module/package
 213             { "java.base=ALL-UNNAMED",                  null }, // missing package
 214             { "java.base/=ALL-UNNAMED",                 null }  // missing package
 215 
 216         };
 217     }
 218 }