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 MemoryNotificationInfo.from to return a valid
0N/A * MemoryNotificationInfo object. Or throw exception if
0N/A * the input CompositeData is invalid.
0N/A * @author Mandy Chung
0N/A *
1952N/A * @compile OpenTypeConverter.java
0N/A * @build MemoryNotifInfoCompositeData
0N/A * @run main MemoryNotifInfoCompositeData
0N/A */
0N/A
0N/Aimport javax.management.openmbean.*;
0N/Aimport java.lang.management.MemoryNotificationInfo;
0N/Aimport java.lang.management.MemoryUsage;
0N/Aimport sun.management.MemoryUsageCompositeData;
0N/A
0N/Apublic class MemoryNotifInfoCompositeData {
0N/A public static void main(String[] argv) throws Exception {
0N/A createGoodCompositeData();
0N/A badNameCompositeData();
0N/A badTypeCompositeData();
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A private static final int POOL_NAME = 1;
0N/A private static final int COUNT = 2;
0N/A private static final int USAGE = 3;
0N/A private static final String[] validItemNames = {
0N/A "dummy1",
0N/A "poolName",
0N/A "count",
0N/A "usage",
0N/A "dummy2",
0N/A };
0N/A
0N/A // these values are synchronized with the item names
0N/A private static final Object[] values = {
0N/A "Dummy",
0N/A "Foo",
0N/A new Long(100),
0N/A MemoryUsageCompositeData.
0N/A toCompositeData(new MemoryUsage(0, 100, 1000, 5000)),
0N/A "Dummy",
0N/A };
0N/A
0N/A public static void createGoodCompositeData() throws Exception {
0N/A
0N/A // get the CompositeType for MemoryUsage
0N/A validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class);
0N/A CompositeType ct =
0N/A new CompositeType("MyCompositeType",
0N/A "CompositeType for MemoryNotificationInfo",
0N/A validItemNames,
0N/A validItemNames,
0N/A validItemTypes);
0N/A CompositeData cd =
0N/A new CompositeDataSupport(ct,
0N/A validItemNames,
0N/A values);
0N/A
0N/A MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
0N/A if (!info.getPoolName().equals(values[POOL_NAME])) {
0N/A throw new RuntimeException("pool name = " + info.getPoolName() +
0N/A " expected = " + values[POOL_NAME]);
0N/A }
0N/A if (info.getCount() != ((Long) values[COUNT]).longValue()) {
0N/A throw new RuntimeException("count = " + info.getCount() +
0N/A " expected = " + values[COUNT]);
0N/A }
0N/A if (info.getUsage().getInit() != 0) {
0N/A throw new RuntimeException("usage init = " +
0N/A info.getUsage().getInit() +
0N/A " expected = 0");
0N/A }
0N/A if (info.getUsage().getUsed() != 100) {
0N/A throw new RuntimeException("usage used = " +
0N/A info.getUsage().getUsed() +
0N/A " expected = 100");
0N/A }
0N/A if (info.getUsage().getCommitted () != 1000) {
0N/A throw new RuntimeException("usage committed = " +
0N/A info.getUsage().getCommitted() +
0N/A " expected = 1000");
0N/A }
0N/A if (info.getUsage().getMax() != 5000) {
0N/A throw new RuntimeException("usage max = " +
0N/A info.getUsage().getMax() +
0N/A " expected = 5000");
0N/A }
0N/A System.out.print("Pool name = " + info.getPoolName());
0N/A System.out.println(" Count = " + info.getCount());
0N/A System.out.println("Usage = " + info.getUsage());
0N/A }
0N/A
0N/A public static void badNameCompositeData() throws Exception {
0N/A CompositeType ct =
0N/A new CompositeType("MyCompositeType",
0N/A "CompositeType for MemoryNotificationInfo",
0N/A badItemNames,
0N/A badItemNames,
0N/A validItemTypes);
0N/A CompositeData cd =
0N/A new CompositeDataSupport(ct,
0N/A badItemNames,
0N/A values);
0N/A
0N/A try {
0N/A MemoryNotificationInfo info = MemoryNotificationInfo.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 badTypeCompositeData() throws Exception {
0N/A CompositeType ct =
0N/A new CompositeType("MyCompositeType",
0N/A "CompositeType for MemoryNotificationInfo",
0N/A validItemNames,
0N/A validItemNames,
0N/A badItemTypes);
0N/A
0N/A final Object[] values1 = {
0N/A "Dummy",
0N/A "Foo",
0N/A new Long(100),
0N/A "Bad memory usage object",
0N/A "Dummy",
0N/A };
0N/A
0N/A CompositeData cd =
0N/A new CompositeDataSupport(ct,
0N/A validItemNames,
0N/A values1);
0N/A
0N/A try {
0N/A MemoryNotificationInfo info = MemoryNotificationInfo.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 OpenType[] validItemTypes = {
0N/A SimpleType.STRING,
0N/A SimpleType.STRING,
0N/A SimpleType.LONG,
0N/A null, // OpenTypeConverter.toOpenType(MemoryUsage.class)
0N/A SimpleType.STRING,
0N/A };
0N/A private static final String[] badItemNames = {
0N/A "poolName",
0N/A "BadCountName",
0N/A "usage",
0N/A "dummy1",
0N/A "dummy2",
0N/A };
0N/A private static final OpenType[] badItemTypes = {
0N/A SimpleType.STRING,
0N/A SimpleType.STRING,
0N/A SimpleType.LONG,
0N/A SimpleType.STRING, // Bad type
0N/A SimpleType.STRING,
0N/A };
0N/A}