Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/IdenticalMBeanInfoTest.java
38839 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 504200426* @summary Check that MBeans with the same class have identical MBeanInfo27* unless they are NotificationBroadcasters28* @author Eamonn McManus29* @run clean IdenticalMBeanInfoTest30* @run build IdenticalMBeanInfoTest31* @run main IdenticalMBeanInfoTest32*/3334import javax.management.*;3536/* What we test here is not required by the spec. It is an37optimization that can save a considerable amount of memory when38there are many MBeans of the same type. There is no reason why two39Standard MBeans of the same type should have different MBeanInfo40objects, unless they are NotificationBroadcasters and return41different arrays from getNotificationInfo(). Note that two MBeans42that share the same MBean interface but are of different classes43cannot have the same MBeanInfo because the MBeanInfo includes the44implementation class name. */45public class IdenticalMBeanInfoTest {46private static String failure = null;4748public static interface WhatsitMBean {49public int getReadOnly();50public int getReadWrite();51public void setReadWrite(int x);52public int op(int x, int y);53}5455public static class Whatsit implements WhatsitMBean {56public Whatsit() {}57public Whatsit(int irrelevant) {}5859public int getReadOnly() { return 0; }60public int getReadWrite() { return 0; }61public void setReadWrite(int x) {}62public int op(int x, int y) { return 0; }63}6465public static interface BroadcasterMBean extends WhatsitMBean {66}6768public static class Broadcaster extends Whatsit69implements BroadcasterMBean, NotificationBroadcaster {70private static int nextId = 1;71private int id = nextId++;7273public void addNotificationListener(NotificationListener l,74NotificationFilter f,75Object h) {}7677public void removeNotificationListener(NotificationListener l) {}7879public MBeanNotificationInfo[] getNotificationInfo() {80return new MBeanNotificationInfo[] {81new MBeanNotificationInfo(new String[] {"type" + id},82"something",83"a something")84};85}86}8788public static void main(String[] args) throws Exception {89MBeanServer mbs = MBeanServerFactory.createMBeanServer();90ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");91ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");92ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");93ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");94ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");9596mbs.registerMBean(new Whatsit(), on1);97mbs.createMBean(Whatsit.class.getName(), on2);98DynamicMBean mbean =99new StandardMBean(new Whatsit(), WhatsitMBean.class);100mbs.registerMBean(mbean, on3);101mbs.registerMBean(new Broadcaster(), on4);102mbs.createMBean(Broadcaster.class.getName(), on5);103MBeanInfo mbi1 = mbs.getMBeanInfo(on1);104MBeanInfo mbi2 = mbs.getMBeanInfo(on2);105MBeanInfo mbi3 = mbs.getMBeanInfo(on3);106MBeanInfo mbi4 = mbs.getMBeanInfo(on4);107MBeanInfo mbi5 = mbs.getMBeanInfo(on5);108109if (mbi1 != mbi2) {110fail("Two MBeans of the same class should have identical " +111"MBeanInfo");112}113114if (mbi2 != mbi3) {115if (true)116System.out.println("IGNORING StandardMBean(...) failure");117else118fail("Plain Standard MBean should have identical MBeanInfo " +119"to StandardMBean(...) with same class as resource");120}121122if (mbi4 == mbi5 || mbi4.equals(mbi5)) {123fail("Two MBeans of the same class should NOT have identical " +124"MBeanInfo if they are NotificationBroadcasters and "+125"do not return the same MBeanNotificationInfo[]");126}127128if (failure == null) {129System.out.println("Test passed");130return;131}132133throw new Exception("TEST FAILED: " + failure);134}135136private static void fail(String why) {137System.out.println("FAILURE: " + why);138failure = why;139}140}141142143