1 /*
   2  * Copyright (c) 2016, 2018, 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  * @bug 8159262
  27  * @summary Test differing scenarios where a module's readability list and a package's exportability list should be walked
  28  * @modules java.base/jdk.internal.misc
  29  * @library /test/lib
  30  * @compile ../AccessCheck/ModuleLibrary.java
  31  * @compile ModuleSameCLMain.java
  32  * @compile ModuleNonBuiltinCLMain.java
  33  * @compile CustomSystemClassLoader.java
  34  * @run driver ModuleStress
  35  */
  36 
  37 import jdk.test.lib.process.ProcessTools;
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  40 
  41 public class ModuleStress {
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         // Test #1: java -version
  46         //   All modules' readability lists and packages' exportability
  47         //   lists should contain only modules defined to the 3 builtin
  48         //   loaders (boot, application, platform).  Thus there is
  49         //   not a need to walk those lists at a GC safepoint since
  50         //   those loaders never die.
  51         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  52              "-Xbootclasspath/a:.",
  53              "-Xlog:module=trace",
  54              "-version");
  55 
  56         OutputAnalyzer oa = new OutputAnalyzer(pb.start());
  57         oa.shouldNotContain("must be walked")
  58           .shouldNotContain("being walked")
  59           .shouldHaveExitValue(0);
  60 
  61         // Next 2 tests involve the use of class p1.c1 and p2.c2
  62         String source1 = "package p1;"   +
  63                          "import p2.c2;" +
  64                          "public class c1 {" +
  65                          "    public c1() {" +
  66                          "       p2.c2 c2_obj = new p2.c2();" +
  67                          "       c2_obj.method2();" +
  68                          "   }" +
  69                          "}";
  70 
  71         String source2 = "package p2;" +
  72                          "public class c2 {" +
  73                          "    public void method2() { }" +
  74                          "}";
  75 
  76         ClassFileInstaller.writeClassToDisk("p2/c2",
  77              InMemoryJavaCompiler.compile("p2.c2", source2),  System.getProperty("test.classes"));
  78 
  79         ClassFileInstaller.writeClassToDisk("p1/c1",
  80              InMemoryJavaCompiler.compile("p1.c1", source1), System.getProperty("test.classes"));
  81 
  82         // Test #2: Load two modules defined to the same customer class loader.
  83         //   m1x's module readability list and package p2's exportability should
  84         //   not be walked at a GC safepoint since both modules are defined to
  85         //   the same loader and thus have the exact same life cycle.
  86         pb = ProcessTools.createJavaProcessBuilder(
  87              "-Xbootclasspath/a:.",
  88              "-Xlog:module=trace",
  89              "ModuleSameCLMain");
  90 
  91         oa = new OutputAnalyzer(pb.start());
  92         oa.shouldNotContain("must be walked")
  93           .shouldNotContain("being walked")
  94           .shouldHaveExitValue(0);
  95 
  96         // Test #3: Load two modules in differing custom class loaders.
  97         //   m1x's module readability list and package p2's exportability list must
  98         //   be walked at a GC safepoint since both modules are defined to non-builtin
  99         //   class loaders which could die and thus be unloaded.
 100         pb = ProcessTools.createJavaProcessBuilder(
 101              "-Xbootclasspath/a:.",
 102              "-Xlog:module=trace",
 103              "ModuleNonBuiltinCLMain");
 104 
 105         oa = new OutputAnalyzer(pb.start());
 106         oa.shouldContain("module m1x reads list must be walked")
 107           .shouldContain("package p2 defined in module m2x, exports list must be walked")
 108           .shouldNotContain("module m2x reads list must be walked")
 109           .shouldHaveExitValue(0);
 110 
 111         // Test #4: Load two modules in differing custom class loaders,
 112         //   of which one has been designated as the custom system class loader
 113         //   via -Djava.system.class.loader=CustomSystemClassLoader. Since
 114         //   m3x is defined to the system class loader, m2x's module readability
 115         //   list does not have to be walked at a GC safepoint, but package p2's
 116         //   exportability list does.
 117         pb = ProcessTools.createJavaProcessBuilder(
 118              "-Djava.system.class.loader=CustomSystemClassLoader",
 119              "-Xbootclasspath/a:.",
 120              "-Xlog:module=trace",
 121              "ModuleNonBuiltinCLMain");
 122 
 123         oa = new OutputAnalyzer(pb.start());
 124         oa.shouldContain("package p2 defined in module m2x, exports list must be walked")
 125           .shouldNotContain("module m2x reads list must be walked")
 126           .shouldHaveExitValue(0);
 127 
 128     }
 129 }