1 /* 2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4982289 27 * @summary Test MemoryUsage.from() method to return a valid MemoryUsage 28 * or throw exception if the input CompositeData is invalid. 29 * @author Mandy Chung 30 * 31 * @modules java.management 32 * @build MemoryUsageCompositeData 33 * @run main MemoryUsageCompositeData 34 */ 35 36 import javax.management.openmbean.*; 37 import java.lang.management.MemoryUsage; 38 39 public class MemoryUsageCompositeData { 40 public static void main(String[] argv) throws Exception { 41 createGoodCompositeData(); 42 badTypeCompositeData(); 43 badNameCompositeData(); 44 System.out.println("Test passed"); 45 } 46 47 public static void createGoodCompositeData() throws Exception { 48 final int K = 1024; 49 // these values are synchronized with the item names 50 final Object[] values = { 51 new Long(5 * K), // committed 52 new Long(1 * K), // init 53 new Long(10 * K), // max 54 new Long(2 * K), // used 55 "Dummy", 56 "Dummy", 57 }; 58 59 CompositeType muct = 60 new CompositeType("MyMemoryUsageCompositeType", 61 "CompositeType for MemoryUsage", 62 memoryUsageItemNames, 63 memoryUsageItemNames, 64 memoryUsageItemTypes); 65 CompositeData cd = 66 new CompositeDataSupport(muct, 67 memoryUsageItemNames, 68 values); 69 MemoryUsage u = MemoryUsage.from(cd); 70 if (u.getInit() != ((Long) values[INIT]).longValue()) { 71 throw new RuntimeException("init = " + u.getInit() + 72 " expected = " + values[INIT]); 73 } 74 if (u.getUsed() != ((Long) values[USED]).longValue()) { 75 throw new RuntimeException("used = " + u.getUsed() + 76 " expected = " + values[USED]); 77 } 78 if (u.getCommitted() != ((Long) values[COMMITTED]).longValue()) { 79 throw new RuntimeException("committed = " + u.getCommitted() + 80 " expected = " + values[COMMITTED]); 81 } 82 if (u.getMax() != ((Long) values[MAX]).longValue()) { 83 throw new RuntimeException("max = " + u.getMax() + 84 " expected = " + values[MAX]); 85 } 86 System.out.println(u); 87 } 88 89 public static void badTypeCompositeData() throws Exception { 90 final int K = 1024; 91 final Object[] values = { 92 new Integer(5 * K), 93 new Long(1 * K), 94 new Long(10 * K), 95 new Long(2 * K), 96 "Dummy", 97 "Dummy", 98 }; 99 100 CompositeType muct = 101 new CompositeType("MyMemoryUsageCompositeType", 102 "CompositeType for MemoryUsage", 103 memoryUsageItemNames, 104 memoryUsageItemNames, 105 badMUItemTypes); 106 CompositeData cd = 107 new CompositeDataSupport(muct, 108 memoryUsageItemNames, 109 values); 110 try { 111 MemoryUsage u = MemoryUsage.from(cd); 112 } catch (IllegalArgumentException e) { 113 System.out.println("Expected exception: " + 114 e.getMessage()); 115 return; 116 } 117 throw new RuntimeException( 118 "IllegalArgumentException not thrown"); 119 } 120 121 public static void badNameCompositeData() throws Exception { 122 final int K = 1024; 123 final Object[] values = { 124 new Long(5 * K), 125 new Long(1 * K), 126 new Long(10 * K), 127 new Long(2 * K), 128 "Dummy", 129 "Dummy", 130 }; 131 132 CompositeType muct = 133 new CompositeType("MyMemoryUsageCompositeType", 134 "CompositeType for MemoryUsage", 135 badMUItemNames, 136 badMUItemNames, 137 memoryUsageItemTypes); 138 CompositeData cd = 139 new CompositeDataSupport(muct, 140 badMUItemNames, 141 values); 142 try { 143 MemoryUsage u = MemoryUsage.from(cd); 144 } catch (IllegalArgumentException e) { 145 System.out.println("Expected exception: " + 146 e.getMessage()); 147 return; 148 } 149 throw new RuntimeException( 150 "IllegalArgumentException not thrown"); 151 } 152 153 private static final int COMMITTED = 0; 154 private static final int INIT = 1; 155 private static final int MAX = 2; 156 private static final int USED = 3; 157 private static final String[] memoryUsageItemNames = { 158 "committed", 159 "init", 160 "max", 161 "used", 162 "dummy1", 163 "dummy2", 164 }; 165 private static final OpenType[] memoryUsageItemTypes = { 166 SimpleType.LONG, 167 SimpleType.LONG, 168 SimpleType.LONG, 169 SimpleType.LONG, 170 SimpleType.STRING, 171 SimpleType.STRING, 172 }; 173 private static final String[] badMUItemNames = { 174 "Committed", 175 "Init", 176 "max", 177 "used", 178 "dummy1", 179 "dummy2", 180 }; 181 private static final OpenType[] badMUItemTypes = { 182 SimpleType.INTEGER, 183 SimpleType.LONG, 184 SimpleType.LONG, 185 SimpleType.LONG, 186 SimpleType.STRING, 187 SimpleType.STRING, 188 }; 189 }