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 //+-- 'bootstrap'
  52 //      |
  53 //      +-- 'platform', jdk.internal.loader.ClassLoaders$PlatformClassLoader
  54 //      |     |
  55 //      |     +-- 'app', jdk.internal.loader.ClassLoaders$AppClassLoader
  56 //      |
  57 //      +-- jdk.internal.reflect.DelegatingClassLoader @<id>
  58 //      |
  59 //      +-- 'Kevin' @<id>, ClassLoaderHierarchyTest$TestClassLoader
  60 //      |
  61 //      +-- ClassLoaderHierarchyTest$TestClassLoader @<id>
  62 //            |
  63 //            +-- 'Bill' @<id>, ClassLoaderHierarchyTest$TestClassLoader
  64 
  65     public void run(CommandExecutor executor) throws ClassNotFoundException {
  66 
  67         ClassLoader unnamed_cl = new TestClassLoader(null, null);
  68         Class<?> c1 = Class.forName("TestClass2", true, unnamed_cl);
  69         if (c1.getClassLoader() != unnamed_cl) {
  70             Assert.fail("TestClass defined by wrong classloader: " + c1.getClassLoader());
  71         }
  72 
  73         ClassLoader named_cl = new TestClassLoader("Kevin", null);
  74         Class<?> c2 = Class.forName("TestClass2", true, named_cl);
  75         if (c2.getClassLoader() != named_cl) {
  76             Assert.fail("TestClass defined by wrong classloader: " + c2.getClassLoader());
  77         }
  78 
  79         ClassLoader named_child_cl = new TestClassLoader("Bill", unnamed_cl);
  80         Class<?> c3 = Class.forName("TestClass2", true, named_child_cl);
  81         if (c3.getClassLoader() != named_child_cl) {
  82             Assert.fail("TestClass defined by wrong classloader: " + c3.getClassLoader());
  83         }
  84 
  85         // First test: simple output, no classes displayed
  86         OutputAnalyzer output = executor.execute("VM.classloaders");
  87         output.shouldContain("'bootstrap'");
  88         output.shouldMatch(".*TestClassLoader");
  89         output.shouldMatch("'Kevin'.*TestClassLoader");
  90         output.shouldMatch("'Bill'.*TestClassLoader");
  91 
  92         // Second test: print with classes.
  93         output = executor.execute("VM.classloaders show-classes");
  94         output.shouldContain("'bootstrap'");
  95         output.shouldContain("java.lang.Object");
  96         output.shouldMatch(".*TestClassLoader");
  97         output.shouldMatch("'Kevin'.*TestClassLoader");
  98         output.shouldMatch("'Bill'.*TestClassLoader");
  99         output.shouldContain("TestClass2");
 100     }
 101 
 102     static class TestClassLoader extends ClassLoader {
 103 
 104         public TestClassLoader() {
 105             super();
 106         }
 107 
 108         public TestClassLoader(String name, ClassLoader parent) {
 109             super(name, parent);
 110         }
 111 
 112         public static final String CLASS_NAME = "TestClass2";
 113 
 114         static ByteBuffer readClassFile(String name)
 115         {
 116             File f = new File(System.getProperty("test.classes", "."),
 117                               name);
 118             try (FileInputStream fin = new FileInputStream(f);
 119                  FileChannel fc = fin.getChannel())
 120             {
 121                 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
 122             } catch (IOException e) {
 123                 Assert.fail("Can't open file: " + name, e);
 124             }
 125 
 126             /* Will not reach here as Assert.fail() throws exception */
 127             return null;
 128         }
 129 
 130         protected Class<?> loadClass(String name, boolean resolve)
 131             throws ClassNotFoundException
 132         {
 133             Class<?> c;
 134             if (!CLASS_NAME.equals(name)) {
 135                 c = super.loadClass(name, resolve);
 136             } else {
 137                 // should not delegate to the system class loader
 138                 c = findClass(name);
 139                 if (resolve) {
 140                     resolveClass(c);
 141                 }
 142             }
 143             return c;
 144         }
 145 
 146         protected Class<?> findClass(String name)
 147             throws ClassNotFoundException
 148         {
 149             if (!CLASS_NAME.equals(name)) {
 150                 throw new ClassNotFoundException("Unexpected class: " + name);
 151             }
 152             return defineClass(name, readClassFile(name + ".class"), null);
 153         }
 154 
 155     }
 156 
 157     @Test
 158     public void jmx() throws ClassNotFoundException {
 159         run(new JMXExecutor());
 160     }
 161 
 162 }
 163 
 164 class TestClass2 {
 165     static {
 166         Runnable r = () -> System.out.println("Hello");
 167         r.run();
 168     }
 169 }
 170