jdk/test/javax/management/Introspector/IdenticalMBeanInfoTest.java
author ohair
Tue, 12 Apr 2011 18:36:42 -0700
changeset 9064 63c8767f5915
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5042004
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that MBeans with the same class have identical MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * unless they are NotificationBroadcasters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* What we test here is not required by the spec.  It is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
   optimization that can save a considerable amount of memory when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   there are many MBeans of the same type.  There is no reason why two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   Standard MBeans of the same type should have different MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   objects, unless they are NotificationBroadcasters and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   different arrays from getNotificationInfo().  Note that two MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   that share the same MBean interface but are of different classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   cannot have the same MBeanInfo because the MBeanInfo includes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   implementation class name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class IdenticalMBeanInfoTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static String failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static interface WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        public int getReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public int getReadWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public void setReadWrite(int x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public int op(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static class Whatsit implements WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public Whatsit() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Whatsit(int irrelevant) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public int getReadOnly() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public int getReadWrite() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public void setReadWrite(int x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public int op(int x, int y) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static interface BroadcasterMBean extends WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static class Broadcaster extends Whatsit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            implements BroadcasterMBean, NotificationBroadcaster {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private static int nextId = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        private int id = nextId++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void addNotificationListener(NotificationListener l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                            NotificationFilter f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                            Object h) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public void removeNotificationListener(NotificationListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                new MBeanNotificationInfo(new String[] {"type" + id},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                          "something",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                          "a something")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        mbs.registerMBean(new Whatsit(), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        mbs.createMBean(Whatsit.class.getName(), on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        DynamicMBean mbean =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new StandardMBean(new Whatsit(), WhatsitMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        mbs.registerMBean(mbean, on3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        mbs.registerMBean(new Broadcaster(), on4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        mbs.createMBean(Broadcaster.class.getName(), on5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        MBeanInfo mbi1 = mbs.getMBeanInfo(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        MBeanInfo mbi2 = mbs.getMBeanInfo(on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        MBeanInfo mbi3 = mbs.getMBeanInfo(on3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        MBeanInfo mbi4 = mbs.getMBeanInfo(on4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        MBeanInfo mbi5 = mbs.getMBeanInfo(on5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (mbi1 != mbi2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            fail("Two MBeans of the same class should have identical " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                 "MBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (mbi2 != mbi3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.out.println("IGNORING StandardMBean(...) failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                fail("Plain Standard MBean should have identical MBeanInfo " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                     "to StandardMBean(...) with same class as resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (mbi4 == mbi5 || mbi4.equals(mbi5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            fail("Two MBeans of the same class should NOT have identical " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                 "MBeanInfo if they are NotificationBroadcasters and "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                 "do not return the same MBeanNotificationInfo[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("FAILURE: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        failure = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}