Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/CompositeData/MemoryNotifInfoCompositeData.java
38821 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 498228926* @summary Test MemoryNotificationInfo.from to return a valid27* MemoryNotificationInfo object. Or throw exception if28* the input CompositeData is invalid.29* @author Mandy Chung30*31* @compile OpenTypeConverter.java32* @build MemoryNotifInfoCompositeData33* @run main MemoryNotifInfoCompositeData34*/3536import javax.management.openmbean.*;37import java.lang.management.MemoryNotificationInfo;38import java.lang.management.MemoryUsage;39import sun.management.MemoryUsageCompositeData;4041public class MemoryNotifInfoCompositeData {42public static void main(String[] argv) throws Exception {43createGoodCompositeData();44badNameCompositeData();45badTypeCompositeData();46System.out.println("Test passed");47}4849private static final int POOL_NAME = 1;50private static final int COUNT = 2;51private static final int USAGE = 3;52private static final String[] validItemNames = {53"dummy1",54"poolName",55"count",56"usage",57"dummy2",58};5960// these values are synchronized with the item names61private static final Object[] values = {62"Dummy",63"Foo",64new Long(100),65MemoryUsageCompositeData.66toCompositeData(new MemoryUsage(0, 100, 1000, 5000)),67"Dummy",68};6970public static void createGoodCompositeData() throws Exception {7172// get the CompositeType for MemoryUsage73validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class);74CompositeType ct =75new CompositeType("MyCompositeType",76"CompositeType for MemoryNotificationInfo",77validItemNames,78validItemNames,79validItemTypes);80CompositeData cd =81new CompositeDataSupport(ct,82validItemNames,83values);8485MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);86if (!info.getPoolName().equals(values[POOL_NAME])) {87throw new RuntimeException("pool name = " + info.getPoolName() +88" expected = " + values[POOL_NAME]);89}90if (info.getCount() != ((Long) values[COUNT]).longValue()) {91throw new RuntimeException("count = " + info.getCount() +92" expected = " + values[COUNT]);93}94if (info.getUsage().getInit() != 0) {95throw new RuntimeException("usage init = " +96info.getUsage().getInit() +97" expected = 0");98}99if (info.getUsage().getUsed() != 100) {100throw new RuntimeException("usage used = " +101info.getUsage().getUsed() +102" expected = 100");103}104if (info.getUsage().getCommitted () != 1000) {105throw new RuntimeException("usage committed = " +106info.getUsage().getCommitted() +107" expected = 1000");108}109if (info.getUsage().getMax() != 5000) {110throw new RuntimeException("usage max = " +111info.getUsage().getMax() +112" expected = 5000");113}114System.out.print("Pool name = " + info.getPoolName());115System.out.println(" Count = " + info.getCount());116System.out.println("Usage = " + info.getUsage());117}118119public static void badNameCompositeData() throws Exception {120CompositeType ct =121new CompositeType("MyCompositeType",122"CompositeType for MemoryNotificationInfo",123badItemNames,124badItemNames,125validItemTypes);126CompositeData cd =127new CompositeDataSupport(ct,128badItemNames,129values);130131try {132MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);133} catch (IllegalArgumentException e) {134System.out.println("Expected exception: " +135e.getMessage());136return;137}138throw new RuntimeException(139"IllegalArgumentException not thrown");140}141142public static void badTypeCompositeData() throws Exception {143CompositeType ct =144new CompositeType("MyCompositeType",145"CompositeType for MemoryNotificationInfo",146validItemNames,147validItemNames,148badItemTypes);149150final Object[] values1 = {151"Dummy",152"Foo",153new Long(100),154"Bad memory usage object",155"Dummy",156};157158CompositeData cd =159new CompositeDataSupport(ct,160validItemNames,161values1);162163try {164MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);165} catch (IllegalArgumentException e) {166System.out.println("Expected exception: " +167e.getMessage());168return;169}170throw new RuntimeException(171"IllegalArgumentException not thrown");172}173174private static OpenType[] validItemTypes = {175SimpleType.STRING,176SimpleType.STRING,177SimpleType.LONG,178null, // OpenTypeConverter.toOpenType(MemoryUsage.class)179SimpleType.STRING,180};181private static final String[] badItemNames = {182"poolName",183"BadCountName",184"usage",185"dummy1",186"dummy2",187};188private static final OpenType[] badItemTypes = {189SimpleType.STRING,190SimpleType.STRING,191SimpleType.LONG,192SimpleType.STRING, // Bad type193SimpleType.STRING,194};195}196197198