1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8159262
  29  * @summary Test differing scenarios where a module's readability list and a package's exportability list should be walked
  30  * @modules java.base/jdk.internal.misc
  31  * @library /testlibrary /test/lib
  32  * @compile ../AccessCheck/ModuleLibrary.java
  33  * @compile ModuleSameCLMain.java
  34  * @compile ModuleNonBuiltinCLMain.java
  35  * @compile CustomSystemClassLoader.java
  36  * @build ModuleStress
  37  * @run main/othervm ModuleStress
  38  */
  39 
  40 import jdk.test.lib.*;
  41 import java.io.File;
  42 
  43 public class ModuleStress {
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         // Test #1: java -version
  48         //   All modules' readability lists and packages' exportability
  49         //   lists should contain only modules defined to the 3 builtin
  50         //   loaders (boot, application, platform).  Thus there is
  51         //   not a need to walk those lists at a GC safepoint since
  52         //   those loaders never die.
  53         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  54              "-Xbootclasspath/a:.",
  55              "-Xlog:modules=trace",
  56              "-version");
  57 
  58         OutputAnalyzer oa = new OutputAnalyzer(pb.start());
  59         oa.shouldNotContain("must be walked")
  60           .shouldNotContain("being walked")
  61           .shouldHaveExitValue(0);
  62 
  63         // Next 2 tests involve the use of class p1.c1 and p2.c2
  64         String source1 = "package p1;"   +
  65                          "import p2.c2;" +
  66                          "public class c1 {" +
  67                          "    public c1() {" +
  68                          "       p2.c2 c2_obj = new p2.c2();" +
  69                          "       c2_obj.method2();" +
  70                          "   }" +
  71                          "}";
  72 
  73         String source2 = "package p2;" +
  74                          "public class c2 {" +
  75                          "    public void method2() { }" +
  76                          "}";
  77 
  78         ClassFileInstaller.writeClassToDisk("p2/c2",
  79              InMemoryJavaCompiler.compile("p2.c2", source2),  System.getProperty("test.classes"));
  80 
  81         ClassFileInstaller.writeClassToDisk("p1/c1",
  82              InMemoryJavaCompiler.compile("p1.c1", source1), System.getProperty("test.classes"));
  83 
  84         // Test #2: Load two modules defined to the same customer class loader.
  85         //   m1's module readability list and package p2's exportability should
  86         //   not be walked at a GC safepoint since both modules are defined to
  87         //   the same loader and thus have the exact same life cycle.
  88         pb = ProcessTools.createJavaProcessBuilder(
  89              "-Xbootclasspath/a:.",
  90              "-Xlog:modules=trace",
  91              "ModuleSameCLMain");
  92 
  93         oa = new OutputAnalyzer(pb.start());
  94         oa.shouldNotContain("must be walked")
  95           .shouldNotContain("being walked")
  96           .shouldHaveExitValue(0);
  97 
  98         // Test #3: Load two modules in differing custom class loaders.
  99         //   m1's module readability list and package p2's exportability list must
 100         //   be walked at a GC safepoint since both modules are defined to non-builtin
 101         //   class loaders which could die and thus be unloaded.
 102         pb = ProcessTools.createJavaProcessBuilder(
 103              "-Xbootclasspath/a:.",
 104              "-Xlog:modules=trace",
 105              "ModuleNonBuiltinCLMain");
 106 
 107         oa = new OutputAnalyzer(pb.start());
 108         oa.shouldContain("module m1 reads list must be walked")
 109           .shouldContain("package p2 defined in module m2, exports list must be walked")
 110           .shouldNotContain("module m2 reads list must be walked")
 111           .shouldHaveExitValue(0);
 112 
 113         // Test #4: Load two modules in differing custom class loaders,
 114         //   of which one has been designated as the custom system class loader
 115         //   via -Djava.system.class.loader=CustomSystemClassLoader. Since
 116         //   m3 is defined to the system class loader, m2's module readability
 117         //   list does not have to be walked at a GC safepoint, but package p2's
 118         //   exportability list does.
 119         pb = ProcessTools.createJavaProcessBuilder(
 120              "-Djava.system.class.loader=CustomSystemClassLoader",
 121              "-Xbootclasspath/a:.",
 122              "-Xlog:modules=trace",
 123              "ModuleNonBuiltinCLMain");
 124 
 125         oa = new OutputAnalyzer(pb.start());
 126         oa.shouldContain("package p2 defined in module m2, exports list must be walked")
 127           .shouldNotContain("module m2 reads list must be walked")
 128           .shouldHaveExitValue(0);
 129 
 130     }
 131 }