1 /* 2 * Copyright (c) 2003, 2013, 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 * @test 26 * @bug 4530538 27 * @summary Basic unit test of ClassLoadingMXBean.getLoadedClassCount() 28 * ClassLoadingMXBean.getTotalLoadedClassCount() 29 * ClassLoadingMXBean.getUnloadedClassCount() 30 * @author Alexei Guibadoulline 31 * @run main/othervm LoadCounts 32 */ 33 34 import java.lang.management.*; 35 36 public class LoadCounts { 37 private static ClassLoadingMXBean mbean 38 = ManagementFactory.getClassLoadingMXBean(); 39 40 public static void main(String argv[]) throws Exception { 41 // Get current count 42 int classesNowPrev = mbean.getLoadedClassCount(); 43 long classesTotalPrev = mbean.getTotalLoadedClassCount(); 44 45 System.out.println("Loading 4 classes with the system class loader"); 46 47 new SimpleOne(); 48 new SimpleTwo(); 49 new Chain(); 50 51 int classesNow = mbean.getLoadedClassCount(); 52 long classesTotal = mbean.getTotalLoadedClassCount(); 53 54 if (classesNow > classesTotal) 55 throw new RuntimeException("getLoadedClassCount() > " 56 + "getTotalLoadedClassCount()"); 57 58 if (classesNowPrev + 4 > classesNow) 59 throw new RuntimeException("Number of loaded classes is " 60 + "expected to be at least " 61 + (classesNowPrev + 4) + ", but " 62 + "MBean.getLoadedClassCount() returned " 63 + classesNow); 64 if (classesTotalPrev + 4 > classesTotal) 65 throw new RuntimeException("Total number of loaded classes is " 66 + "expected to be at least " 67 + (classesTotalPrev + 4) + ", but " 68 + "MBean.getTotalLoadedClassCount() " 69 + "returned " + classesTotal); 70 71 System.out.println("Creating new class loader instances"); 72 73 LeftHand leftHand = new LeftHand(); 74 RightHand rightHand = new RightHand(); 75 LoaderForTwoInstances ins1 = new LoaderForTwoInstances(); 76 LoaderForTwoInstances ins2 = new LoaderForTwoInstances(); 77 78 // Load different type of classes with different 79 // initiating classloaders but the same defining class loader. 80 System.out.println("Loading 2 class instances; each by " + 81 "2 initiating class loaders."); 82 83 classesNowPrev = mbean.getLoadedClassCount(); 84 classesTotalPrev = mbean.getTotalLoadedClassCount(); 85 try { 86 Class.forName("Body", true, leftHand); 87 Class.forName("Body", true, rightHand); 88 Class.forName("TheSameClass", true, ins1); 89 Class.forName("TheSameClass", true, ins2); 90 } catch (ClassNotFoundException e) { 91 System.out.println("Unexpected excetion " + e); 92 e.printStackTrace(System.out); 93 throw new RuntimeException(); 94 } 95 classesNow = mbean.getLoadedClassCount(); 96 classesTotal = mbean.getTotalLoadedClassCount(); 97 98 // Expected 2 classes got loaded since they are loaded by 99 // same defining class loader 100 if (classesNowPrev + 2 > classesNow) 101 throw new RuntimeException("Number of loaded classes is " 102 + "expected to be at least " 103 + (classesNowPrev + 4) + ", but " 104 + "MBean.getLoadedClassCount() returned " 105 + classesNow); 106 if (classesTotalPrev + 2 > classesTotal) 107 throw new RuntimeException("Total number of loaded classes is " 108 + "expected to be at least " 109 + (classesTotalPrev + 4) + ", but " 110 + "MBean.getTotalLoadedClassCount() " 111 + "returned " + classesTotal); 112 113 System.out.println("Test passed."); 114 } 115 } 116 117 class SimpleOne {} 118 class SimpleTwo {} 119 120 class Chain { 121 Slave slave = new Slave(); 122 } 123 class Slave {} 124 125 class LeftHand extends ClassLoader { 126 public LeftHand() { 127 super(LeftHand.class.getClassLoader()); 128 } 129 } 130 class RightHand extends ClassLoader { 131 public RightHand() { 132 super(RightHand.class.getClassLoader()); 133 } 134 } 135 class Body {} 136 137 class LoaderForTwoInstances extends ClassLoader { 138 public LoaderForTwoInstances() { 139 super(LoaderForTwoInstances.class.getClassLoader()); 140 } 141 } 142 class TheSameClass {}