Path: blob/master/test/jdk/javax/management/mxbean/MXBeanPreRegisterTest.java
51442 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 617551726* @summary Ensure that preRegister etc are called, but not when wrapped27* by the class StandardMBean28* @author Eamonn McManus29*30* @run clean MXBeanPreRegisterTest31* @run build MXBeanPreRegisterTest32* @run main MXBeanPreRegisterTest33*/3435import javax.management.*;3637public class MXBeanPreRegisterTest {38public static interface EmptyMBean {}3940public static interface EmptyMXBean extends EmptyMBean {}4142public static class Base implements MBeanRegistration {43public ObjectName preRegister(MBeanServer mbs, ObjectName n) {44count++;45return n;46}4748public void postRegister(Boolean done) {49count++;50}5152public void preDeregister() {53count++;54}5556public void postDeregister() {57count++;58}5960int count;61}6263public static class Empty extends Base implements EmptyMBean {}6465public static class EmptyMX extends Base implements EmptyMXBean {}6667public static void main(String[] args) throws Exception {68for (boolean mx : new boolean[] {false, true})69for (boolean wrapped : new boolean[] {false, true})70test(mx, wrapped);71if (failure != null)72throw new Exception("TEST FAILED: " + failure);73System.out.println("TEST PASSED");74}7576private static void test(boolean mx, boolean wrapped) throws Exception {77MBeanServer mbs = MBeanServerFactory.newMBeanServer();78ObjectName on = new ObjectName("a:b=c");79Base mbean = mx ? new EmptyMX() : new Empty();80Object reg = wrapped ? new StandardMBean(mbean, null, mx) : mbean;81mbs.registerMBean(reg, on);82mbs.unregisterMBean(on);83String testDescr =84(mx ? "MXBean" : "Standard MBean") +85(wrapped ? " wrapped in StandardMBean class should not" :86" should") +87" call MBeanRegistration methods";88boolean ok = mbean.count == (wrapped ? 0 : 4);89if (ok)90System.out.println("OK: " + testDescr);91else {92failure = testDescr;93System.out.println("FAILED: " + failure);94}95}9697private static String failure;98}99100101