1 /*
   2  * Copyright (c) 2005, 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 6295859
  27  * @summary Check that a StandardMBean has immutableInfo=true if it is
  28  * a NotificationBroadcasterSupport that doesn't override getNotificationInfo()
  29  * @author Eamonn McManus
  30  *
  31  * @run clean ImmutableNotificationInfoTest
  32  * @run build ImmutableNotificationInfoTest
  33  * @run main ImmutableNotificationInfoTest
  34  */
  35 import javax.management.Descriptor;
  36 import javax.management.MBeanInfo;
  37 import javax.management.MBeanNotificationInfo;
  38 import javax.management.MBeanServer;
  39 import javax.management.MBeanServerFactory;
  40 import javax.management.NotificationBroadcaster;
  41 import javax.management.NotificationBroadcasterSupport;
  42 import javax.management.NotificationFilter;
  43 import javax.management.NotificationListener;
  44 import javax.management.ObjectName;
  45 
  46 public class ImmutableNotificationInfoTest {
  47     public interface UserBroadcasterMBean {}
  48     public interface NoOverrideNBSMBean {}
  49     public interface OverrideNBSMBean {}
  50 
  51     public static class UserBroadcaster
  52             implements UserBroadcasterMBean, NotificationBroadcaster {
  53         public void removeNotificationListener(NotificationListener listener) {
  54         }
  55 
  56         public void addNotificationListener(NotificationListener listener,
  57                 NotificationFilter filter, Object handback) {
  58         }
  59 
  60         public MBeanNotificationInfo[] getNotificationInfo() {
  61             return new MBeanNotificationInfo[0];
  62         }
  63     }
  64 
  65     public static class NoOverrideNBS extends NotificationBroadcasterSupport
  66             implements NoOverrideNBSMBean {
  67     }
  68 
  69     public static class OverrideNBS extends NotificationBroadcasterSupport
  70             implements OverrideNBSMBean {
  71         public MBeanNotificationInfo[] getNotificationInfo() {
  72             return new MBeanNotificationInfo[0];
  73         }
  74     }
  75 
  76     public static void main(String[] args) throws Exception {
  77         boolean ok;
  78         ok = test(new UserBroadcaster(), false);
  79         ok &= test(new NoOverrideNBS(), true);
  80         ok &= test(new OverrideNBS(), false);
  81         if (!ok)
  82             throw new Exception("TEST FAILED: immutability incorrect");
  83     }
  84 
  85     private static boolean test(Object mbean, boolean expectImmutable)
  86             throws Exception {
  87         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  88         ObjectName on = new ObjectName("a:b=c");
  89         mbs.registerMBean(mbean, on);
  90         MBeanInfo mbi = mbs.getMBeanInfo(on);
  91         Descriptor d = mbi.getDescriptor();
  92         String immutableValue = (String) d.getFieldValue("immutableInfo");
  93         boolean immutable = ("true".equals(immutableValue));
  94         if (immutable != expectImmutable) {
  95             System.out.println("FAILED: " + mbean.getClass().getName() +
  96                     " -> " + immutableValue);
  97             return false;
  98         } else {
  99             System.out.println("OK: " + mbean.getClass().getName());
 100             return true;
 101         }
 102     }
 103 }