1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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  * @modules java.management
  31  * @run clean MXBeanPreRegisterTest
  32  * @run build MXBeanPreRegisterTest
  33  * @run main MXBeanPreRegisterTest
  34  */
  35 
  36 import javax.management.*;
  37 
  38 public class MXBeanPreRegisterTest {
  39     public static interface EmptyMBean {}
  40 
  41     public static interface EmptyMXBean extends EmptyMBean {}
  42 
  43     public static class Base implements MBeanRegistration {
  44         public ObjectName preRegister(MBeanServer mbs, ObjectName n) {
  45             count++;
  46             return n;
  47         }
  48 
  49         public void postRegister(Boolean done) {
  50             count++;
  51         }
  52 
  53         public void preDeregister() {
  54             count++;
  55         }
  56 
  57         public void postDeregister() {
  58             count++;
  59         }
  60 
  61         int count;
  62     }
  63 
  64     public static class Empty extends Base implements EmptyMBean {}
  65 
  66     public static class EmptyMX extends Base implements EmptyMXBean {}
  67 
  68     public static void main(String[] args) throws Exception {
  69         for (boolean mx : new boolean[] {false, true})
  70             for (boolean wrapped : new boolean[] {false, true})
  71                 test(mx, wrapped);
  72         if (failure != null)
  73             throw new Exception("TEST FAILED: " + failure);
  74         System.out.println("TEST PASSED");
  75     }
  76 
  77     private static void test(boolean mx, boolean wrapped) throws Exception {
  78         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  79         ObjectName on = new ObjectName("a:b=c");
  80         Base mbean = mx ? new EmptyMX() : new Empty();
  81         Object reg = wrapped ? new StandardMBean(mbean, null, mx) : mbean;
  82         mbs.registerMBean(reg, on);
  83         mbs.unregisterMBean(on);
  84         String testDescr =
  85             (mx ? "MXBean" : "Standard MBean") +
  86             (wrapped ? " wrapped in StandardMBean class should not" :
  87              " should") +
  88             " call MBeanRegistration methods";
  89         boolean ok = mbean.count == (wrapped ? 0 : 4);
  90         if (ok)
  91             System.out.println("OK: " + testDescr);
  92         else {
  93             failure = testDescr;
  94             System.out.println("FAILED: " + failure);
  95         }
  96     }
  97 
  98     private static String failure;
  99 }