Path: blob/master/test/jdk/javax/management/mxbean/PreRegisterNameTest.java
51442 views
/*1* Copyright (c) 2006, 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 644804226* @summary Test that MXBeans can define their own names in preRegister27* @author Eamonn McManus28*/2930import java.lang.management.ManagementFactory;31import java.lang.reflect.Constructor;32import javax.management.MBeanRegistration;33import javax.management.MBeanServer;34import javax.management.ObjectInstance;35import javax.management.ObjectName;36import javax.management.StandardMBean;3738/*39* Test that an MXBean can decide its name by returning a value from40* the preRegister method. Also test the same thing for Standard MBeans41* for good measure.42*/43public class PreRegisterNameTest {44public static interface SpumeMXBean {}4546public static class Spume implements SpumeMXBean, MBeanRegistration {47private ObjectName realName;4849public Spume(ObjectName realName) {50this.realName = realName;51}5253public void preDeregister() throws Exception {54}5556public void postDeregister() {57}5859public void postRegister(Boolean registrationDone) {60}6162public ObjectName preRegister(MBeanServer server, ObjectName name) {63return realName;64}65}6667public static interface ThingMBean {68public boolean getNoddy();69}7071public static class Thing implements ThingMBean, MBeanRegistration {72private ObjectName realName;7374public Thing(ObjectName realName) {75this.realName = realName;76}7778public ObjectName preRegister(MBeanServer mbs, ObjectName name) {79return realName;80}8182public void postRegister(Boolean done) {}8384public void preDeregister() {}8586public void postDeregister() {}8788public boolean getNoddy() {89return true;90}91}9293public static class XThing extends StandardMBean implements ThingMBean {94private ObjectName realName;9596public XThing(ObjectName realName) {97super(ThingMBean.class, false);98this.realName = realName;99}100101@Override102public ObjectName preRegister(MBeanServer server, ObjectName name) {103return realName;104}105106public boolean getNoddy() {107return false;108}109}110111public static class XSpume extends StandardMBean implements SpumeMXBean {112private ObjectName realName;113114public XSpume(ObjectName realName) {115super(SpumeMXBean.class, true);116this.realName = realName;117}118119@Override120public ObjectName preRegister(MBeanServer server, ObjectName name)121throws Exception {122super.preRegister(server, realName);123return realName;124}125}126127public static void main(String[] args) throws Exception {128MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();129for (Class<?> c : new Class<?>[] {130Spume.class, Thing.class, XSpume.class, XThing.class131}) {132for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {133System.out.println("Class " + c.getName() + " with name " + n +134"...");135ObjectName realName = new ObjectName("a:type=" + c.getName());136Constructor<?> constr = c.getConstructor(ObjectName.class);137Object mbean = constr.newInstance(realName);138ObjectInstance oi;139String what =140"Registering MBean of type " + c.getName() + " under name " +141"<" + n + ">: ";142try {143oi = mbs.registerMBean(mbean, n);144} catch (Exception e) {145e.printStackTrace();146fail(what + " got " + e);147continue;148}149ObjectName registeredName = oi.getObjectName();150if (!registeredName.equals(realName))151fail(what + " registered as " + registeredName);152if (!mbs.isRegistered(realName)) {153fail(what + " not registered as expected");154}155mbs.unregisterMBean(registeredName);156}157}158System.err.flush();159if (failures == 0)160System.out.println("TEST PASSED");161else162throw new Exception("TEST FAILED: " + failure);163}164165private static void fail(String msg) {166System.err.println("FAILED: " + msg);167failure = msg;168failures++;169}170171private static int failures;172private static String failure;173}174175176