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.
   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 /*
  26  * @test ClassLoadUnloadTest
  27  * @bug 8142506
  28  * @modules java.base/jdk.internal.misc
  29  * @library /test/lib /runtime/testlibrary
  30  * @library classes
  31  * @build test.Empty
  32  * @run driver ClassLoadUnloadTest
  33  */
  34 
  35 import jdk.test.lib.process.ProcessTools;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import java.lang.ref.WeakReference;
  38 import java.lang.reflect.Method;
  39 import java.util.ArrayList;
  40 import java.util.Collections;
  41 import java.util.List;
  42 
  43 public class ClassLoadUnloadTest {
  44     private static OutputAnalyzer out;
  45     private static ProcessBuilder pb;
  46     private static class ClassUnloadTestMain {
  47         public static void main(String... args) throws Exception {
  48             String className = "test.Empty";
  49             ClassLoader cl = ClassUnloadCommon.newClassLoader();
  50             Class<?> c = cl.loadClass(className);
  51             cl = null; c = null;
  52             ClassUnloadCommon.triggerUnloading();
  53         }
  54     }
  55 
  56     static void checkFor(String... outputStrings) throws Exception {
  57         out = new OutputAnalyzer(pb.start());
  58         for (String s: outputStrings) {
  59             out.shouldContain(s);
  60         }
  61         out.shouldHaveExitValue(0);
  62     }
  63 
  64     static void checkAbsent(String... outputStrings) throws Exception {
  65         out = new OutputAnalyzer(pb.start());
  66         for (String s: outputStrings) {
  67             out.shouldNotContain(s);
  68         }
  69         out.shouldHaveExitValue(0);
  70     }
  71 
  72     // Use the same command-line heap size setting as ../ClassUnload/UnloadTest.java
  73     static ProcessBuilder exec(String... args) throws Exception {
  74         List<String> argsList = new ArrayList<>();
  75         Collections.addAll(argsList, args);
  76         Collections.addAll(argsList, "-Xmn8m");
  77         Collections.addAll(argsList, "-Dtest.class.path=" + System.getProperty("test.class.path", "."));
  78         Collections.addAll(argsList, ClassUnloadTestMain.class.getName());
  79         return ProcessTools.createJavaProcessBuilder(argsList.toArray(new String[argsList.size()]));
  80     }
  81 
  82     public static void main(String... args) throws Exception {
  83 
  84         //  -Xlog:class+unload=info
  85         pb = exec("-Xlog:class+unload=info");
  86         checkFor("[class,unload]", "unloading class");
  87 
  88         //  -Xlog:class+unload=off
  89         pb = exec("-Xlog:class+unload=off");
  90         checkAbsent("[class,unload]");
  91 
  92         //  -XX:+TraceClassUnloading
  93         pb = exec("-XX:+TraceClassUnloading");
  94         checkFor("[class,unload]", "unloading class");
  95 
  96         //  -XX:-TraceClassUnloading
  97         pb = exec("-XX:-TraceClassUnloading");
  98         checkAbsent("[class,unload]");
  99 
 100         //  -Xlog:class+load=info
 101         pb = exec("-Xlog:class+load=info");
 102         checkFor("[class,load]", "java.lang.Object", "source:");
 103 
 104         //  -Xlog:class+load=debug
 105         pb = exec("-Xlog:class+load=debug");
 106         checkFor("[class,load]", "java.lang.Object", "source:", "klass:", "super:", "loader:", "bytes:");
 107 
 108         //  -Xlog:class+load=off
 109         pb = exec("-Xlog:class+load=off");
 110         checkAbsent("[class,load]");
 111 
 112         //  -XX:+TraceClassLoading
 113         pb = exec("-XX:+TraceClassLoading");
 114         checkFor("[class,load]", "java.lang.Object", "source:");
 115 
 116         //  -XX:-TraceClassLoading
 117         pb = exec("-XX:-TraceClassLoading");
 118         checkAbsent("[class,load]");
 119 
 120         //  -verbose:class
 121         pb = exec("-verbose:class");
 122         checkFor("[class,load]", "java.lang.Object", "source:");
 123         checkFor("[class,unload]", "unloading class");
 124 
 125         //  -Xlog:class+loader+data=trace
 126         pb = exec("-Xlog:class+loader+data=trace");
 127         checkFor("[class,loader,data]", "create class loader data");
 128 
 129     }
 130 }