jdk/test/java/lang/management/ClassLoadingMXBean/LoadCounts.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5808 3a1f603c5ca7
child 21423 6f6edad5b031
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of ClassLoadingMXBean.getLoadedClassCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *                             ClassLoadingMXBean.getTotalLoadedClassCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *                             ClassLoadingMXBean.getUnloadedClassCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author  Alexei Guibadoulline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class LoadCounts {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static ClassLoadingMXBean mbean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        = ManagementFactory.getClassLoadingMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String argv[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        // Get current count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        int classesNowPrev = mbean.getLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        long classesTotalPrev = mbean.getTotalLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.out.println("Loading 4 classes with the system class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        new SimpleOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        new SimpleTwo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new Chain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int classesNow = mbean.getLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        long classesTotal = mbean.getTotalLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (classesNow > classesTotal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new RuntimeException("getLoadedClassCount() > "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                     + "getTotalLoadedClassCount()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (classesNowPrev + 4 != classesNow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new RuntimeException("Number of loaded classes is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                     + (classesNowPrev + 4) + ", but "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                     + "MBean.getLoadedClassCount() returned "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                     + classesNow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (classesTotalPrev + 4 != classesTotal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new RuntimeException("Total number of loaded classes is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                     + (classesTotalPrev + 4) + ", but "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                     + "MBean.getTotalLoadedClassCount() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                     + "returned " + classesTotal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println("Creating new class loader instances");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        LeftHand leftHand = new LeftHand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        RightHand rightHand = new RightHand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        LoaderForTwoInstances ins1 = new LoaderForTwoInstances();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        LoaderForTwoInstances ins2 = new LoaderForTwoInstances();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Load different type of classes with different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // initiating classloaders but the same defining class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println("Loading 2 class instances; each by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                           "2 initiating class loaders.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        classesNowPrev = mbean.getLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        classesTotalPrev = mbean.getTotalLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Class.forName("Body", true, leftHand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Class.forName("Body", true, rightHand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Class.forName("TheSameClass", true, ins1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            Class.forName("TheSameClass", true, ins2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.out.println("Unexpected excetion " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        classesNow = mbean.getLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        classesTotal = mbean.getTotalLoadedClassCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Expected 2 classes got loaded since they are loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // same defining class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (classesNowPrev + 2 != classesNow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new RuntimeException("Expected Number of loaded classes is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                     + (classesNowPrev + 4) + ", but "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                     + "MBean.getLoadedClassCount() returned "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                     + classesNow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (classesTotalPrev + 2 != classesTotal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new RuntimeException("Total number of loaded classes is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                     + (classesTotalPrev + 4) + ", but "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                     + "MBean.getTotalLoadedClassCount() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                     + "returned " + classesTotal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
class SimpleOne {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
class SimpleTwo {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
class Chain {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    Slave slave = new Slave();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
class Slave {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
class Body {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
class TheSameClass {}