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