468N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
468N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
468N/A *
468N/A * This code is free software; you can redistribute it and/or modify it
468N/A * under the terms of the GNU General Public License version 2 only, as
468N/A * published by the Free Software Foundation.
468N/A *
468N/A * This code is distributed in the hope that it will be useful, but WITHOUT
468N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
468N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
468N/A * version 2 for more details (a copy is included in the LICENSE file that
468N/A * accompanied this code).
468N/A *
468N/A * You should have received a copy of the GNU General Public License version
468N/A * 2 along with this work; if not, write to the Free Software Foundation,
468N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
468N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
468N/A */
468N/A
468N/A/*
468N/A * @test
468N/A * @bug 6689505
468N/A * @summary Checks that MBeanServerNotification.toString contains the
468N/A * MBean name.
468N/A * @author Daniel Fuchs
468N/A * @compile MBeanServerNotificationTest.java
468N/A * @run main MBeanServerNotificationTest
468N/A */
468N/A
468N/Aimport com.sun.jmx.mbeanserver.Util;
468N/Aimport javax.management.*;
468N/Aimport java.util.concurrent.*;
468N/A
468N/Apublic class MBeanServerNotificationTest {
468N/A final static String[] names = {
468N/A ":type=Wombat", "wombat:type=Wombat",null,
468N/A };
468N/A public static void main(String[] args) throws Exception {
468N/A System.out.println("Test that MBeanServerNotification.toString " +
468N/A "contains the name of the MBean being registered " +
468N/A "or unregistered.");
468N/A int failures = 0;
468N/A final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
468N/A for (String str:names) {
468N/A try {
468N/A final ObjectName name = (str==null)?null:new ObjectName(str);
468N/A failures+=test(mbs, name, name!=null);
468N/A } catch(Exception x) {
468N/A x.printStackTrace(System.out);
468N/A System.out.println("Test failed for: "+str);
468N/A failures++;
468N/A }
468N/A }
468N/A if (failures == 0)
468N/A System.out.println("Test passed");
468N/A else {
468N/A System.out.println("TEST FAILED: " + failures + " failure(s)");
468N/A System.exit(1);
468N/A }
468N/A }
468N/A
468N/A private static enum Registration {
468N/A REGISTER(MBeanServerNotification.REGISTRATION_NOTIFICATION),
468N/A UNREGISTER(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
468N/A final String type;
468N/A private Registration(String type) {this.type = type;}
468N/A public int test(MBeanServerNotification n, ObjectName name) {
468N/A int failures = 0;
468N/A System.out.println("Testing: "+n);
468N/A if (!n.toString().endsWith("[type="+type+
468N/A "][message="+n.getMessage()+
468N/A "][mbeanName="+name+"]")) {
468N/A System.err.println("Test failed for "+ type+
468N/A " ["+name+"]: "+n);
468N/A failures++;
468N/A }
468N/A return failures;
468N/A }
468N/A public MBeanServerNotification create(ObjectName name) {
468N/A return new MBeanServerNotification(type,
468N/A MBeanServerDelegate.DELEGATE_NAME, next(), name);
468N/A }
468N/A private static long next = 0;
468N/A private static synchronized long next() {return next++;}
468N/A
468N/A }
468N/A
468N/A private static int test(MBeanServer mbs, ObjectName name,
468N/A boolean register)
468N/A throws Exception {
468N/A System.out.println("--------" + name + "--------");
468N/A
468N/A int failures = 0;
468N/A for (Registration reg : Registration.values()) {
468N/A failures = reg.test(reg.create(name), name);
468N/A }
468N/A if (!register) return failures;
468N/A
468N/A final ArrayBlockingQueue<Notification> queue =
468N/A new ArrayBlockingQueue<Notification>(10);
468N/A final NotificationListener listener = new NotificationListener() {
468N/A public void handleNotification(Notification notification,
468N/A Object handback) {
468N/A try {
468N/A queue.put(notification);
468N/A } catch(Exception x) {
468N/A x.printStackTrace(System.out);
468N/A }
468N/A }
468N/A };
468N/A mbs.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME,
468N/A listener, null, name);
468N/A final ObjectInstance oi = mbs.registerMBean(new Wombat(), name);
468N/A try {
468N/A failures+=Registration.REGISTER.test((MBeanServerNotification)
468N/A queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
468N/A } finally {
468N/A mbs.unregisterMBean(oi.getObjectName());
468N/A failures+=Registration.UNREGISTER.test((MBeanServerNotification)
468N/A queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
468N/A }
468N/A return failures;
468N/A }
468N/A
468N/A public static interface WombatMBean {}
468N/A public static class Wombat implements WombatMBean {}
468N/A
468N/A}