Path: blob/master/test/jdk/javax/management/Introspector/IdenticalMBeanInfoTest.java
41152 views
/*1* Copyright (c) 2005, 2015, 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*30* @run clean IdenticalMBeanInfoTest31* @run build IdenticalMBeanInfoTest32* @run main IdenticalMBeanInfoTest33*/3435import javax.management.*;3637/* What we test here is not required by the spec. It is an38optimization that can save a considerable amount of memory when39there are many MBeans of the same type. There is no reason why two40Standard MBeans of the same type should have different MBeanInfo41objects, unless they are NotificationBroadcasters and return42different arrays from getNotificationInfo(). Note that two MBeans43that share the same MBean interface but are of different classes44cannot have the same MBeanInfo because the MBeanInfo includes the45implementation class name. */46public class IdenticalMBeanInfoTest {47private static String failure = null;4849public static interface WhatsitMBean {50public int getReadOnly();51public int getReadWrite();52public void setReadWrite(int x);53public int op(int x, int y);54}5556public static class Whatsit implements WhatsitMBean {57public Whatsit() {}58public Whatsit(int irrelevant) {}5960public int getReadOnly() { return 0; }61public int getReadWrite() { return 0; }62public void setReadWrite(int x) {}63public int op(int x, int y) { return 0; }64}6566public static interface BroadcasterMBean extends WhatsitMBean {67}6869public static class Broadcaster extends Whatsit70implements BroadcasterMBean, NotificationBroadcaster {71private static int nextId = 1;72private int id = nextId++;7374public void addNotificationListener(NotificationListener l,75NotificationFilter f,76Object h) {}7778public void removeNotificationListener(NotificationListener l) {}7980public MBeanNotificationInfo[] getNotificationInfo() {81return new MBeanNotificationInfo[] {82new MBeanNotificationInfo(new String[] {"type" + id},83"something",84"a something")85};86}87}8889public static void main(String[] args) throws Exception {90MBeanServer mbs = MBeanServerFactory.createMBeanServer();91ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");92ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");93ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");94ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");95ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");9697mbs.registerMBean(new Whatsit(), on1);98mbs.createMBean(Whatsit.class.getName(), on2);99DynamicMBean mbean =100new StandardMBean(new Whatsit(), WhatsitMBean.class);101mbs.registerMBean(mbean, on3);102mbs.registerMBean(new Broadcaster(), on4);103mbs.createMBean(Broadcaster.class.getName(), on5);104MBeanInfo mbi1 = mbs.getMBeanInfo(on1);105MBeanInfo mbi2 = mbs.getMBeanInfo(on2);106MBeanInfo mbi3 = mbs.getMBeanInfo(on3);107MBeanInfo mbi4 = mbs.getMBeanInfo(on4);108MBeanInfo mbi5 = mbs.getMBeanInfo(on5);109110if (mbi1 != mbi2) {111fail("Two MBeans of the same class should have identical " +112"MBeanInfo");113}114115if (mbi2 != mbi3) {116if (true)117System.out.println("IGNORING StandardMBean(...) failure");118else119fail("Plain Standard MBean should have identical MBeanInfo " +120"to StandardMBean(...) with same class as resource");121}122123if (mbi4 == mbi5 || mbi4.equals(mbi5)) {124fail("Two MBeans of the same class should NOT have identical " +125"MBeanInfo if they are NotificationBroadcasters and "+126"do not return the same MBeanNotificationInfo[]");127}128129if (failure == null) {130System.out.println("Test passed");131return;132}133134throw new Exception("TEST FAILED: " + failure);135}136137private static void fail(String why) {138System.out.println("FAILURE: " + why);139failure = why;140}141}142143144