Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/PreRegisterTest.java
38839 views
/*1* Copyright (c) 2003, 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* @run clean PreRegisterTest29* @run build PreRegisterTest30* @run main PreRegisterTest31*/3233/* Check that an ObjectName returned by MBeanRegistration.preRegister is34the one used, even if createMBean had a different non-null ObjectName. */3536import java.util.Set;37import javax.management.*;3839public class PreRegisterTest {40static final ObjectName oldName, newName;4142static {43try {44oldName = new ObjectName("a:type=old");45newName = new ObjectName("a:type=new");46} catch (MalformedObjectNameException e) {47e.printStackTrace();48throw new Error();49}50}5152public static class X implements XMBean, MBeanRegistration {53public ObjectName preRegister(MBeanServer mbs, ObjectName name) {54return newName;55}56public void postRegister(Boolean done) {}57public void preDeregister() {}58public void postDeregister() {}59}6061public static interface XMBean {62}6364public static void main(String[] args) throws Exception {65System.out.println("Testing preRegister ObjectName substitution");66MBeanServer mbs = MBeanServerFactory.newMBeanServer();67mbs.createMBean(X.class.getName(), oldName);68Set names = mbs.queryNames(null, null);69System.out.println("MBean names after createMBean: " + names);70boolean ok = true;71if (names.contains(oldName)) {72ok = false;73System.out.println("TEST FAILS: previous name was used");74}75if (!names.contains(newName)) {76ok = false;77System.out.println("TEST FAILS: substitute name was not used");78}7980if (ok) {81System.out.println("Test passes: ObjectName correctly " +82"substituted");83} else {84System.out.println("TEST FAILS: ObjectName not correctly " +85"substituted");86System.exit(1);87}88}89}909192