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