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