1 /*
   2  * Copyright 2004 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     5024531
  27  * @summary Basic Test for ManagementFactory.newPlatformMXBean().
  28  * @author  Mandy Chung
  29  *
  30  * @compile -source 1.5 MXBeanProxyTest.java
  31  * @run main MXBeanProxyTest
  32  */
  33 import javax.management.*;
  34 import java.lang.management.ClassLoadingMXBean;
  35 import java.lang.management.RuntimeMXBean;
  36 import static java.lang.management.ManagementFactory.*;
  37 public class MXBeanProxyTest {
  38     private static MBeanServer server = getPlatformMBeanServer();
  39     public static void main(String[] argv) throws Exception {
  40         boolean iae = false;
  41         try {
  42             // Get a MXBean proxy with invalid name
  43             newPlatformMXBeanProxy(server,
  44                                    "Invalid ObjectName",
  45                                    RuntimeMXBean.class);
  46         } catch (IllegalArgumentException e) {
  47             // Expected exception
  48             System.out.println("EXPECTED: " + e);
  49             iae = true;
  50         }
  51         if (!iae) {
  52             throw new RuntimeException("Invalid ObjectName " +
  53                 " was not detected");
  54         }
  55 
  56         try {
  57             // Get a MXBean proxy with non existent MXBean
  58             newPlatformMXBeanProxy(server,
  59                                    "java.lang:type=Foo",
  60                                    RuntimeMXBean.class);
  61             iae = false;
  62         } catch (IllegalArgumentException e) {
  63             // Expected exception
  64             System.out.println("EXPECTED: " + e);
  65             iae = true;
  66         }
  67         if (!iae) {
  68             throw new RuntimeException("Non existent MXBean " +
  69                 " was not detected");
  70         }
  71 
  72         try {
  73             // Mismatch MXBean interface
  74             newPlatformMXBeanProxy(server,
  75                                    RUNTIME_MXBEAN_NAME,
  76                                    ClassLoadingMXBean.class);
  77             iae = false;
  78         } catch (IllegalArgumentException e) {
  79             // Expected exception
  80             System.out.println("EXPECTED: " + e);
  81             iae = true;
  82         }
  83         if (!iae) {
  84             throw new RuntimeException("Mismatched MXBean interface " +
  85                 " was not detected");
  86         }
  87 
  88         final FooMBean foo = new Foo();
  89         final ObjectName objName = new ObjectName("java.lang:type=Foo");
  90         server.registerMBean(foo, objName);
  91         try {
  92             // non-platform MXBean
  93             newPlatformMXBeanProxy(server,
  94                                    "java.lang:type=Foo",
  95                                    FooMBean.class);
  96             iae = false;
  97         } catch (IllegalArgumentException e) {
  98             // Expected exception
  99             System.out.println("EXPECTED: " + e);
 100             iae = true;
 101         }
 102         if (!iae) {
 103             throw new RuntimeException("Non-platform MXBean " +
 104                 " was not detected");
 105         }
 106 
 107 
 108         // Successfully get MXBean
 109         RuntimeMXBean rm = newPlatformMXBeanProxy(server,
 110                                                   RUNTIME_MXBEAN_NAME,
 111                                                   RuntimeMXBean.class);
 112         System.out.println("VM uptime = " + rm.getUptime());
 113         System.out.println("Test passed.");
 114     }
 115 
 116     public interface FooMBean {
 117         public boolean isFoo();
 118     }
 119     static class Foo implements FooMBean {
 120         public boolean isFoo() {
 121             return true;
 122         }
 123     }
 124 }