Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/mxbean/MXBeanPreRegisterTest.java
38841 views
/*1* Copyright (c) 2005, 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 617551726* @summary Ensure that preRegister etc are called, but not when wrapped27* by the class StandardMBean28* @author Eamonn McManus29* @run clean MXBeanPreRegisterTest30* @run build MXBeanPreRegisterTest31* @run main MXBeanPreRegisterTest32*/3334import javax.management.*;3536public class MXBeanPreRegisterTest {37public static interface EmptyMBean {}3839public static interface EmptyMXBean extends EmptyMBean {}4041public static class Base implements MBeanRegistration {42public ObjectName preRegister(MBeanServer mbs, ObjectName n) {43count++;44return n;45}4647public void postRegister(Boolean done) {48count++;49}5051public void preDeregister() {52count++;53}5455public void postDeregister() {56count++;57}5859int count;60}6162public static class Empty extends Base implements EmptyMBean {}6364public static class EmptyMX extends Base implements EmptyMXBean {}6566public static void main(String[] args) throws Exception {67for (boolean mx : new boolean[] {false, true})68for (boolean wrapped : new boolean[] {false, true})69test(mx, wrapped);70if (failure != null)71throw new Exception("TEST FAILED: " + failure);72System.out.println("TEST PASSED");73}7475private static void test(boolean mx, boolean wrapped) throws Exception {76MBeanServer mbs = MBeanServerFactory.newMBeanServer();77ObjectName on = new ObjectName("a:b=c");78Base mbean = mx ? new EmptyMX() : new Empty();79Object reg = wrapped ? new StandardMBean(mbean, null, mx) : mbean;80mbs.registerMBean(reg, on);81mbs.unregisterMBean(on);82String testDescr =83(mx ? "MXBean" : "Standard MBean") +84(wrapped ? " wrapped in StandardMBean class should not" :85" should") +86" call MBeanRegistration methods";87boolean ok = mbean.count == (wrapped ? 0 : 4);88if (ok)89System.out.println("OK: " + testDescr);90else {91failure = testDescr;92System.out.println("FAILED: " + failure);93}94}9596private static String failure;97}9899100