jdk/test/javax/management/Introspector/IdenticalMBeanInfoTest.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main IdenticalMBeanInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* What we test here is not required by the spec.  It is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   optimization that can save a considerable amount of memory when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   there are many MBeans of the same type.  There is no reason why two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   Standard MBeans of the same type should have different MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   objects, unless they are NotificationBroadcasters and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   different arrays from getNotificationInfo().  Note that two MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   that share the same MBean interface but are of different classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   cannot have the same MBeanInfo because the MBeanInfo includes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   implementation class name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class IdenticalMBeanInfoTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static interface WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public int getReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public int getReadWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public void setReadWrite(int x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public int op(int x, int y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static class Whatsit implements WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Whatsit() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public Whatsit(int irrelevant) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public int getReadOnly() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public int getReadWrite() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public void setReadWrite(int x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public int op(int x, int y) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static interface BroadcasterMBean extends WhatsitMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static class Broadcaster extends Whatsit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            implements BroadcasterMBean, NotificationBroadcaster {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        private static int nextId = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private int id = nextId++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void addNotificationListener(NotificationListener l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                            NotificationFilter f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                            Object h) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public void removeNotificationListener(NotificationListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                new MBeanNotificationInfo(new String[] {"type" + id},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                          "something",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                          "a something")
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        mbs.registerMBean(new Whatsit(), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        mbs.createMBean(Whatsit.class.getName(), on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        DynamicMBean mbean =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            new StandardMBean(new Whatsit(), WhatsitMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        mbs.registerMBean(mbean, on3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        mbs.registerMBean(new Broadcaster(), on4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        mbs.createMBean(Broadcaster.class.getName(), on5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        MBeanInfo mbi1 = mbs.getMBeanInfo(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        MBeanInfo mbi2 = mbs.getMBeanInfo(on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        MBeanInfo mbi3 = mbs.getMBeanInfo(on3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        MBeanInfo mbi4 = mbs.getMBeanInfo(on4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        MBeanInfo mbi5 = mbs.getMBeanInfo(on5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (mbi1 != mbi2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            fail("Two MBeans of the same class should have identical " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                 "MBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (mbi2 != mbi3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println("IGNORING StandardMBean(...) failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                fail("Plain Standard MBean should have identical MBeanInfo " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                     "to StandardMBean(...) with same class as resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (mbi4 == mbi5 || mbi4.equals(mbi5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            fail("Two MBeans of the same class should NOT have identical " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                 "MBeanInfo if they are NotificationBroadcasters and "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                 "do not return the same MBeanNotificationInfo[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.out.println("FAILURE: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        failure = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}