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