author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5808 | 3a1f603c5ca7 |
child 21423 | 6f6edad5b031 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
33 |
import java.lang.management.*; |
|
34 |
import java.util.*; |
|
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 |
+ (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 |
||
113 |
class SimpleOne {} |
|
114 |
class SimpleTwo {} |
|
115 |
||
116 |
class Chain { |
|
117 |
Slave slave = new Slave(); |
|
118 |
} |
|
119 |
class Slave {} |
|
120 |
||
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
121 |
class LeftHand extends ClassLoader { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
122 |
public LeftHand() { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
123 |
super(LeftHand.class.getClassLoader()); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
124 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
125 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
126 |
class RightHand extends ClassLoader { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
127 |
public RightHand() { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
128 |
super(RightHand.class.getClassLoader()); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
129 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
130 |
} |
2 | 131 |
class Body {} |
132 |
||
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
133 |
class LoaderForTwoInstances extends ClassLoader { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
134 |
public LoaderForTwoInstances() { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
135 |
super(LoaderForTwoInstances.class.getClassLoader()); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
136 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
137 |
} |
2 | 138 |
class TheSameClass {} |