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