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 MemoryNotificationInfo.from to return a valid 28 * MemoryNotificationInfo object. Or throw exception if 29 * the input CompositeData is invalid. 30 * @author Mandy Chung 31 * 32 * @modules java.management/sun.management 33 * 34 * @compile OpenTypeConverter.java 35 * @build MemoryNotifInfoCompositeData 36 * @run main MemoryNotifInfoCompositeData 37 */ 38 39 import javax.management.openmbean.*; 40 import java.lang.management.MemoryNotificationInfo; 41 import java.lang.management.MemoryUsage; 42 import sun.management.MemoryUsageCompositeData; 43 44 public class MemoryNotifInfoCompositeData { 45 public static void main(String[] argv) throws Exception { 46 createGoodCompositeData(); 47 badNameCompositeData(); 48 badTypeCompositeData(); 49 System.out.println("Test passed"); 50 } 51 52 private static final int POOL_NAME = 1; 53 private static final int COUNT = 2; 54 private static final int USAGE = 3; 55 private static final String[] validItemNames = { 56 "dummy1", 57 "poolName", 58 "count", 59 "usage", 60 "dummy2", 61 }; 62 63 // these values are synchronized with the item names 64 private static final Object[] values = { 65 "Dummy", 66 "Foo", 67 new Long(100), 68 MemoryUsageCompositeData. 69 toCompositeData(new MemoryUsage(0, 100, 1000, 5000)), 70 "Dummy", 71 }; 72 73 public static void createGoodCompositeData() throws Exception { 74 75 // get the CompositeType for MemoryUsage 76 validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class); 77 CompositeType ct = 78 new CompositeType("MyCompositeType", 79 "CompositeType for MemoryNotificationInfo", 80 validItemNames, 81 validItemNames, 82 validItemTypes); 83 CompositeData cd = 84 new CompositeDataSupport(ct, 85 validItemNames, 86 values); 87 88 MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); 89 if (!info.getPoolName().equals(values[POOL_NAME])) { 90 throw new RuntimeException("pool name = " + info.getPoolName() + 91 " expected = " + values[POOL_NAME]); 92 } 93 if (info.getCount() != ((Long) values[COUNT]).longValue()) { 94 throw new RuntimeException("count = " + info.getCount() + 95 " expected = " + values[COUNT]); 96 } 97 if (info.getUsage().getInit() != 0) { 98 throw new RuntimeException("usage init = " + 99 info.getUsage().getInit() + 100 " expected = 0"); 101 } 102 if (info.getUsage().getUsed() != 100) { 103 throw new RuntimeException("usage used = " + 104 info.getUsage().getUsed() + 105 " expected = 100"); 106 } 107 if (info.getUsage().getCommitted () != 1000) { 108 throw new RuntimeException("usage committed = " + 109 info.getUsage().getCommitted() + 110 " expected = 1000"); 111 } 112 if (info.getUsage().getMax() != 5000) { 113 throw new RuntimeException("usage max = " + 114 info.getUsage().getMax() + 115 " expected = 5000"); 116 } 117 System.out.print("Pool name = " + info.getPoolName()); 118 System.out.println(" Count = " + info.getCount()); 119 System.out.println("Usage = " + info.getUsage()); 120 } 121 122 public static void badNameCompositeData() throws Exception { 123 CompositeType ct = 124 new CompositeType("MyCompositeType", 125 "CompositeType for MemoryNotificationInfo", 126 badItemNames, 127 badItemNames, 128 validItemTypes); 129 CompositeData cd = 130 new CompositeDataSupport(ct, 131 badItemNames, 132 values); 133 134 try { 135 MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); 136 } catch (IllegalArgumentException e) { 137 System.out.println("Expected exception: " + 138 e.getMessage()); 139 return; 140 } 141 throw new RuntimeException( 142 "IllegalArgumentException not thrown"); 143 } 144 145 public static void badTypeCompositeData() throws Exception { 146 CompositeType ct = 147 new CompositeType("MyCompositeType", 148 "CompositeType for MemoryNotificationInfo", 149 validItemNames, 150 validItemNames, 151 badItemTypes); 152 153 final Object[] values1 = { 154 "Dummy", 155 "Foo", 156 new Long(100), 157 "Bad memory usage object", 158 "Dummy", 159 }; 160 161 CompositeData cd = 162 new CompositeDataSupport(ct, 163 validItemNames, 164 values1); 165 166 try { 167 MemoryNotificationInfo info = MemoryNotificationInfo.from(cd); 168 } catch (IllegalArgumentException e) { 169 System.out.println("Expected exception: " + 170 e.getMessage()); 171 return; 172 } 173 throw new RuntimeException( 174 "IllegalArgumentException not thrown"); 175 } 176 177 private static OpenType[] validItemTypes = { 178 SimpleType.STRING, 179 SimpleType.STRING, 180 SimpleType.LONG, 181 null, // OpenTypeConverter.toOpenType(MemoryUsage.class) 182 SimpleType.STRING, 183 }; 184 private static final String[] badItemNames = { 185 "poolName", 186 "BadCountName", 187 "usage", 188 "dummy1", 189 "dummy2", 190 }; 191 private static final OpenType[] badItemTypes = { 192 SimpleType.STRING, 193 SimpleType.STRING, 194 SimpleType.LONG, 195 SimpleType.STRING, // Bad type 196 SimpleType.STRING, 197 }; 198 }