0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4982289
0N/A * @summary Test MemoryUsage.from() method to return a valid MemoryUsage
0N/A * or throw exception if the input CompositeData is invalid.
0N/A * @author Mandy Chung
0N/A *
0N/A * @build MemoryUsageCompositeData
0N/A * @run main MemoryUsageCompositeData
0N/A */
0N/A
0N/Aimport javax.management.openmbean.*;
0N/Aimport java.lang.management.MemoryUsage;
0N/A
0N/Apublic class MemoryUsageCompositeData {
0N/A public static void main(String[] argv) throws Exception {
0N/A createGoodCompositeData();
0N/A badTypeCompositeData();
0N/A badNameCompositeData();
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A public static void createGoodCompositeData() throws Exception {
0N/A final int K = 1024;
0N/A // these values are synchronized with the item names
0N/A final Object[] values = {
0N/A new Long(5 * K), // committed
0N/A new Long(1 * K), // init
0N/A new Long(10 * K), // max
0N/A new Long(2 * K), // used
0N/A "Dummy",
0N/A "Dummy",
0N/A };
0N/A
0N/A CompositeType muct =
0N/A new CompositeType("MyMemoryUsageCompositeType",
0N/A "CompositeType for MemoryUsage",
0N/A memoryUsageItemNames,
0N/A memoryUsageItemNames,
0N/A memoryUsageItemTypes);
0N/A CompositeData cd =
0N/A new CompositeDataSupport(muct,
0N/A memoryUsageItemNames,
0N/A values);
0N/A MemoryUsage u = MemoryUsage.from(cd);
0N/A if (u.getInit() != ((Long) values[INIT]).longValue()) {
0N/A throw new RuntimeException("init = " + u.getInit() +
0N/A " expected = " + values[INIT]);
0N/A }
0N/A if (u.getUsed() != ((Long) values[USED]).longValue()) {
0N/A throw new RuntimeException("used = " + u.getUsed() +
0N/A " expected = " + values[USED]);
0N/A }
0N/A if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {
0N/A throw new RuntimeException("committed = " + u.getCommitted() +
0N/A " expected = " + values[COMMITTED]);
0N/A }
0N/A if (u.getMax() != ((Long) values[MAX]).longValue()) {
0N/A throw new RuntimeException("max = " + u.getMax() +
0N/A " expected = " + values[MAX]);
0N/A }
0N/A System.out.println(u);
0N/A }
0N/A
0N/A public static void badTypeCompositeData() throws Exception {
0N/A final int K = 1024;
0N/A final Object[] values = {
0N/A new Integer(5 * K),
0N/A new Long(1 * K),
0N/A new Long(10 * K),
0N/A new Long(2 * K),
0N/A "Dummy",
0N/A "Dummy",
0N/A };
0N/A
0N/A CompositeType muct =
0N/A new CompositeType("MyMemoryUsageCompositeType",
0N/A "CompositeType for MemoryUsage",
0N/A memoryUsageItemNames,
0N/A memoryUsageItemNames,
0N/A badMUItemTypes);
0N/A CompositeData cd =
0N/A new CompositeDataSupport(muct,
0N/A memoryUsageItemNames,
0N/A values);
0N/A try {
0N/A MemoryUsage u = MemoryUsage.from(cd);
0N/A } catch (IllegalArgumentException e) {
0N/A System.out.println("Expected exception: " +
0N/A e.getMessage());
0N/A return;
0N/A }
0N/A throw new RuntimeException(
0N/A "IllegalArgumentException not thrown");
0N/A }
0N/A
0N/A public static void badNameCompositeData() throws Exception {
0N/A final int K = 1024;
0N/A final Object[] values = {
0N/A new Long(5 * K),
0N/A new Long(1 * K),
0N/A new Long(10 * K),
0N/A new Long(2 * K),
0N/A "Dummy",
0N/A "Dummy",
0N/A };
0N/A
0N/A CompositeType muct =
0N/A new CompositeType("MyMemoryUsageCompositeType",
0N/A "CompositeType for MemoryUsage",
0N/A badMUItemNames,
0N/A badMUItemNames,
0N/A memoryUsageItemTypes);
0N/A CompositeData cd =
0N/A new CompositeDataSupport(muct,
0N/A badMUItemNames,
0N/A values);
0N/A try {
0N/A MemoryUsage u = MemoryUsage.from(cd);
0N/A } catch (IllegalArgumentException e) {
0N/A System.out.println("Expected exception: " +
0N/A e.getMessage());
0N/A return;
0N/A }
0N/A throw new RuntimeException(
0N/A "IllegalArgumentException not thrown");
0N/A }
0N/A
0N/A private static final int COMMITTED = 0;
0N/A private static final int INIT = 1;
0N/A private static final int MAX = 2;
0N/A private static final int USED = 3;
0N/A private static final String[] memoryUsageItemNames = {
0N/A "committed",
0N/A "init",
0N/A "max",
0N/A "used",
0N/A "dummy1",
0N/A "dummy2",
0N/A };
0N/A private static final OpenType[] memoryUsageItemTypes = {
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.STRING,
0N/A SimpleType.STRING,
0N/A };
0N/A private static final String[] badMUItemNames = {
0N/A "Committed",
0N/A "Init",
0N/A "max",
0N/A "used",
0N/A "dummy1",
0N/A "dummy2",
0N/A };
0N/A private static final OpenType[] badMUItemTypes = {
0N/A SimpleType.INTEGER,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.STRING,
0N/A SimpleType.STRING,
0N/A };
0N/A}