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