< prev index next >

test/hotspot/jtreg/serviceability/dcmd/vm/ClassLoaderHierarchyTest.java

Print this page
rev 50411 : imported patch 8203682-jcmd-print-classloader-hierarchy
rev 50412 : [mq]: 8203682-jcmd-print-classloader-hierarchy-delta


  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);




  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
  58 //      |
  59 //      +-- "Kevin", ClassLoaderHierarchyTest$TestClassLoader
  60 //      |
  61 //      +-- ClassLoaderHierarchyTest$TestClassLoader
  62 //            |
  63 //            +-- "Bill", 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);


< prev index next >