Path: blob/master/test/jdk/java/lang/management/CompositeData/MemoryUsageCompositeData.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 MemoryUsage.from() method to return a valid MemoryUsage27* or throw exception if the input CompositeData is invalid.28* @author Mandy Chung29*30* @build MemoryUsageCompositeData31* @run main MemoryUsageCompositeData32*/3334import javax.management.openmbean.*;35import java.lang.management.MemoryUsage;3637public class MemoryUsageCompositeData {38public static void main(String[] argv) throws Exception {39createGoodCompositeData();40badTypeCompositeData();41badNameCompositeData();42System.out.println("Test passed");43}4445public static void createGoodCompositeData() throws Exception {46final int K = 1024;47// these values are synchronized with the item names48final Object[] values = {49new Long(5 * K), // committed50new Long(1 * K), // init51new Long(10 * K), // max52new Long(2 * K), // used53"Dummy",54"Dummy",55};5657CompositeType muct =58new CompositeType("MyMemoryUsageCompositeType",59"CompositeType for MemoryUsage",60memoryUsageItemNames,61memoryUsageItemNames,62memoryUsageItemTypes);63CompositeData cd =64new CompositeDataSupport(muct,65memoryUsageItemNames,66values);67MemoryUsage u = MemoryUsage.from(cd);68if (u.getInit() != ((Long) values[INIT]).longValue()) {69throw new RuntimeException("init = " + u.getInit() +70" expected = " + values[INIT]);71}72if (u.getUsed() != ((Long) values[USED]).longValue()) {73throw new RuntimeException("used = " + u.getUsed() +74" expected = " + values[USED]);75}76if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) {77throw new RuntimeException("committed = " + u.getCommitted() +78" expected = " + values[COMMITTED]);79}80if (u.getMax() != ((Long) values[MAX]).longValue()) {81throw new RuntimeException("max = " + u.getMax() +82" expected = " + values[MAX]);83}84System.out.println(u);85}8687public static void badTypeCompositeData() throws Exception {88final int K = 1024;89final Object[] values = {90new Integer(5 * K),91new Long(1 * K),92new Long(10 * K),93new Long(2 * K),94"Dummy",95"Dummy",96};9798CompositeType muct =99new CompositeType("MyMemoryUsageCompositeType",100"CompositeType for MemoryUsage",101memoryUsageItemNames,102memoryUsageItemNames,103badMUItemTypes);104CompositeData cd =105new CompositeDataSupport(muct,106memoryUsageItemNames,107values);108try {109MemoryUsage u = MemoryUsage.from(cd);110} catch (IllegalArgumentException e) {111System.out.println("Expected exception: " +112e.getMessage());113return;114}115throw new RuntimeException(116"IllegalArgumentException not thrown");117}118119public static void badNameCompositeData() throws Exception {120final int K = 1024;121final Object[] values = {122new Long(5 * K),123new Long(1 * K),124new Long(10 * K),125new Long(2 * K),126"Dummy",127"Dummy",128};129130CompositeType muct =131new CompositeType("MyMemoryUsageCompositeType",132"CompositeType for MemoryUsage",133badMUItemNames,134badMUItemNames,135memoryUsageItemTypes);136CompositeData cd =137new CompositeDataSupport(muct,138badMUItemNames,139values);140try {141MemoryUsage u = MemoryUsage.from(cd);142} catch (IllegalArgumentException e) {143System.out.println("Expected exception: " +144e.getMessage());145return;146}147throw new RuntimeException(148"IllegalArgumentException not thrown");149}150151private static final int COMMITTED = 0;152private static final int INIT = 1;153private static final int MAX = 2;154private static final int USED = 3;155private static final String[] memoryUsageItemNames = {156"committed",157"init",158"max",159"used",160"dummy1",161"dummy2",162};163private static final OpenType[] memoryUsageItemTypes = {164SimpleType.LONG,165SimpleType.LONG,166SimpleType.LONG,167SimpleType.LONG,168SimpleType.STRING,169SimpleType.STRING,170};171private static final String[] badMUItemNames = {172"Committed",173"Init",174"max",175"used",176"dummy1",177"dummy2",178};179private static final OpenType[] badMUItemTypes = {180SimpleType.INTEGER,181SimpleType.LONG,182SimpleType.LONG,183SimpleType.LONG,184SimpleType.STRING,185SimpleType.STRING,186};187}188189190