1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @summary Test of diagnostic command VM.classloaders
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.compiler
  31  *          java.management
  32  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  33  * @run testng ClassLoaderHierarchyTest
  34  */
  35 
  36 import org.testng.Assert;
  37 import org.testng.annotations.Test;
  38 
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import jdk.test.lib.dcmd.CommandExecutor;
  41 import jdk.test.lib.dcmd.JMXExecutor;
  42 
  43 import java.io.File;
  44 import java.io.FileInputStream;
  45 import java.io.IOException;
  46 import java.nio.ByteBuffer;
  47 import java.nio.channels.FileChannel;
  48 
  49 public class ClassLoaderHierarchyTest {
  50 
  51 //    jcmd  my-vm VM.classloaders
  52 //    +-- <bootstrap>
  53 //    |
  54 //    +-- platform (instance of jdk.internal.loader.ClassLoaders$PlatformClassLoader)
  55 //    |     |
  56 //    |     +-- app (instance of jdk.internal.loader.ClassLoaders$AppClassLoader)
  57 //    |
  58 //    +-- <unnamed> (instance of jdk.internal.reflect.DelegatingClassLoader)
  59 //    |
  60 //    +-- Kevin (instance of ClassLoaderHierarchyTest$TestClassLoader)
  61 //    |
  62 //    +-- <unnamed> (instance of ClassLoaderHierarchyTest$TestClassLoader)
  63 //          |
  64 //          +-- Bill (instance of ClassLoaderHierarchyTest$TestClassLoader)
  65 //
  66 
  67     public void run(CommandExecutor executor) throws ClassNotFoundException {
  68 
  69         ClassLoader unnamed_cl = new TestClassLoader(null, null);
  70         Class<?> c1 = Class.forName("TestClass2", true, unnamed_cl);
  71         if (c1.getClassLoader() != unnamed_cl) {
  72             Assert.fail("TestClass defined by wrong classloader: " + c1.getClassLoader());
  73         }
  74 
  75         ClassLoader named_cl = new TestClassLoader("Kevin", null);
  76         Class<?> c2 = Class.forName("TestClass2", true, named_cl);
  77         if (c2.getClassLoader() != named_cl) {
  78             Assert.fail("TestClass defined by wrong classloader: " + c2.getClassLoader());
  79         }
  80 
  81         ClassLoader named_child_cl = new TestClassLoader("Bill", unnamed_cl);
  82         Class<?> c3 = Class.forName("TestClass2", true, named_child_cl);
  83         if (c3.getClassLoader() != named_child_cl) {
  84             Assert.fail("TestClass defined by wrong classloader: " + c3.getClassLoader());
  85         }
  86 
  87         // First test: simple output, no classes displayed
  88         OutputAnalyzer output = executor.execute("VM.classloaders");
  89         output.shouldContain("<bootstrap>");
  90         output.shouldMatch("<unnamed>.*instance of.*TestClassLoader");
  91         output.shouldMatch("Kevin.*instance of.*TestClassLoader");
  92         output.shouldMatch("Bill.*instance of.*TestClassLoader");
  93 
  94         // Second test: print with classes.
  95         output = executor.execute("VM.classloaders show-classes");
  96         output.shouldContain("<bootstrap>");
  97         output.shouldContain("java.lang.Object");
  98         output.shouldMatch("<unnamed>.*instance of.*TestClassLoader");
  99         output.shouldMatch("Kevin.*instance of.*TestClassLoader");
 100         output.shouldMatch("Bill.*instance of.*TestClassLoader");
 101         output.shouldContain("TestClass2");
 102     }
 103 
 104     static class TestClassLoader extends ClassLoader {
 105 
 106         public TestClassLoader() {
 107             super();
 108         }
 109 
 110         public TestClassLoader(String name, ClassLoader parent) {
 111             super(name, parent);
 112         }
 113 
 114         public static final String CLASS_NAME = "TestClass2";
 115 
 116         static ByteBuffer readClassFile(String name)
 117         {
 118             File f = new File(System.getProperty("test.classes", "."),
 119                               name);
 120             try (FileInputStream fin = new FileInputStream(f);
 121                  FileChannel fc = fin.getChannel())
 122             {
 123                 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
 124             } catch (IOException e) {
 125                 Assert.fail("Can't open file: " + name, e);
 126             }
 127 
 128             /* Will not reach here as Assert.fail() throws exception */
 129             return null;
 130         }
 131 
 132         protected Class<?> loadClass(String name, boolean resolve)
 133             throws ClassNotFoundException
 134         {
 135             Class<?> c;
 136             if (!CLASS_NAME.equals(name)) {
 137                 c = super.loadClass(name, resolve);
 138             } else {
 139                 // should not delegate to the system class loader
 140                 c = findClass(name);
 141                 if (resolve) {
 142                     resolveClass(c);
 143                 }
 144             }
 145             return c;
 146         }
 147 
 148         protected Class<?> findClass(String name)
 149             throws ClassNotFoundException
 150         {
 151             if (!CLASS_NAME.equals(name)) {
 152                 throw new ClassNotFoundException("Unexpected class: " + name);
 153             }
 154             return defineClass(name, readClassFile(name + ".class"), null);
 155         }
 156 
 157     }
 158 
 159     @Test
 160     public void jmx() throws ClassNotFoundException {
 161         run(new JMXExecutor());
 162     }
 163 
 164 }
 165 
 166 class TestClass2 {
 167     static {
 168         Runnable r = () -> System.out.println("Hello");
 169         r.run();
 170     }
 171 }
 172