0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4530538
0N/A * @summary Basic unit test of ClassLoadingMXBean.getLoadedClassCount()
0N/A * ClassLoadingMXBean.getTotalLoadedClassCount()
0N/A * ClassLoadingMXBean.getUnloadedClassCount()
0N/A * @author Alexei Guibadoulline
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class LoadCounts {
0N/A private static ClassLoadingMXBean mbean
0N/A = ManagementFactory.getClassLoadingMXBean();
0N/A
0N/A public static void main(String argv[]) throws Exception {
0N/A // Get current count
0N/A int classesNowPrev = mbean.getLoadedClassCount();
0N/A long classesTotalPrev = mbean.getTotalLoadedClassCount();
0N/A
0N/A System.out.println("Loading 4 classes with the system class loader");
0N/A
0N/A new SimpleOne();
0N/A new SimpleTwo();
0N/A new Chain();
0N/A
0N/A int classesNow = mbean.getLoadedClassCount();
0N/A long classesTotal = mbean.getTotalLoadedClassCount();
0N/A
0N/A if (classesNow > classesTotal)
0N/A throw new RuntimeException("getLoadedClassCount() > "
0N/A + "getTotalLoadedClassCount()");
0N/A
0N/A if (classesNowPrev + 4 != classesNow)
0N/A throw new RuntimeException("Number of loaded classes is "
0N/A + (classesNowPrev + 4) + ", but "
0N/A + "MBean.getLoadedClassCount() returned "
0N/A + classesNow);
0N/A if (classesTotalPrev + 4 != classesTotal)
0N/A throw new RuntimeException("Total number of loaded classes is "
0N/A + (classesTotalPrev + 4) + ", but "
0N/A + "MBean.getTotalLoadedClassCount() "
0N/A + "returned " + classesTotal);
0N/A
0N/A System.out.println("Creating new class loader instances");
0N/A
0N/A LeftHand leftHand = new LeftHand();
0N/A RightHand rightHand = new RightHand();
0N/A LoaderForTwoInstances ins1 = new LoaderForTwoInstances();
0N/A LoaderForTwoInstances ins2 = new LoaderForTwoInstances();
0N/A
0N/A // Load different type of classes with different
0N/A // initiating classloaders but the same defining class loader.
0N/A System.out.println("Loading 2 class instances; each by " +
0N/A "2 initiating class loaders.");
0N/A
0N/A classesNowPrev = mbean.getLoadedClassCount();
0N/A classesTotalPrev = mbean.getTotalLoadedClassCount();
0N/A try {
0N/A Class.forName("Body", true, leftHand);
0N/A Class.forName("Body", true, rightHand);
0N/A Class.forName("TheSameClass", true, ins1);
0N/A Class.forName("TheSameClass", true, ins2);
0N/A } catch (ClassNotFoundException e) {
0N/A System.out.println("Unexpected excetion " + e);
0N/A e.printStackTrace(System.out);
0N/A throw new RuntimeException();
0N/A }
0N/A classesNow = mbean.getLoadedClassCount();
0N/A classesTotal = mbean.getTotalLoadedClassCount();
0N/A
0N/A // Expected 2 classes got loaded since they are loaded by
0N/A // same defining class loader
0N/A if (classesNowPrev + 2 != classesNow)
0N/A throw new RuntimeException("Expected Number of loaded classes is "
0N/A + (classesNowPrev + 4) + ", but "
0N/A + "MBean.getLoadedClassCount() returned "
0N/A + classesNow);
0N/A if (classesTotalPrev + 2 != classesTotal)
0N/A throw new RuntimeException("Total number of loaded classes is "
0N/A + (classesTotalPrev + 4) + ", but "
0N/A + "MBean.getTotalLoadedClassCount() "
0N/A + "returned " + classesTotal);
0N/A
0N/A System.out.println("Test passed.");
0N/A }
0N/A}
0N/A
0N/Aclass SimpleOne {}
0N/Aclass SimpleTwo {}
0N/A
0N/Aclass Chain {
0N/A Slave slave = new Slave();
0N/A}
0N/Aclass Slave {}
0N/A
2495N/Aclass LeftHand extends ClassLoader {
2495N/A public LeftHand() {
2495N/A super(LeftHand.class.getClassLoader());
2495N/A }
2495N/A}
2495N/Aclass RightHand extends ClassLoader {
2495N/A public RightHand() {
2495N/A super(RightHand.class.getClassLoader());
2495N/A }
2495N/A}
0N/Aclass Body {}
0N/A
2495N/Aclass LoaderForTwoInstances extends ClassLoader {
2495N/A public LoaderForTwoInstances() {
2495N/A super(LoaderForTwoInstances.class.getClassLoader());
2495N/A }
2495N/A}
0N/Aclass TheSameClass {}