Path: blob/master/test/jdk/java/lang/management/CompositeData/MemoryNotifInfoCompositeData.java
41152 views
/*1* Copyright (c) 2004, 2015, 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* @modules java.management/sun.management32* @compile OpenTypeConverter.java33* @build MemoryNotifInfoCompositeData34* @run main MemoryNotifInfoCompositeData35*/3637import javax.management.openmbean.*;38import java.lang.management.MemoryNotificationInfo;39import java.lang.management.MemoryUsage;40import sun.management.MemoryUsageCompositeData;4142public class MemoryNotifInfoCompositeData {43public static void main(String[] argv) throws Exception {44createGoodCompositeData();45badNameCompositeData();46badTypeCompositeData();47System.out.println("Test passed");48}4950private static final int POOL_NAME = 1;51private static final int COUNT = 2;52private static final int USAGE = 3;53private static final String[] validItemNames = {54"dummy1",55"poolName",56"count",57"usage",58"dummy2",59};6061// these values are synchronized with the item names62private static final Object[] values = {63"Dummy",64"Foo",65new Long(100),66MemoryUsageCompositeData.67toCompositeData(new MemoryUsage(0, 100, 1000, 5000)),68"Dummy",69};7071public static void createGoodCompositeData() throws Exception {7273// get the CompositeType for MemoryUsage74validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class);75CompositeType ct =76new CompositeType("MyCompositeType",77"CompositeType for MemoryNotificationInfo",78validItemNames,79validItemNames,80validItemTypes);81CompositeData cd =82new CompositeDataSupport(ct,83validItemNames,84values);8586MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);87if (!info.getPoolName().equals(values[POOL_NAME])) {88throw new RuntimeException("pool name = " + info.getPoolName() +89" expected = " + values[POOL_NAME]);90}91if (info.getCount() != ((Long) values[COUNT]).longValue()) {92throw new RuntimeException("count = " + info.getCount() +93" expected = " + values[COUNT]);94}95if (info.getUsage().getInit() != 0) {96throw new RuntimeException("usage init = " +97info.getUsage().getInit() +98" expected = 0");99}100if (info.getUsage().getUsed() != 100) {101throw new RuntimeException("usage used = " +102info.getUsage().getUsed() +103" expected = 100");104}105if (info.getUsage().getCommitted () != 1000) {106throw new RuntimeException("usage committed = " +107info.getUsage().getCommitted() +108" expected = 1000");109}110if (info.getUsage().getMax() != 5000) {111throw new RuntimeException("usage max = " +112info.getUsage().getMax() +113" expected = 5000");114}115System.out.print("Pool name = " + info.getPoolName());116System.out.println(" Count = " + info.getCount());117System.out.println("Usage = " + info.getUsage());118}119120public static void badNameCompositeData() throws Exception {121CompositeType ct =122new CompositeType("MyCompositeType",123"CompositeType for MemoryNotificationInfo",124badItemNames,125badItemNames,126validItemTypes);127CompositeData cd =128new CompositeDataSupport(ct,129badItemNames,130values);131132try {133MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);134} catch (IllegalArgumentException e) {135System.out.println("Expected exception: " +136e.getMessage());137return;138}139throw new RuntimeException(140"IllegalArgumentException not thrown");141}142143public static void badTypeCompositeData() throws Exception {144CompositeType ct =145new CompositeType("MyCompositeType",146"CompositeType for MemoryNotificationInfo",147validItemNames,148validItemNames,149badItemTypes);150151final Object[] values1 = {152"Dummy",153"Foo",154new Long(100),155"Bad memory usage object",156"Dummy",157};158159CompositeData cd =160new CompositeDataSupport(ct,161validItemNames,162values1);163164try {165MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);166} catch (IllegalArgumentException e) {167System.out.println("Expected exception: " +168e.getMessage());169return;170}171throw new RuntimeException(172"IllegalArgumentException not thrown");173}174175private static OpenType[] validItemTypes = {176SimpleType.STRING,177SimpleType.STRING,178SimpleType.LONG,179null, // OpenTypeConverter.toOpenType(MemoryUsage.class)180SimpleType.STRING,181};182private static final String[] badItemNames = {183"poolName",184"BadCountName",185"usage",186"dummy1",187"dummy2",188};189private static final OpenType[] badItemTypes = {190SimpleType.STRING,191SimpleType.STRING,192SimpleType.LONG,193SimpleType.STRING, // Bad type194SimpleType.STRING,195};196}197198199