0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5042004
0N/A * @summary Check that MBeans with the same class have identical MBeanInfo
0N/A * unless they are NotificationBroadcasters
0N/A * @author Eamonn McManus
0N/A * @run clean IdenticalMBeanInfoTest
0N/A * @run build IdenticalMBeanInfoTest
0N/A * @run main IdenticalMBeanInfoTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/A
0N/A/* What we test here is not required by the spec. It is an
0N/A optimization that can save a considerable amount of memory when
0N/A there are many MBeans of the same type. There is no reason why two
0N/A Standard MBeans of the same type should have different MBeanInfo
0N/A objects, unless they are NotificationBroadcasters and return
0N/A different arrays from getNotificationInfo(). Note that two MBeans
0N/A that share the same MBean interface but are of different classes
0N/A cannot have the same MBeanInfo because the MBeanInfo includes the
0N/A implementation class name. */
0N/Apublic class IdenticalMBeanInfoTest {
0N/A private static String failure = null;
0N/A
0N/A public static interface WhatsitMBean {
0N/A public int getReadOnly();
0N/A public int getReadWrite();
0N/A public void setReadWrite(int x);
0N/A public int op(int x, int y);
0N/A }
0N/A
0N/A public static class Whatsit implements WhatsitMBean {
0N/A public Whatsit() {}
0N/A public Whatsit(int irrelevant) {}
0N/A
0N/A public int getReadOnly() { return 0; }
0N/A public int getReadWrite() { return 0; }
0N/A public void setReadWrite(int x) {}
0N/A public int op(int x, int y) { return 0; }
0N/A }
0N/A
0N/A public static interface BroadcasterMBean extends WhatsitMBean {
0N/A }
0N/A
0N/A public static class Broadcaster extends Whatsit
0N/A implements BroadcasterMBean, NotificationBroadcaster {
0N/A private static int nextId = 1;
0N/A private int id = nextId++;
0N/A
0N/A public void addNotificationListener(NotificationListener l,
0N/A NotificationFilter f,
0N/A Object h) {}
0N/A
0N/A public void removeNotificationListener(NotificationListener l) {}
0N/A
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A return new MBeanNotificationInfo[] {
0N/A new MBeanNotificationInfo(new String[] {"type" + id},
0N/A "something",
0N/A "a something")
0N/A };
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");
0N/A ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");
0N/A ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");
0N/A ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");
0N/A ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");
0N/A
0N/A mbs.registerMBean(new Whatsit(), on1);
0N/A mbs.createMBean(Whatsit.class.getName(), on2);
0N/A DynamicMBean mbean =
0N/A new StandardMBean(new Whatsit(), WhatsitMBean.class);
0N/A mbs.registerMBean(mbean, on3);
0N/A mbs.registerMBean(new Broadcaster(), on4);
0N/A mbs.createMBean(Broadcaster.class.getName(), on5);
0N/A MBeanInfo mbi1 = mbs.getMBeanInfo(on1);
0N/A MBeanInfo mbi2 = mbs.getMBeanInfo(on2);
0N/A MBeanInfo mbi3 = mbs.getMBeanInfo(on3);
0N/A MBeanInfo mbi4 = mbs.getMBeanInfo(on4);
0N/A MBeanInfo mbi5 = mbs.getMBeanInfo(on5);
0N/A
0N/A if (mbi1 != mbi2) {
0N/A fail("Two MBeans of the same class should have identical " +
0N/A "MBeanInfo");
0N/A }
0N/A
0N/A if (mbi2 != mbi3) {
0N/A if (true)
0N/A System.out.println("IGNORING StandardMBean(...) failure");
0N/A else
0N/A fail("Plain Standard MBean should have identical MBeanInfo " +
0N/A "to StandardMBean(...) with same class as resource");
0N/A }
0N/A
0N/A if (mbi4 == mbi5 || mbi4.equals(mbi5)) {
0N/A fail("Two MBeans of the same class should NOT have identical " +
0N/A "MBeanInfo if they are NotificationBroadcasters and "+
0N/A "do not return the same MBeanNotificationInfo[]");
0N/A }
0N/A
0N/A if (failure == null) {
0N/A System.out.println("Test passed");
0N/A return;
0N/A }
0N/A
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A }
0N/A
0N/A private static void fail(String why) {
0N/A System.out.println("FAILURE: " + why);
0N/A failure = why;
0N/A }
0N/A}