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 import java.util.Arrays;
  36 
  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 @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                 "-XaddExports: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 -upgrademodulepath upgrademods -d mods/m2 src/m2/**
  85         compiled = CompilerUtils.compile(
  86                 SRC_DIR.resolve(TEST2_MODULE),
  87                 MODS_DIR.resolve(TEST2_MODULE),
  88                 "-upgrademodulepath", UPGRADE_MODS_DIRS.toString(),
  89                 "-XaddExports: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() throws Exception {
 109 
 110         int exitValue
 111             =  executeTestJava("-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 112                                "-version")
 113                 .outputTo(System.out)
 114                 .errorTo(System.out)
 115                 .getExitValue();
 116 
 117         assertTrue(exitValue == 0);
 118     }
 119 
 120 
 121     /**
 122      * Run class path application that uses jdk.internal.misc.Unsafe
 123      */
 124     public void testUnnamedModule() throws Exception {
 125 
 126         // java -XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED \
 127         //      -cp mods/$TESTMODULE jdk.test.UsesUnsafe
 128 
 129         String classpath = MODS_DIR.resolve(TEST1_MODULE).toString();
 130         int exitValue
 131             = executeTestJava("-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED",
 132                               "-cp", classpath,
 133                               TEST1_MAIN_CLASS)
 134                 .outputTo(System.out)
 135                 .errorTo(System.out)
 136                 .getExitValue();
 137 
 138         assertTrue(exitValue == 0);
 139     }
 140 
 141 
 142     /**
 143      * Run named module that uses jdk.internal.misc.Unsafe
 144      */
 145     public void testNamedModule() throws Exception {
 146 
 147         //  java -XaddExports:java.base/jdk.internal.misc=test \
 148         //       -mp mods -m $TESTMODULE/$MAIN_CLASS
 149 
 150         String mid = TEST1_MODULE + "/" + TEST1_MAIN_CLASS;
 151         int exitValue =
 152             executeModularTest(mid, null,
 153                 Arrays.asList("-XaddExports:java.base/jdk.internal.misc=" + TEST1_MODULE),
 154                 null, Arrays.asList(MODS_DIR), null)
 155                     .getExitValue();
 156 
 157         assertTrue(exitValue == 0);
 158     }
 159 
 160 
 161     /**
 162      * Test -XaddExports with upgraded module
 163      */
 164     public void testWithUpgradedModule() throws Exception {
 165 
 166         // java -XaddExports:java.transaction/javax.transaction.internal=m2
 167         //      -upgrademodulepath upgrademods -mp mods -m ...
 168         String mid = TEST2_MODULE + "/" + TEST2_MAIN_CLASS;
 169         int exitValue = executeModularTest(mid, null,
 170             Arrays.asList("-XaddExports:java.transaction/javax.transaction.internal=m2",
 171                           "-upgrademodulepath", UPGRADE_MODS_DIRS.toString()),
 172             null, Arrays.asList(MODS_DIR), null)
 173                 .getExitValue();
 174 
 175         assertTrue(exitValue == 0);
 176     }
 177 
 178 
 179     /**
 180      * Test -XaddExports with module that is added to the set of root modules
 181      * with -addmods.
 182      */
 183     public void testWithAddMods() throws Exception {
 184 
 185         // java -XaddExports:m4/jdk.test4=m3 -mp mods -m ...
 186         String mid = TEST3_MODULE + "/" + TEST3_MAIN_CLASS;
 187         int exitValue = executeModularTest(mid, null,
 188             Arrays.asList("-XaddExports:m4/jdk.test4=m3"),
 189             null, Arrays.asList(MODS_DIR),
 190             Arrays.asList(TEST4_MODULE))
 191                 .getExitValue();
 192 
 193         assertTrue(exitValue == 0);
 194     }
 195 
 196 
 197     /**
 198      * -XaddExports can only be specified once
 199      */
 200     public void testWithDuplicateOption() throws Exception {
 201 
 202         int exitValue
 203             =  executeTestJava("-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 204                                "-XaddExports:java.base/jdk.internal.reflect=ALL-UNNAMED",
 205                                "-version")
 206                 .outputTo(System.out)
 207                 .errorTo(System.out)
 208                 .shouldContain("specified more than once")
 209                 .getExitValue();
 210 
 211         assertTrue(exitValue != 0);
 212     }
 213 
 214 
 215     /**
 216      * Exercise -XaddExports with bad values
 217      */
 218     @Test(dataProvider = "badvalues")
 219     public void testWithBadValue(String value, String ignore) throws Exception {
 220 
 221         //  -XaddExports:$VALUE -version
 222         int exitValue =
 223             executeTestJava("-XaddExports:" + value,
 224                             "-version")
 225                 .outputTo(System.out)
 226                 .errorTo(System.out)
 227                 .getExitValue();
 228 
 229         assertTrue(exitValue != 0);
 230     }
 231 
 232     @DataProvider(name = "badvalues")
 233     public Object[][] badValues() {
 234         return new Object[][]{
 235 
 236             { "java.base/jdk.internal.misc",            null }, // missing target
 237             { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
 238             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module
 239             { "java.base/sun.monkey=ALL-UNNAMED",       null }, // unknown package
 240             { "java.monkey/sun.monkey=ALL-UNNAMED",     null }, // unknown module/package
 241             { "java.base=ALL-UNNAMED",                  null }, // missing package
 242             { "java.base/=ALL-UNNAMED",                 null }  // missing package
 243 
 244         };
 245     }
 246 }