Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MXBean/MXBeanBehavior.java
38828 views
/*1* Copyright (c) 2005, 2013, 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 632021126* @summary Check that java.lang.management MXBeans have the same behavior27* as user MXBeans28* @author Eamonn McManus29* @run main/othervm MXBeanBehavior30*/3132import java.lang.management.*;33import java.lang.reflect.*;34import java.util.*;35import javax.management.*;3637public class MXBeanBehavior {38// Exclude list: list of platform MBeans that are not MXBeans39public static final HashSet<String> excludeList = new HashSet<>(40Arrays.asList("com.sun.management:type=DiagnosticCommand"));4142public static void main(String[] args) throws Exception {43MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();4445/* Test that all the MBeans in the java.* and com.sun.management*46domains are MXBeans with the appropriate behavior. */47Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),48null);49names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),50null));51for (ObjectName name : names)52test(mbs, name);5354/* Now do some rudimentary testing of inter-MXBean references.55It should be possible for a user MXBean to return e.g. the56CompilationMXBean from the platform from an attribute of57type CompilationMXBean, and have the MXBean infrastructure58map this into that MXBean's standard ObjectName. It should59also be possible for a proxy for this user MXBean to have60this attribute's value mapped back into a CompilationMXBean61instance, which however will be another proxy rather than62the original object. Finally, it should be possible to set63the attribute in the user's MXBean through a proxy, giving64the real CompilationMXBean as an argument, and have this be65translated into that MXBean's standard ObjectName. The66user's MXBean will receive a proxy in this case, though we67don't check that. */68ObjectName refName = new ObjectName("d:type=CompilationRef");69mbs.registerMBean(new CompilationImpl(), refName);70CompilationRefMXBean refProxy =71JMX.newMXBeanProxy(mbs, refName, CompilationRefMXBean.class);72refProxy.getCompilationMXBean();73refProxy.setCompilationMXBean(ManagementFactory.getCompilationMXBean());74ObjectName on =75(ObjectName) mbs.getAttribute(refName, "CompilationMXBean");76checkEqual(on, new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME),77"Referenced object name");78mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));7980System.out.println("TEST PASSED");81}8283/* Check the behavior of this MXBean to ensure that it conforms to84what is expected of all MXBeans as detailed in85javax.management.MXBean. Its MBeanInfo should have a86Descriptor with the fields mxbean and interfaceClassName, and87furthermore we know that our implementation sets immutableInfo88here. Each attribute should have Descriptor with the fields89openType and originalType that have appropriate values. We90don't currently check operations though the same considerations91would apply there. (If the MBeanInfo and MBeanAttributeInfo92tests pass we can reasonably suppose that this MXBean will93behave the same as all other MXBeans, so MBeanOperationInfo,94MBeanNotificationInfo, and MBeanConstructorInfo will be covered95by generic MXBean tests.96*/97private static void test(MBeanServer mbs, ObjectName name) throws Exception {98if(excludeList.contains(name.getCanonicalName())) {99// Skipping not MXBean objects.100return;101}102System.out.println("Testing: " + name);103104MBeanInfo mbi = mbs.getMBeanInfo(name);105Descriptor mbid = mbi.getDescriptor();106Object[] values = mbid.getFieldValues("immutableInfo",107"interfaceClassName",108"mxbean");109checkEqual(values[0], "true", name + " immutableInfo field");110checkEqual(values[2], "true", name + " mxbean field");111String interfaceClassName = (String) values[1];112if (!mbs.isInstanceOf(name, interfaceClassName)) {113throw new RuntimeException(name + " not instance of " +114interfaceClassName);115}116Class interfaceClass = Class.forName(interfaceClassName);117for (MBeanAttributeInfo mbai : mbi.getAttributes()) {118Descriptor mbaid = mbai.getDescriptor();119Object[] avalues = mbaid.getFieldValues("openType",120"originalType");121if (avalues[0] == null || avalues[1] == null) {122throw new RuntimeException("Null attribute descriptor fields: " +123Arrays.toString(avalues));124}125if (mbai.isReadable()) {126String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();127Method m = interfaceClass.getMethod(mname);128Type t = m.getGenericReturnType();129String ret =130(t instanceof Class) ? ((Class) t).getName() : t.toString();131if (!ret.equals(avalues[1])) {132final String msg =133name + " attribute " + mbai.getName() + " has wrong " +134"originalType: " + avalues[1] + " vs " + ret;135throw new RuntimeException(msg);136}137}138}139}140141private static void checkEqual(Object x, Object y, String what) {142final boolean eq;143if (x == y)144eq = true;145else if (x == null)146eq = false;147else148eq = x.equals(y);149if (!eq)150throw new RuntimeException(what + " should be " + y + ", is " + x);151}152153public static interface CompilationRefMXBean {154public CompilationMXBean getCompilationMXBean();155public void setCompilationMXBean(CompilationMXBean mxb);156}157158public static class CompilationImpl implements CompilationRefMXBean {159public CompilationMXBean getCompilationMXBean() {160return ManagementFactory.getCompilationMXBean();161}162163public void setCompilationMXBean(CompilationMXBean mxb) {164if (mxb == ManagementFactory.getCompilationMXBean())165return;166MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)167Proxy.getInvocationHandler(mxb);168ObjectName expectedName;169try {170expectedName =171new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);172} catch (MalformedObjectNameException e) {173throw new RuntimeException(e);174}175checkEqual(mbsih.getObjectName(), expectedName,176"Proxy name in setCompilationMXBean");177}178}179}180181182