blob: 378caec5e97a0e535bf7d054e1844ce0342a8560 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 */
32
33import java.lang.management.*;
34import java.util.*;
35
36public 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 + (classesNowPrev + 4) + ", but "
61 + "MBean.getLoadedClassCount() returned "
62 + classesNow);
63 if (classesTotalPrev + 4 != classesTotal)
64 throw new RuntimeException("Total number of loaded classes is "
65 + (classesTotalPrev + 4) + ", but "
66 + "MBean.getTotalLoadedClassCount() "
67 + "returned " + classesTotal);
68
69 System.out.println("Creating new class loader instances");
70
71 LeftHand leftHand = new LeftHand();
72 RightHand rightHand = new RightHand();
73 LoaderForTwoInstances ins1 = new LoaderForTwoInstances();
74 LoaderForTwoInstances ins2 = new LoaderForTwoInstances();
75
76 // Load different type of classes with different
77 // initiating classloaders but the same defining class loader.
78 System.out.println("Loading 2 class instances; each by " +
79 "2 initiating class loaders.");
80
81 classesNowPrev = mbean.getLoadedClassCount();
82 classesTotalPrev = mbean.getTotalLoadedClassCount();
83 try {
84 Class.forName("Body", true, leftHand);
85 Class.forName("Body", true, rightHand);
86 Class.forName("TheSameClass", true, ins1);
87 Class.forName("TheSameClass", true, ins2);
88 } catch (ClassNotFoundException e) {
89 System.out.println("Unexpected excetion " + e);
90 e.printStackTrace(System.out);
91 throw new RuntimeException();
92 }
93 classesNow = mbean.getLoadedClassCount();
94 classesTotal = mbean.getTotalLoadedClassCount();
95
96 // Expected 2 classes got loaded since they are loaded by
97 // same defining class loader
98 if (classesNowPrev + 2 != classesNow)
99 throw new RuntimeException("Expected Number of loaded classes is "
100 + (classesNowPrev + 4) + ", but "
101 + "MBean.getLoadedClassCount() returned "
102 + classesNow);
103 if (classesTotalPrev + 2 != classesTotal)
104 throw new RuntimeException("Total number of loaded classes is "
105 + (classesTotalPrev + 4) + ", but "
106 + "MBean.getTotalLoadedClassCount() "
107 + "returned " + classesTotal);
108
109 System.out.println("Test passed.");
110 }
111}
112
113class SimpleOne {}
114class SimpleTwo {}
115
116class Chain {
117 Slave slave = new Slave();
118}
119class Slave {}
120
121class LeftHand extends ClassLoader {}
122class RightHand extends ClassLoader {}
123class Body {}
124
125class LoaderForTwoInstances extends ClassLoader {}
126class TheSameClass {}