1 /*
   2  * Copyright (c) 2015, 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  * @summary Ensure that a newly introduced java.base package placed within the --patch-module
  27  *          directory is considered part of the boot loader's visibility boundary
  28  * @requires !(os.family == "windows")
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @run driver PatchModuleVisibility
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 
  39 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 
  43 public class PatchModuleVisibility {
  44 
  45     public static void main(String[] args) throws Throwable {
  46 
  47       String Vis2_B_src =
  48               "package p2;" +
  49               "public class Vis2_B {" +
  50               "    public void m() {" +
  51               "        System.out.println(\"In B's m()\");" +
  52               "    }" +
  53               "}";
  54 
  55       String Vis2_A_src =
  56               "import p2.*;" +
  57               "public class Vis2_A {" +
  58               "    public static void main(String args[]) throws Exception {" +
  59                       // Try loading a class within a newly introduced java.base
  60                       // package.  Make sure the class can be found via --patch-module.
  61               "        try {" +
  62               "            p2.Vis2_B b = new p2.Vis2_B();" +
  63               "            if (b.getClass().getClassLoader() != null) {" +
  64               "                throw new RuntimeException(\"PatchModuleVisibility FAILED - class B " +
  65                                                            "should be loaded by boot class loader\\n\");" +
  66               "            }" +
  67               "            b.m();" +
  68               "        } catch (Throwable e) {" +
  69               "            throw new RuntimeException(\"PatchModuleVisibility FAILED - test " +
  70                                                        "should not throw an error or exception\\n\");" +
  71               "        }" +
  72               "        System.out.println(\"PatchModuleVisibility PASSED\\n\");" +
  73               "    }" +
  74               "}";
  75 
  76       ClassFileInstaller.writeClassToDisk("p2/Vis2_B",
  77           InMemoryJavaCompiler.compile("p2.Vis2_B", Vis2_B_src), System.getProperty("test.classes"));
  78       ClassFileInstaller.writeClassToDisk("p2/Vis2_B", "mods2/java.base");
  79 
  80       ClassFileInstaller.writeClassToDisk("Vis2_A",
  81           InMemoryJavaCompiler.compile("Vis2_A", Vis2_A_src), System.getProperty("test.classes"));
  82 
  83       // Make sure the classes are actually being loaded from mods2
  84       Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
  85                                                            "p2" + File.separator + "Vis2_B.class"));
  86 
  87       new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(
  88               "--patch-module=java.base=mods2/java.base",
  89               "--add-exports=java.base/p2=ALL-UNNAMED",
  90               "Vis2_A")
  91           .start()).shouldHaveExitValue(0);
  92     }
  93 }