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