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