jdk/test/javax/management/MBeanServer/MBeanTest.java
changeset 18805 b359f8adc8ad
child 30376 2ccf2cf7ea48
equal deleted inserted replaced
18804:8561f2098727 18805:b359f8adc8ad
       
     1 /*
       
     2  * Copyright (c) 2013, 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 import javax.management.MBeanServer;
       
    25 import javax.management.MBeanServerFactory;
       
    26 import javax.management.NotCompliantMBeanException;
       
    27 import javax.management.ObjectName;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug 8010285
       
    32  * @summary General MBean test.
       
    33  * @author Jaroslav Bachorik
       
    34  * @run clean MBeanTest
       
    35  * @run build MBeanTest
       
    36  * @run main MBeanTest
       
    37  */
       
    38 public class MBeanTest {
       
    39     private static interface PrivateMBean {
       
    40         public int[] getInts();
       
    41     }
       
    42 
       
    43     public static class Private implements PrivateMBean {
       
    44         public int[] getInts() {
       
    45             return new int[]{1,2,3};
       
    46         }
       
    47     }
       
    48 
       
    49     public static interface NonCompliantMBean {
       
    50         public boolean getInt();
       
    51         public boolean isInt();
       
    52         public void setInt(int a);
       
    53         public void setInt(long b);
       
    54     }
       
    55 
       
    56     public static class NonCompliant implements NonCompliantMBean {
       
    57         public boolean getInt() {
       
    58             return false;
       
    59         }
       
    60 
       
    61         public boolean isInt() {
       
    62             return true;
       
    63         }
       
    64 
       
    65         public void setInt(int a) {
       
    66         }
       
    67 
       
    68         public void setInt(long b) {
       
    69         }
       
    70     }
       
    71 
       
    72     public static interface CompliantMBean {
       
    73         public boolean isFlag();
       
    74         public int getInt();
       
    75         public void setInt(int value);
       
    76     }
       
    77 
       
    78     public static class Compliant implements CompliantMBean {
       
    79         public boolean isFlag() {
       
    80             return false;
       
    81         }
       
    82 
       
    83         public int getInt() {
       
    84             return 1;
       
    85         }
       
    86 
       
    87         public void setInt(int value) {
       
    88         }
       
    89     }
       
    90 
       
    91     private static int failures = 0;
       
    92 
       
    93     public static void main(String[] args) throws Exception {
       
    94         testCompliant(CompliantMBean.class, new Compliant());
       
    95         testNonCompliant(PrivateMBean.class, new Private());
       
    96         testNonCompliant(NonCompliantMBean.class, new NonCompliant());
       
    97 
       
    98         if (failures == 0)
       
    99             System.out.println("Test passed");
       
   100         else
       
   101             throw new Exception("TEST FAILURES: " + failures);
       
   102     }
       
   103 
       
   104     private static void fail(String msg) {
       
   105         failures++;
       
   106         System.out.println("FAIL: " + msg);
       
   107     }
       
   108 
       
   109     private static void success(String msg) {
       
   110         System.out.println("OK: " + msg);
       
   111     }
       
   112 
       
   113     private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {
       
   114         try {
       
   115             System.out.println("Registering a non-compliant MBean " +
       
   116                                 iface.getName() + " ...");
       
   117 
       
   118             MBeanServer mbs = MBeanServerFactory.newMBeanServer();
       
   119             ObjectName on = new ObjectName("test:type=NonCompliant");
       
   120 
       
   121             mbs.registerMBean(bean, on);
       
   122 
       
   123             fail("Registered a non-compliant MBean - " + iface.getName());
       
   124         } catch (Exception e) {
       
   125             Throwable t = e;
       
   126             while (t != null && !(t instanceof NotCompliantMBeanException)) {
       
   127                 t = t.getCause();
       
   128             }
       
   129             if (t != null) {
       
   130                 success("MBean not registered");
       
   131             } else {
       
   132                 throw e;
       
   133             }
       
   134         }
       
   135     }
       
   136     private static void testCompliant(Class<?> iface, Object bean) throws Exception {
       
   137         try {
       
   138             System.out.println("Registering a compliant MBean " +
       
   139                                 iface.getName() + " ...");
       
   140 
       
   141             MBeanServer mbs = MBeanServerFactory.newMBeanServer();
       
   142             ObjectName on = new ObjectName("test:type=Compliant");
       
   143 
       
   144             mbs.registerMBean(bean, on);
       
   145             success("Registered a compliant MBean - " + iface.getName());
       
   146         } catch (Exception e) {
       
   147             Throwable t = e;
       
   148             while (t != null && !(t instanceof NotCompliantMBeanException)) {
       
   149                 t = t.getCause();
       
   150             }
       
   151             if (t != null) {
       
   152                 fail("MBean not registered");
       
   153             } else {
       
   154                 throw e;
       
   155             }
       
   156         }
       
   157     }
       
   158 }