1 /* 2 * Copyright (c) 2005, 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 5043762 27 * @summary Attribute existence check fails if 28 * RequiredModelMBean.addAttributeChangeNotificationListener 29 * is called with a non-existent attribute name and there are 30 * no other attributes. 31 * @author Yves Joan 32 * @author Eamonn McManus 33 * 34 * @run clean AddAttributeChangeNotificationListenerTest 35 * @run build AddAttributeChangeNotificationListenerTest 36 * @run main AddAttributeChangeNotificationListenerTest 37 */ 38 39 import java.lang.reflect.Method; 40 import javax.management.AttributeChangeNotification; 41 import javax.management.MBeanException; 42 import javax.management.MBeanServer; 43 import javax.management.MBeanServerFactory; 44 import javax.management.Notification; 45 import javax.management.NotificationListener; 46 import javax.management.ObjectName; 47 import javax.management.RuntimeOperationsException; 48 import javax.management.modelmbean.*; 49 50 /** 51 * We do invoke addAttributeChangeNotificationListener to add 52 * a listener on an attribute not defined in the ModelMBeanInfo 53 * of the RequiredModelMBean instance used. 54 */ 55 public class AddAttributeChangeNotificationListenerTest { 56 57 public static void main(String args[] ) { 58 AddAttributeChangeNotificationListenerTest test = 59 new AddAttributeChangeNotificationListenerTest(); 60 61 try { 62 test.run(args); 63 } catch(Exception e) { 64 System.out.println("FAIL"); 65 e.printStackTrace(); 66 System.exit(1); 67 } 68 69 System.out.println("PASS"); 70 } 71 72 private void run( String[] args) throws Exception { 73 int errCount = 0; 74 String testName = "AddAttributeChangeNotificationListenerTest0001"; 75 ObjectName modelMBeanObjectName = null; 76 ModelMBeanInfo modelMBeanInfo = null; 77 MBeanServer mbeanserver = MBeanServerFactory.newMBeanServer(); 78 String modelMBeanName = "RequiredModelMBean"; 79 String modelMBeanClassName = 80 "javax.management.modelmbean.RequiredModelMBean"; 81 82 modelMBeanObjectName = 83 new ObjectName("AddAttributeChangeNotificationListenerTest:type=" + 84 modelMBeanName); 85 86 System.out.println("Build a ModelMBeanInfo without attribute State"); 87 modelMBeanInfo = createModelMBeanInfo(); 88 89 System.out.println("Create and register a RequiredModelMBean " + 90 "with that MBeanInfo"); 91 Object[] params = { modelMBeanInfo }; 92 String[] sig = { "javax.management.modelmbean.ModelMBeanInfo" }; 93 mbeanserver.createMBean(modelMBeanClassName, 94 modelMBeanObjectName, 95 params, 96 sig); 97 98 ModelMBeanListener aListener = new ModelMBeanListener(); 99 100 // add an attribute change listener 101 System.out.println("Add an attribute change listener for State"); 102 try { 103 mbeanserver.invoke(modelMBeanObjectName, 104 "addAttributeChangeNotificationListener", 105 (new Object[] {aListener, "State", null}), 106 (new String[] {"javax.management.NotificationListener", 107 "java.lang.String", 108 "java.lang.Object"})); 109 System.out.println("NOK: Did not get expected " + 110 "RuntimeOperationsException"); 111 errCount++; 112 } catch (Exception e) { 113 if (e instanceof MBeanException) 114 e = ((MBeanException) e).getTargetException(); 115 if (e instanceof RuntimeOperationsException) { 116 RuntimeOperationsException roe = 117 (RuntimeOperationsException) e; 118 Exception target = roe.getTargetException(); 119 System.out.println("OK: Got expected RuntimeOperationsException"); 120 121 if ( target instanceof IllegalArgumentException ) { 122 System.out.println("OK: Got expected " + 123 "wrapped IllegalArgumentException"); 124 } else { 125 System.out.println("NOK: Got wrapped " 126 + target 127 + " as we expect IllegalArgumentException"); 128 errCount++; 129 } 130 } else { 131 System.out.println("NOK: Got " 132 + e 133 + " as we expect RuntimeOperationsException"); 134 errCount++; 135 } 136 } 137 138 if ( errCount != 0 ) 139 throw new Exception(errCount 140 + " error(s) occured"); 141 } 142 143 /** 144 * Returns a ModelMBeanInfo with two operations: 145 * setManagedResource 146 * addAttributeChangeNotificationListener 147 */ 148 private ModelMBeanInfo createModelMBeanInfo() throws Exception { 149 // operation setManagedResource 150 String descriptionOp1Set = "ManagedResource description setter"; 151 Class[] paramsSet1 = {Class.forName("java.lang.Object"), 152 Class.forName("java.lang.String")}; 153 Method oper1Set = 154 RequiredModelMBean.class.getMethod("setManagedResource", 155 paramsSet1); 156 ModelMBeanOperationInfo operation1Set = 157 new ModelMBeanOperationInfo(descriptionOp1Set, 158 oper1Set); 159 160 // operation addAttributeChangeNotificationListener 161 String descriptionop2Set = 162 "addAttributeChangeNotificationListener description"; 163 Class [] paramsSet2 = 164 {Class.forName("javax.management.NotificationListener"), 165 Class.forName("java.lang.String"), 166 Class.forName("java.lang.Object")}; 167 Method oper2Set = 168 RequiredModelMBean.class.getMethod( 169 "addAttributeChangeNotificationListener", 170 paramsSet2); 171 ModelMBeanOperationInfo operation2Set = 172 new ModelMBeanOperationInfo(descriptionop2Set, 173 oper2Set); 174 175 // define ModelMBeanInfo 176 String className = "ModelMBeansInfo"; 177 String descriptionmodel = "Model MBean Test"; 178 ModelMBeanAttributeInfo[] attributes = null; 179 ModelMBeanOperationInfo[] operations = { 180 operation1Set, 181 operation2Set 182 }; 183 ModelMBeanNotificationInfo[] notifications = null; 184 ModelMBeanConstructorInfo[] constructors = null; 185 186 ModelMBeanInfoSupport modelMBeanInfo = 187 new ModelMBeanInfoSupport(className, 188 descriptionmodel, 189 attributes, 190 constructors, 191 operations, 192 notifications); 193 return modelMBeanInfo; 194 } 195 196 public static class ModelMBeanListener implements NotificationListener { 197 198 public ModelMBeanListener() { 199 tally = 0; 200 } 201 202 public void handleNotification(Notification acn, Object handback) { 203 tally++; 204 } 205 206 public int getCount() { 207 return tally; 208 } 209 210 public int setCount(int newTally) { 211 tally = newTally; 212 return tally; 213 } 214 215 private int tally = 0; 216 217 } 218 }