1 /*
   2  * Copyright (c) 2014, 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  * @build AddExportsTest CompilerUtils jdk.testlibrary.*
  28  * @run testng AddExportsTest
  29  * @summary Basic tests for java -XaddExports
  30  */
  31 
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 
  35 import static jdk.testlibrary.ProcessTools.*;
  36 
  37 import org.testng.annotations.BeforeTest;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 import static org.testng.Assert.*;
  41 
  42 
  43 @Test
  44 public class AddExportsTest {
  45 
  46     private static final String TEST_SRC = System.getProperty("test.src");
  47 
  48     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  49     private static final Path MODS_DIR = Paths.get("mods");
  50 
  51     // the module name of the test module
  52     private static final String TEST_MODULE = "test";
  53 
  54     // the module main class
  55     private static final String MAIN_CLASS = "jdk.test.UsesUnsafe";
  56 
  57 
  58     @BeforeTest
  59     public void compileTestModule() throws Exception {
  60 
  61         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  62         boolean compiled
  63             = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  64                                     MODS_DIR.resolve(TEST_MODULE),
  65                                     "-XaddExports:java.base/sun.misc=test");
  66 
  67         assertTrue(compiled, "test module did not compile");
  68     }
  69 
  70     /**
  71      * Sanity check with -version
  72      */
  73     public void testSanity() throws Exception {
  74 
  75         int exitValue
  76             =  executeTestJava("-XaddExports:java.base/sun.reflect=ALL-UNNAMED",
  77                                "-version")
  78                 .outputTo(System.out)
  79                 .errorTo(System.out)
  80                 .getExitValue();
  81 
  82         assertTrue(exitValue == 0);
  83     }
  84 
  85 
  86     /**
  87      * Run class path application that uses sun.misc.Unsafe
  88      */
  89     public void testUnnamedModule() throws Exception {
  90 
  91         // java -XaddExports:java.base/sun.misc=ALL-UNNAMED \
  92         //      -cp mods/$TESTMODULE jdk.test.UsesUnsafe
  93 
  94         String classpath = MODS_DIR.resolve(TEST_MODULE).toString();
  95         int exitValue
  96             = executeTestJava("-XaddExports:java.base/sun.misc=ALL-UNNAMED",
  97                               "-cp", classpath,
  98                               MAIN_CLASS)
  99                 .outputTo(System.out)
 100                 .errorTo(System.out)
 101                 .getExitValue();
 102 
 103         assertTrue(exitValue == 0);
 104     }
 105 
 106 
 107     /**
 108      * Run named module that uses sun.misc.Unsafe
 109      */
 110     public void testNamedModule() throws Exception {
 111 
 112         //  java -XaddExports:java.base/sun.misc=test \
 113         //       -mp mods -m $TESTMODULE/$MAIN_CLASS
 114 
 115         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 116         int exitValue =
 117             executeTestJava("-XaddExports:java.base/sun.misc=" + TEST_MODULE,
 118                             "-mp", MODS_DIR.toString(),
 119                             "-m", mid)
 120                 .outputTo(System.out)
 121                 .errorTo(System.out)
 122                 .getExitValue();
 123 
 124         assertTrue(exitValue == 0);
 125     }
 126 
 127     /**
 128      * -XaddExports can only be specified once
 129      */
 130     public void testWithDuplicateOption() throws Exception {
 131 
 132         int exitValue
 133             =  executeTestJava("-XaddExports:java.base/sun.reflect=ALL-UNNAMED",
 134                                "-XaddExports:java.base/sun.reflect=ALL-UNNAMED",
 135                                "-version")
 136                 .outputTo(System.out)
 137                 .errorTo(System.out)
 138                 .shouldContain("can only be specified once")
 139                 .getExitValue();
 140 
 141         assertTrue(exitValue != 0);
 142     }
 143 
 144     /**
 145      * Exercise -XaddExports with bad values
 146      */
 147     @Test(dataProvider = "badvalues")
 148     public void testWithBadValue(String value, String ignore) throws Exception {
 149 
 150         //  -XaddExports:$VALUE -version
 151         int exitValue =
 152             executeTestJava("-XaddExports:" + value,
 153                             "-version")
 154                 .outputTo(System.out)
 155                 .errorTo(System.out)
 156                 .getExitValue();
 157 
 158         assertTrue(exitValue != 0);
 159     }
 160 
 161     @DataProvider(name = "badvalues")
 162     public Object[][] badValues() {
 163         return new Object[][]{
 164 
 165             { "java.base/sun.misc",                  null }, // missing target
 166             { "java.base/sun.misc=sun.monkey",       null }, // unknown target
 167             { "java.monkey/sun.monkey=ALL-UNNAMED",  null }, // unknown module
 168             { "java.base/sun.monkey=ALL-UNNAMED",    null }, // unknown package
 169             { "java.monkey/sun.monkey=ALL-UNNAMED",  null }, // unknown module/package
 170             { "java.base=ALL-UNNAMED",               null }, // missing package
 171             { "java.base/=ALL-UNNAMED",              null }  // missing package
 172 
 173         };
 174     }
 175 }