J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6175517 |
| 27 | * @summary Ensure that preRegister etc are called, but not when wrapped |
| 28 | * by the class StandardMBean |
| 29 | * @author Eamonn McManus |
| 30 | * @run clean MXBeanPreRegisterTest |
| 31 | * @run build MXBeanPreRegisterTest |
| 32 | * @run main MXBeanPreRegisterTest |
| 33 | */ |
| 34 | |
| 35 | import javax.management.*; |
| 36 | |
| 37 | public class MXBeanPreRegisterTest { |
| 38 | public static interface EmptyMBean {} |
| 39 | |
| 40 | public static interface EmptyMXBean extends EmptyMBean {} |
| 41 | |
| 42 | public static class Base implements MBeanRegistration { |
| 43 | public ObjectName preRegister(MBeanServer mbs, ObjectName n) { |
| 44 | count++; |
| 45 | return n; |
| 46 | } |
| 47 | |
| 48 | public void postRegister(Boolean done) { |
| 49 | count++; |
| 50 | } |
| 51 | |
| 52 | public void preDeregister() { |
| 53 | count++; |
| 54 | } |
| 55 | |
| 56 | public void postDeregister() { |
| 57 | count++; |
| 58 | } |
| 59 | |
| 60 | int count; |
| 61 | } |
| 62 | |
| 63 | public static class Empty extends Base implements EmptyMBean {} |
| 64 | |
| 65 | public static class EmptyMX extends Base implements EmptyMXBean {} |
| 66 | |
| 67 | public static void main(String[] args) throws Exception { |
| 68 | for (boolean mx : new boolean[] {false, true}) |
| 69 | for (boolean wrapped : new boolean[] {false, true}) |
| 70 | test(mx, wrapped); |
| 71 | if (failure != null) |
| 72 | throw new Exception("TEST FAILED: " + failure); |
| 73 | System.out.println("TEST PASSED"); |
| 74 | } |
| 75 | |
| 76 | private static void test(boolean mx, boolean wrapped) throws Exception { |
| 77 | MBeanServer mbs = MBeanServerFactory.newMBeanServer(); |
| 78 | ObjectName on = new ObjectName("a:b=c"); |
| 79 | Base mbean = mx ? new EmptyMX() : new Empty(); |
| 80 | Object reg = wrapped ? new StandardMBean(mbean, null, mx) : mbean; |
| 81 | mbs.registerMBean(reg, on); |
| 82 | mbs.unregisterMBean(on); |
| 83 | String testDescr = |
| 84 | (mx ? "MXBean" : "Standard MBean") + |
| 85 | (wrapped ? " wrapped in StandardMBean class should not" : |
| 86 | " should") + |
| 87 | " call MBeanRegistration methods"; |
| 88 | boolean ok = mbean.count == (wrapped ? 0 : 4); |
| 89 | if (ok) |
| 90 | System.out.println("OK: " + testDescr); |
| 91 | else { |
| 92 | failure = testDescr; |
| 93 | System.out.println("FAILED: " + failure); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private static String failure; |
| 98 | } |