jdk/test/javax/management/namespace/SourceNamespaceTest.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 SourceNamespaceTest.java
       
    26  * @summary Test how queryNames works with Namespaces.
       
    27  * @bug 5072476
       
    28  * @author Daniel Fuchs
       
    29  * @run clean SourceNamespaceTest Wombat WombatMBean
       
    30  * @run build SourceNamespaceTest Wombat WombatMBean
       
    31  * @run main SourceNamespaceTest
       
    32  */
       
    33 
       
    34 
       
    35 import javax.management.JMException;
       
    36 import javax.management.JMX;
       
    37 import javax.management.MBeanServer;
       
    38 import javax.management.MBeanServerFactory;
       
    39 import javax.management.ObjectName;
       
    40 import javax.management.namespace.JMXNamespace;
       
    41 import javax.management.namespace.JMXNamespaces;
       
    42 
       
    43 /**
       
    44  * A simple test to test the source directory parameter...
       
    45  * @author dfuchs
       
    46  */
       
    47 public class SourceNamespaceTest {
       
    48 
       
    49 
       
    50     public static void localTest() throws JMException {
       
    51         final JMXNamespace adir =
       
    52                 new JMXNamespace(MBeanServerFactory.newMBeanServer());
       
    53 
       
    54         // put a wombat in adir...
       
    55         final Wombat w1 = new Wombat();
       
    56         final ObjectName wn1 = new ObjectName("wilderness:type=Wombat,name=gloups");
       
    57         adir.getSourceServer().registerMBean(w1,wn1);
       
    58 
       
    59         // register adir
       
    60         final MBeanServer server = MBeanServerFactory.newMBeanServer();
       
    61         server.registerMBean(adir, JMXNamespaces.getNamespaceObjectName("adir"));
       
    62 
       
    63         if (! (server.isRegistered(JMXNamespaces.insertPath("adir", wn1))))
       
    64             throw new RuntimeException("Test failed: " +
       
    65                     JMXNamespaces.insertPath("adir", wn1) + " not found");
       
    66 
       
    67         System.out.println("Wombat gloups correctly registered...");
       
    68 
       
    69         // put another wombat in adir...
       
    70         final Wombat w2 = new Wombat();
       
    71         final ObjectName wn2 =
       
    72                 new ObjectName("wilderness:type=Wombat,name=pasgloups");
       
    73         server.registerMBean(w2,JMXNamespaces.insertPath("adir", wn2));
       
    74 
       
    75         if (! (server.isRegistered(JMXNamespaces.insertPath("adir", wn2))))
       
    76             throw new RuntimeException("Test failed: " +
       
    77                     JMXNamespaces.insertPath("adir", wn2) + " not found");
       
    78 
       
    79         System.out.println("Wombat pasgloups correctly registered...");
       
    80 
       
    81 
       
    82         // make an alias
       
    83         final JMXNamespace alias = new JMXNamespace(
       
    84                 JMXNamespaces.narrowToNamespace(server,"adir"));
       
    85         server.registerMBean(alias,
       
    86                 JMXNamespaces.getNamespaceObjectName("alias"));
       
    87 
       
    88         if (! (server.isRegistered(JMXNamespaces.insertPath("alias", wn1))))
       
    89             throw new RuntimeException("Test failed: " +
       
    90                     JMXNamespaces.insertPath("alias", wn1) + " not found");
       
    91 
       
    92         System.out.println("Wombat gloups accessible through alias...");
       
    93 
       
    94         if (! (server.isRegistered(JMXNamespaces.insertPath("alias", wn2))))
       
    95             throw new RuntimeException("Test failed: " +
       
    96                     JMXNamespaces.insertPath("alias", wn2) + " not found");
       
    97 
       
    98         System.out.println("Wombat pasgloups accessible through alias...");
       
    99 
       
   100         final WombatMBean wp2 = JMX.newMBeanProxy(server,
       
   101                 JMXNamespaces.insertPath("alias",wn2), WombatMBean.class);
       
   102         System.out.println(JMXNamespaces.insertPath("alias",wn2).toString()
       
   103                 +" says: "+wp2.getCaption());
       
   104 
       
   105         // We're going to make another alias, but register it in a different
       
   106         // MBeanServer. This is to make sure that source server and target
       
   107         // server are not mixed up.
       
   108         //
       
   109         final MBeanServer server2 = MBeanServerFactory.newMBeanServer();
       
   110         final JMXNamespace alias2 = new JMXNamespace(
       
   111                 JMXNamespaces.narrowToNamespace(server,"adir"));
       
   112         server2.registerMBean(alias2,
       
   113                 JMXNamespaces.getNamespaceObjectName("alias2"));
       
   114 
       
   115 
       
   116         if (! (server2.isRegistered(JMXNamespaces.insertPath("alias2", wn1))))
       
   117             throw new RuntimeException("Test failed: " +
       
   118                     JMXNamespaces.insertPath("alias2", wn1) + " not found");
       
   119 
       
   120         System.out.println("Wombat gloups accessible through alias2...");
       
   121 
       
   122         if (! (server2.isRegistered(JMXNamespaces.insertPath("alias2", wn2))))
       
   123             throw new RuntimeException("Test failed: " +
       
   124                     JMXNamespaces.insertPath("alias2", wn2) + " not found");
       
   125 
       
   126         System.out.println("Wombat pasgloups accessible through alias...");
       
   127 
       
   128         final WombatMBean wp22 = JMX.newMBeanProxy(server2,
       
   129                 JMXNamespaces.insertPath("alias2",wn2), WombatMBean.class);
       
   130         System.out.println(JMXNamespaces.insertPath("alias2",wn2).toString()
       
   131                 +" says: "+wp22.getCaption());
       
   132 
       
   133 
       
   134 
       
   135     }
       
   136 
       
   137     public static void main(String[] args) throws Exception {
       
   138         localTest();
       
   139     }
       
   140 
       
   141 }