Path: blob/master/test/jdk/javax/management/MBeanServer/PreRegisterTest.java
51504 views
/*1* Copyright (c) 2003, 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 491184626* @summary Test that MBeanRegistration can change caller ObjectName27* @author Eamonn McManus28*29* @run clean PreRegisterTest30* @run build PreRegisterTest31* @run main PreRegisterTest32*/3334/* Check that an ObjectName returned by MBeanRegistration.preRegister is35the one used, even if createMBean had a different non-null ObjectName. */3637import java.util.Set;38import javax.management.*;3940public class PreRegisterTest {41static final ObjectName oldName, newName;4243static {44try {45oldName = new ObjectName("a:type=old");46newName = new ObjectName("a:type=new");47} catch (MalformedObjectNameException e) {48e.printStackTrace();49throw new Error();50}51}5253public static class X implements XMBean, MBeanRegistration {54public ObjectName preRegister(MBeanServer mbs, ObjectName name) {55return newName;56}57public void postRegister(Boolean done) {}58public void preDeregister() {}59public void postDeregister() {}60}6162public static interface XMBean {63}6465public static void main(String[] args) throws Exception {66System.out.println("Testing preRegister ObjectName substitution");67MBeanServer mbs = MBeanServerFactory.newMBeanServer();68mbs.createMBean(X.class.getName(), oldName);69Set names = mbs.queryNames(null, null);70System.out.println("MBean names after createMBean: " + names);71boolean ok = true;72if (names.contains(oldName)) {73ok = false;74System.out.println("TEST FAILS: previous name was used");75}76if (!names.contains(newName)) {77ok = false;78System.out.println("TEST FAILS: substitute name was not used");79}8081if (ok) {82System.out.println("Test passes: ObjectName correctly " +83"substituted");84} else {85System.out.println("TEST FAILS: ObjectName not correctly " +86"substituted");87System.exit(1);88}89}90}919293