1 /* 2 * Copyright (c) 2008, 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 6689505 27 * @summary Checks that MBeanServerNotification.toString contains the 28 * MBean name. 29 * @author Daniel Fuchs 30 * @modules java.management/com.sun.jmx.mbeanserver 31 * @compile MBeanServerNotificationTest.java 32 * @run main MBeanServerNotificationTest 33 */ 34 35 import com.sun.jmx.mbeanserver.Util; 36 import javax.management.*; 37 import java.util.concurrent.*; 38 39 public class MBeanServerNotificationTest { 40 final static String[] names = { 41 ":type=Wombat", "wombat:type=Wombat",null, 42 }; 43 public static void main(String[] args) throws Exception { 44 System.out.println("Test that MBeanServerNotification.toString " + 45 "contains the name of the MBean being registered " + 46 "or unregistered."); 47 int failures = 0; 48 final MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 49 for (String str:names) { 50 try { 51 final ObjectName name = (str==null)?null:new ObjectName(str); 52 failures+=test(mbs, name, name!=null); 53 } catch(Exception x) { 54 x.printStackTrace(System.out); 55 System.out.println("Test failed for: "+str); 56 failures++; 57 } 58 } 59 if (failures == 0) 60 System.out.println("Test passed"); 61 else { 62 System.out.println("TEST FAILED: " + failures + " failure(s)"); 63 System.exit(1); 64 } 65 } 66 67 private static enum Registration { 68 REGISTER(MBeanServerNotification.REGISTRATION_NOTIFICATION), 69 UNREGISTER(MBeanServerNotification.UNREGISTRATION_NOTIFICATION); 70 final String type; 71 private Registration(String type) {this.type = type;} 72 public int test(MBeanServerNotification n, ObjectName name) { 73 int failures = 0; 74 System.out.println("Testing: "+n); 75 if (!n.toString().endsWith("[type="+type+ 76 "][message="+n.getMessage()+ 77 "][mbeanName="+name+"]")) { 78 System.err.println("Test failed for "+ type+ 79 " ["+name+"]: "+n); 80 failures++; 81 } 82 return failures; 83 } 84 public MBeanServerNotification create(ObjectName name) { 85 return new MBeanServerNotification(type, 86 MBeanServerDelegate.DELEGATE_NAME, next(), name); 87 } 88 private static long next = 0; 89 private static synchronized long next() {return next++;} 90 91 } 92 93 private static int test(MBeanServer mbs, ObjectName name, 94 boolean register) 95 throws Exception { 96 System.out.println("--------" + name + "--------"); 97 98 int failures = 0; 99 for (Registration reg : Registration.values()) { 100 failures = reg.test(reg.create(name), name); 101 } 102 if (!register) return failures; 103 104 final ArrayBlockingQueue<Notification> queue = 105 new ArrayBlockingQueue<Notification>(10); 106 final NotificationListener listener = new NotificationListener() { 107 public void handleNotification(Notification notification, 108 Object handback) { 109 try { 110 queue.put(notification); 111 } catch(Exception x) { 112 x.printStackTrace(System.out); 113 } 114 } 115 }; 116 mbs.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, 117 listener, null, name); 118 final ObjectInstance oi = mbs.registerMBean(new Wombat(), name); 119 try { 120 failures+=Registration.REGISTER.test((MBeanServerNotification) 121 queue.poll(2, TimeUnit.SECONDS), oi.getObjectName()); 122 } finally { 123 mbs.unregisterMBean(oi.getObjectName()); 124 failures+=Registration.UNREGISTER.test((MBeanServerNotification) 125 queue.poll(2, TimeUnit.SECONDS), oi.getObjectName()); 126 } 127 return failures; 128 } 129 130 public static interface WombatMBean {} 131 public static class Wombat implements WombatMBean {} 132 133 }