jdk/test/javax/management/namespace/ExportNamespaceTest.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2008 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 ExportNamespaceTest.java
       
    26  * @summary Test that you can export a single namespace through a
       
    27  *          JMXConnectorServer.
       
    28  * @author Daniel Fuchs
       
    29  * @bug 5072476
       
    30  * @run clean ExportNamespaceTest Wombat WombatMBean
       
    31  * @run build ExportNamespaceTest Wombat WombatMBean
       
    32  * @run main ExportNamespaceTest
       
    33  */
       
    34 
       
    35 import javax.management.JMX;
       
    36 import javax.management.ObjectName;
       
    37 import javax.management.namespace.JMXNamespace;
       
    38 import javax.management.namespace.JMXNamespaces;
       
    39 import javax.management.MBeanServer;
       
    40 import javax.management.MBeanServerConnection;
       
    41 import javax.management.MBeanServerFactory;
       
    42 import javax.management.remote.JMXConnector;
       
    43 import javax.management.remote.JMXConnectorFactory;
       
    44 import javax.management.remote.JMXConnectorServer;
       
    45 import javax.management.remote.JMXConnectorServerFactory;
       
    46 import javax.management.remote.JMXServiceURL;
       
    47 
       
    48 
       
    49 /**
       
    50  * Test simple creation/registration of namespace.
       
    51  *
       
    52  */
       
    53 public class ExportNamespaceTest {
       
    54 
       
    55     public static void testExport() throws Exception {
       
    56         final JMXNamespace my =
       
    57                 new JMXNamespace(MBeanServerFactory.newMBeanServer());
       
    58         final MBeanServer s = MBeanServerFactory.newMBeanServer();
       
    59         final ObjectName myname = JMXNamespaces.getNamespaceObjectName("my");
       
    60         final ObjectName wname = ObjectName.getInstance("backyard:type=Wombat");
       
    61         my.getSourceServer().registerMBean(new Wombat(),wname);
       
    62         s.registerMBean(my,myname);
       
    63 
       
    64         if (!s.queryNames(new ObjectName("my//b*:*"),null).contains(
       
    65                 JMXNamespaces.insertPath("my", wname))) {
       
    66             throw new RuntimeException("1: Wombat not found: "+wname);
       
    67         }
       
    68 
       
    69         final MBeanServer cd = JMXNamespaces.narrowToNamespace(s, "my");
       
    70         if (!cd.queryNames(new ObjectName("b*:*"),null).contains(wname)) {
       
    71             throw new RuntimeException("2: Wombat not found: "+wname);
       
    72         }
       
    73 
       
    74         final JMXServiceURL url = new JMXServiceURL("rmi",null,0);
       
    75         final JMXConnectorServer server =
       
    76                 JMXConnectorServerFactory.newJMXConnectorServer(url, null, cd);
       
    77         server.start();
       
    78 
       
    79         final JMXConnector jc = JMXConnectorFactory.
       
    80                 connect(server.getAddress(),null);
       
    81         final MBeanServerConnection mbsc = jc.getMBeanServerConnection();
       
    82 
       
    83         if (!mbsc.queryNames(new ObjectName("b*:*"),null).contains(wname)) {
       
    84             throw new RuntimeException("3: Wombat not found: "+wname);
       
    85         }
       
    86         System.out.println("Found a Wombat in my backyard.");
       
    87 
       
    88         final String deepThoughts = "I want to leave this backyard!";
       
    89         final WombatMBean w = JMX.newMBeanProxy(mbsc, wname, WombatMBean.class);
       
    90         w.setCaption(deepThoughts);
       
    91         if (!deepThoughts.equals(w.getCaption()))
       
    92                 throw new RuntimeException("4: Wombat is not thinking right: "+
       
    93                         w.getCaption());
       
    94 
       
    95     }
       
    96 
       
    97     public static void main(String... args) throws Exception {
       
    98         testExport();
       
    99     }
       
   100 }