jdk/test/java/lang/management/CompositeData/MemoryUsageCompositeData.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 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     4982289
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test MemoryUsage.from() method to return a valid MemoryUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          or throw exception if the input CompositeData is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @build MemoryUsageCompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main MemoryUsageCompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.openmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class MemoryUsageCompositeData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        createGoodCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        badTypeCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        badNameCompositeData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void createGoodCompositeData() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        final int K = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // these values are synchronized with the item names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final Object[] values = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new Long(5 * K),  // committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            new Long(1 * K),  // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            new Long(10 * K), // max
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            new Long(2 * K),  // used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        CompositeType muct =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            new CompositeType("MyMemoryUsageCompositeType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                              "CompositeType for MemoryUsage",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                              memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                              memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                              memoryUsageItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            new CompositeDataSupport(muct,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                     memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                     values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        MemoryUsage u = MemoryUsage.from(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (u.getInit() != ((Long) values[INIT]).longValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new RuntimeException("init = " + u.getInit() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               " expected = " + values[INIT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (u.getUsed() != ((Long) values[USED]).longValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new RuntimeException("used = " + u.getUsed() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
               " expected = " + values[USED]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new RuntimeException("committed = " + u.getCommitted() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
               " expected = " + values[COMMITTED]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (u.getMax() != ((Long) values[MAX]).longValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new RuntimeException("max = " + u.getMax() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
               " expected = " + values[MAX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static void badTypeCompositeData() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        final int K = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        final Object[] values = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            new Integer(5 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            new Long(1 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            new Long(10 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            new Long(2 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        CompositeType muct =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new CompositeType("MyMemoryUsageCompositeType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                              "CompositeType for MemoryUsage",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                              memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                              memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                              badMUItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
           new CompositeDataSupport(muct,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                    memoryUsageItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                    values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            MemoryUsage u = MemoryUsage.from(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Expected exception: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            "IllegalArgumentException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static void badNameCompositeData() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        final int K = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        final Object[] values = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            new Long(5 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            new Long(1 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new Long(10 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            new Long(2 * K),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            "Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        CompositeType muct =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            new CompositeType("MyMemoryUsageCompositeType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                              "CompositeType for MemoryUsage",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                               badMUItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                               badMUItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               memoryUsageItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            new CompositeDataSupport(muct,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                     badMUItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                     values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            MemoryUsage u = MemoryUsage.from(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("Expected exception: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            "IllegalArgumentException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int COMMITTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int INIT      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final int MAX       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final int USED      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final String[] memoryUsageItemNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        "committed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        "init",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        "max",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        "used",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        "dummy1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        "dummy2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final OpenType[] memoryUsageItemTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        SimpleType.STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        SimpleType.STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final String[] badMUItemNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        "Committed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        "Init",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        "max",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        "used",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        "dummy1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        "dummy2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final OpenType[] badMUItemTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        SimpleType.INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        SimpleType.LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        SimpleType.STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        SimpleType.STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}