Path: blob/master/test/jdk/java/lang/management/MXBean/MXBeanBehavior.java
41155 views
/*1* Copyright (c) 2005, 2016, 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* @modules jdk.management30* @run main/othervm MXBeanBehavior31*/3233import java.lang.management.*;34import java.lang.reflect.*;35import java.util.*;36import javax.management.*;3738public class MXBeanBehavior {39// Exclude list: list of platform MBeans that are not MXBeans40public static final HashSet<String> excludeList = new HashSet<>(41Arrays.asList("com.sun.management:type=DiagnosticCommand"));4243public static void main(String[] args) throws Exception {44MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();4546/* Test that all the MBeans in the java.* and com.sun.management*47domains are MXBeans with the appropriate behavior. */48Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),49null);50names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),51null));52for (ObjectName name : names)53test(mbs, name);5455/* Now do some rudimentary testing of inter-MXBean references.56It should be possible for a user MXBean to return e.g. the57CompilationMXBean from the platform from an attribute of58type CompilationMXBean, and have the MXBean infrastructure59map this into that MXBean's standard ObjectName. It should60also be possible for a proxy for this user MXBean to have61this attribute's value mapped back into a CompilationMXBean62instance, which however will be another proxy rather than63the original object. Finally, it should be possible to set64the attribute in the user's MXBean through a proxy, giving65the real CompilationMXBean as an argument, and have this be66translated into that MXBean's standard ObjectName. The67user's MXBean will receive a proxy in this case, though we68don't check that. */69ObjectName refName = new ObjectName("d:type=CompilationRef");70mbs.registerMBean(new CompilationImpl(), refName);71CompilationRefMXBean refProxy =72JMX.newMXBeanProxy(mbs, refName, CompilationRefMXBean.class);73refProxy.getCompilationMXBean();74refProxy.setCompilationMXBean(ManagementFactory.getCompilationMXBean());75ObjectName on =76(ObjectName) mbs.getAttribute(refName, "CompilationMXBean");77checkEqual(on, new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME),78"Referenced object name");79mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));8081System.out.println("TEST PASSED");82}8384/* Check the behavior of this MXBean to ensure that it conforms to85what is expected of all MXBeans as detailed in86javax.management.MXBean. Its MBeanInfo should have a87Descriptor with the fields mxbean and interfaceClassName, and88furthermore we know that our implementation sets immutableInfo89here. Each attribute should have Descriptor with the fields90openType and originalType that have appropriate values. We91don't currently check operations though the same considerations92would apply there. (If the MBeanInfo and MBeanAttributeInfo93tests pass we can reasonably suppose that this MXBean will94behave the same as all other MXBeans, so MBeanOperationInfo,95MBeanNotificationInfo, and MBeanConstructorInfo will be covered96by generic MXBean tests.97*/98private static void test(MBeanServer mbs, ObjectName name) throws Exception {99if(excludeList.contains(name.getCanonicalName())) {100// Skipping not MXBean objects.101return;102}103System.out.println("Testing: " + name);104105MBeanInfo mbi = mbs.getMBeanInfo(name);106Descriptor mbid = mbi.getDescriptor();107Object[] values = mbid.getFieldValues("immutableInfo",108"interfaceClassName",109"mxbean");110checkEqual(values[0], "true", name + " immutableInfo field");111checkEqual(values[2], "true", name + " mxbean field");112String interfaceClassName = (String) values[1];113if (!mbs.isInstanceOf(name, interfaceClassName)) {114throw new RuntimeException(name + " not instance of " +115interfaceClassName);116}117Class interfaceClass = Class.forName(interfaceClassName);118for (MBeanAttributeInfo mbai : mbi.getAttributes()) {119Descriptor mbaid = mbai.getDescriptor();120Object[] avalues = mbaid.getFieldValues("openType",121"originalType");122if (avalues[0] == null || avalues[1] == null) {123throw new RuntimeException("Null attribute descriptor fields: " +124Arrays.toString(avalues));125}126if (mbai.isReadable()) {127String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();128Method m = interfaceClass.getMethod(mname);129Type t = m.getGenericReturnType();130String ret =131(t instanceof Class) ? ((Class) t).getName() : t.toString();132if (!ret.equals(avalues[1])) {133final String msg =134name + " attribute " + mbai.getName() + " has wrong " +135"originalType: " + avalues[1] + " vs " + ret;136throw new RuntimeException(msg);137}138}139}140}141142private static void checkEqual(Object x, Object y, String what) {143final boolean eq;144if (x == y)145eq = true;146else if (x == null)147eq = false;148else149eq = x.equals(y);150if (!eq)151throw new RuntimeException(what + " should be " + y + ", is " + x);152}153154public static interface CompilationRefMXBean {155public CompilationMXBean getCompilationMXBean();156public void setCompilationMXBean(CompilationMXBean mxb);157}158159public static class CompilationImpl implements CompilationRefMXBean {160public CompilationMXBean getCompilationMXBean() {161return ManagementFactory.getCompilationMXBean();162}163164public void setCompilationMXBean(CompilationMXBean mxb) {165if (mxb == ManagementFactory.getCompilationMXBean())166return;167MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)168Proxy.getInvocationHandler(mxb);169ObjectName expectedName;170try {171expectedName =172new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);173} catch (MalformedObjectNameException e) {174throw new RuntimeException(e);175}176checkEqual(mbsih.getObjectName(), expectedName,177"Proxy name in setCompilationMXBean");178}179}180}181182183