Path: blob/master/test/jdk/javax/management/mxbean/MXBeanInteropTest2.java
41149 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 805886526* @summary Checks access to test MXBean27* @author Olivier Lagneau28* @modules java.management.rmi29* @library /lib/testlibrary30* @compile Basic.java31* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanInteropTest232*/3334import java.util.Iterator;35import java.util.Map;36import java.util.Set;3738import javax.management.Attribute;39import javax.management.JMX;40import javax.management.MBeanAttributeInfo;41import javax.management.MBeanConstructorInfo;42import javax.management.MBeanServer;43import java.lang.management.ManagementFactory;44import javax.management.MBeanInfo;45import javax.management.MBeanNotificationInfo;46import javax.management.MBeanOperationInfo;47import javax.management.MBeanServerConnection;48import javax.management.ObjectName;49import javax.management.remote.JMXConnector;50import javax.management.remote.JMXConnectorFactory;51import javax.management.remote.JMXConnectorServer;52import javax.management.remote.JMXConnectorServerFactory;53import javax.management.remote.JMXServiceURL;5455public class MXBeanInteropTest2 {5657private static String BASIC_MXBEAN_CLASS_NAME = "Basic";5859/*60* First Debug properties and arguments are collect in expected61* map (argName, value) format, then calls original test's run method.62*/63public static void main(String args[]) throws Exception {6465System.out.println("=================================================");6667// Parses parameters68Utils.parseDebugProperties();69Map<String, Object> map = Utils.parseParameters(args) ;7071// Run test72MXBeanInteropTest2 test = new MXBeanInteropTest2();73test.run(map);7475}7677public void run(Map<String, Object> args) {7879System.out.println("MXBeanInteropTest2::run: Start") ;80int errorCount = 0 ;8182try {83// JMX MbeanServer used inside single VM as if remote.84MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();8586JMXServiceURL url = new JMXServiceURL("rmi", null, 0);87JMXConnectorServer cs =88JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);89cs.start();9091JMXServiceURL addr = cs.getAddress();92JMXConnector cc = JMXConnectorFactory.connect(addr);93MBeanServerConnection mbsc = cc.getMBeanServerConnection();9495// Prints all MBeans whatever the domain is.96printMBeans(mbsc) ;9798// Call test body99errorCount += doBasicMXBeanTest(mbsc) ;100101// Terminate the JMX Client102cc.close();103104} catch(Exception e) {105Utils.printThrowable(e, true) ;106throw new RuntimeException(e);107}108109if ( errorCount == 0 ) {110System.out.println("MXBeanInteropTest2::run: Done without any error") ;111} else {112System.out.println("MXBeanInteropTest2::run: Done with "113+ errorCount114+ " error(s)") ;115throw new RuntimeException("errorCount = " + errorCount);116}117}118119120/**121* Prints all MBeans whatever the domain is.122*/123private static void printMBeans(MBeanServerConnection mbsc) throws Exception {124Set<ObjectName> set = mbsc.queryNames(null, null);125System.out.println("---- MBeans found :");126127for (Iterator<ObjectName> iter = set.iterator(); iter.hasNext(); ) {128System.out.println(iter.next().toString());129}130131System.out.println("\n") ;132}133134135private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {136int errorCount = 0 ;137System.out.println("---- doBasicMXBeanTest") ;138139try {140ObjectName objName =141new ObjectName("sqe:type=BasicMXBean") ;142mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);143MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);144printMBeanInfo(mbInfo);145System.out.println("---- OK\n") ;146System.out.println("getMBeanInfo\t\t"147+ mbInfo);148System.out.println("---- OK\n") ;149150System.out.println("Check mxbean field in the MBeanInfo");151String mxbeanField =152(String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);153154if ( mxbeanField == null || ! mxbeanField.equals("true")) {155System.out.println("---- ERROR : Improper mxbean field value "156+ mxbeanField);157errorCount++;158}159System.out.println("---- OK\n") ;160161System.out.println("Set attribute ObjectNameAtt");162Attribute att = new Attribute("ObjectNameAtt", objName);163mbsc.setAttribute(objName, att);164ObjectName value =165(ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");166167if ( ! value.equals(objName) ) {168errorCount++;169System.out.println("---- ERROR : setAttribute failed, got "170+ value171+ " while expecting "172+ objName);173}174System.out.println("---- OK\n") ;175176System.out.println("Call operation doNothing");177mbsc.invoke(objName, "doNothing", null, null);178System.out.println("---- OK\n") ;179180System.out.println("Call operation getWeather");181Object weather = mbsc.invoke(objName,182"getWeather",183new Object[]{Boolean.TRUE},184new String[]{"boolean"});185System.out.println("Weather is " + weather);186System.out.println("---- OK\n") ;187} catch (Exception e) {188Utils.printThrowable(e, true) ;189errorCount++ ;190System.out.println("---- ERROR\n") ;191}192193return errorCount ;194}195196private void printMBeanInfo(MBeanInfo mbInfo) {197System.out.println("Description " + mbInfo.getDescription());198199for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) {200System.out.println("Constructor " + ctor.getName());201}202203for (MBeanAttributeInfo att : mbInfo.getAttributes()) {204System.out.println("Attribute " + att.getName()205+ " [" + att.getType() + "]");206}207208for (MBeanOperationInfo oper : mbInfo.getOperations()) {209System.out.println("Operation " + oper.getName());210}211212for (MBeanNotificationInfo notif : mbInfo.getNotifications()) {213System.out.println("Notification " + notif.getName());214}215}216}217218219