jdk/test/javax/management/namespace/NullDomainObjectNameTest.java
changeset 1156 bbc2d15aaf7a
child 1570 4165709c91e3
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     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  * @test NullDomainObjectNameTest.java
       
    25  * @summary Test that null domains are correctly handled in namespaces.
       
    26  * @author Daniel Fuchs
       
    27  * @run clean NullDomainObjectNameTest Wombat WombatMBean
       
    28  * @compile -XDignore.symbol.file=true  NullDomainObjectNameTest.java
       
    29  * @run build NullDomainObjectNameTest Wombat WombatMBean
       
    30  * @run main NullDomainObjectNameTest
       
    31  */
       
    32 
       
    33 import com.sun.jmx.namespace.RoutingServerProxy;
       
    34 import java.lang.management.ManagementFactory;
       
    35 import java.util.Arrays;
       
    36 import java.util.logging.Logger;
       
    37 import javax.management.MBeanRegistrationException;
       
    38 import javax.management.MBeanServer;
       
    39 import javax.management.MBeanServerFactory;
       
    40 import javax.management.NotCompliantMBeanException;
       
    41 import javax.management.ObjectInstance;
       
    42 import javax.management.ObjectName;
       
    43 import javax.management.namespace.JMXNamespaces;
       
    44 import javax.management.namespace.JMXRemoteNamespace;
       
    45 import javax.management.namespace.JMXNamespace;
       
    46 import javax.management.remote.JMXConnectorServer;
       
    47 import javax.management.remote.JMXConnectorServerFactory;
       
    48 import javax.management.remote.JMXServiceURL;
       
    49 
       
    50 /**
       
    51  * Class NullDomainObjectNameTest
       
    52  * @author Sun Microsystems, 2005 - All rights reserved.
       
    53  */
       
    54 public class NullDomainObjectNameTest {
       
    55 
       
    56     /**
       
    57      * A logger for this class.
       
    58      **/
       
    59     private static final Logger LOG =
       
    60             Logger.getLogger(NullDomainObjectNameTest.class.getName());
       
    61 
       
    62     /** Creates a new instance of NullDomainObjectNameTest */
       
    63     public NullDomainObjectNameTest() {
       
    64     }
       
    65 
       
    66     public static class MyWombat
       
    67             extends Wombat {
       
    68         public MyWombat() throws NotCompliantMBeanException {
       
    69             super();
       
    70         }
       
    71         @Override
       
    72         public ObjectName preRegister(MBeanServer server, ObjectName name)
       
    73         throws Exception {
       
    74 
       
    75             if (name == null)
       
    76                 name = new ObjectName(":type=Wombat");
       
    77 
       
    78             return super.preRegister(server, name);
       
    79         }
       
    80 
       
    81     }
       
    82 
       
    83     static String failure=null;
       
    84 
       
    85     public static void testRegister() throws Exception {
       
    86         final MBeanServer top = ManagementFactory.getPlatformMBeanServer();
       
    87         final MBeanServer sub = MBeanServerFactory.createMBeanServer();
       
    88         final JMXServiceURL url = new JMXServiceURL("rmi",null,0);
       
    89         final JMXConnectorServer srv =
       
    90                 JMXConnectorServerFactory.newJMXConnectorServer(url,null,sub);
       
    91         srv.start();
       
    92 
       
    93         try {
       
    94 
       
    95             // Create a namespace rmi// that points to 'sub' and flows through
       
    96             // a JMXRemoteNamespace connected to 'srv'
       
    97             // The namespace rmi// will accept createMBean, but not registerMBean.
       
    98             //
       
    99             final JMXRemoteNamespace rmiHandler = JMXRemoteNamespace.
       
   100                     newJMXRemoteNamespace(srv.getAddress(),
       
   101                     null);
       
   102             top.registerMBean(rmiHandler,JMXNamespaces.getNamespaceObjectName("rmi"));
       
   103             top.invoke(JMXNamespaces.getNamespaceObjectName("rmi"),
       
   104                     "connect", null, null);
       
   105 
       
   106             // Create a namespace direct// that points to 'sub' and flows
       
   107             // through a direct reference to 'sub'.
       
   108             // The namespace direct// will accept createMBean, and registerMBean.
       
   109             //
       
   110             final JMXNamespace directHandler = new JMXNamespace(sub);
       
   111             top.registerMBean(directHandler,
       
   112                     JMXNamespaces.getNamespaceObjectName("direct"));
       
   113 
       
   114             // Now cd to each of the created namespace.
       
   115             //
       
   116             MBeanServer cdrmi = JMXNamespaces.narrowToNamespace(top,"rmi");
       
   117             MBeanServer cddirect = JMXNamespaces.narrowToNamespace(top,"direct");
       
   118             boolean ok = false;
       
   119 
       
   120             // Check that calling createMBean with a null domain works
       
   121             // for namespace rmi//
       
   122             //
       
   123             try {
       
   124                 final ObjectInstance moi1 =
       
   125                         cdrmi.createMBean(MyWombat.class.getName(),
       
   126                         new ObjectName(":type=Wombat"));
       
   127                 System.out.println(moi1.getObjectName().toString()+
       
   128                         ": created through rmi//");
       
   129                 assertEquals(moi1.getObjectName().getDomain(),
       
   130                         cddirect.getDefaultDomain());
       
   131                 cddirect.unregisterMBean(moi1.getObjectName());
       
   132             } catch (MBeanRegistrationException x) {
       
   133                 System.out.println("Received unexpected exception: " + x);
       
   134                 failed("Received unexpected exception: " + x);
       
   135             }
       
   136 
       
   137             // Check that calling refgisterMBean with a null domain works
       
   138             // for namespace direct//
       
   139             //
       
   140             try {
       
   141                 final ObjectInstance moi2 =
       
   142                         cddirect.registerMBean(new MyWombat(),
       
   143                         new ObjectName(":type=Wombat"));
       
   144                 System.out.println(moi2.getObjectName().toString()+
       
   145                         ": created through direct//");
       
   146                 assertEquals(moi2.getObjectName().getDomain(),
       
   147                         cdrmi.getDefaultDomain());
       
   148                 cdrmi.unregisterMBean(moi2.getObjectName());
       
   149             } catch (MBeanRegistrationException x) {
       
   150                 System.out.println("Received unexpected exception: " + x);
       
   151                 failed("Received unexpected exception: " + x);
       
   152             }
       
   153 
       
   154             // Now artificially pretend that 'sub' is contained in a faked//
       
   155             // namespace.
       
   156             //
       
   157             RoutingServerProxy proxy =
       
   158                     new RoutingServerProxy(sub, "", "faked", false);
       
   159 
       
   160             // These should fail because the ObjectName doesn't start
       
   161             // with "faked//"
       
   162             try {
       
   163                 final ObjectInstance moi3 =
       
   164                     proxy.registerMBean(new MyWombat(),
       
   165                     new ObjectName(":type=Wombat"));
       
   166                 System.out.println(moi3.getObjectName().toString()+
       
   167                     ": created through faked//");
       
   168                 failed("expected MBeanRegistrationException");
       
   169             } catch (MBeanRegistrationException x) {
       
   170                 System.out.println("Received expected exception: " + x);
       
   171                 if (!(x.getCause() instanceof IllegalArgumentException)) {
       
   172                     System.err.println("Bad wrapped exception: "+ x.getCause());
       
   173                     failed("expected IllegalArgumentException");
       
   174                 }
       
   175             }
       
   176 
       
   177             // null should work with "faked//"
       
   178             final ObjectInstance moi3 =
       
   179                     proxy.registerMBean(new MyWombat(),null);
       
   180             assertEquals(moi3.getObjectName().getDomain(),
       
   181                          "faked//"+sub.getDefaultDomain());
       
   182 
       
   183             System.out.println(moi3.getObjectName().toString() +
       
   184                     ": created through faked//");
       
   185 
       
   186             // Now check that null is correctly handled (accepted or rejected)
       
   187             // in queries for each of the above configs.
       
   188             //
       
   189             ObjectName wombat = moi3.getObjectName().withDomain(
       
   190                     moi3.getObjectName().getDomain().substring("faked//".length()));
       
   191             ObjectInstance moi = new ObjectInstance(wombat,moi3.getClassName());
       
   192 
       
   193             System.out.println("Checking queryNames(" +
       
   194                     "new ObjectName(\":*\"),null) with rmi//");
       
   195             assertEquals(cdrmi.queryNames(
       
   196                     new ObjectName(":*"),null).contains(wombat),true);
       
   197             System.out.println("Checking queryNames(" +
       
   198                     "new ObjectName(\":*\"),null) with direct//");
       
   199             assertEquals(cddirect.queryNames(
       
   200                     new ObjectName(":*"),null).contains(wombat),true);
       
   201             System.out.println("Checking queryMBeans(" +
       
   202                     "new ObjectName(\":*\"),null) with rmi//");
       
   203             assertEquals(cdrmi.queryMBeans(
       
   204                     new ObjectName(":*"),null).contains(moi),true);
       
   205             System.out.println("Checking queryMBeans(" +
       
   206                     "new ObjectName(\":*\"),null) with direct//");
       
   207             assertEquals(cddirect.queryMBeans(
       
   208                     new ObjectName(":*"),null).contains(moi),true);
       
   209 
       
   210             // These should fail because the ObjectName doesn't start
       
   211             // with "faked//"
       
   212             try {
       
   213                 System.out.println("Checking queryNames(" +
       
   214                     "new ObjectName(\":*\"),null) with faked//");
       
   215                 assertEquals(proxy.queryNames(
       
   216                         new ObjectName(":*"),null).
       
   217                         contains(moi3.getObjectName()),true);
       
   218                 failed("queryNames(null,null) should have failed for faked//");
       
   219             } catch (IllegalArgumentException x) {
       
   220                 System.out.println("Received expected exception for faked//: "+x);
       
   221             }
       
   222             // These should fail because the ObjectName doesn't start
       
   223             // with "faked//"
       
   224             try {
       
   225                 System.out.println("Checking queryMBeans(" +
       
   226                     "new ObjectName(\":*\"),null) with faked//");
       
   227                 assertEquals(proxy.queryMBeans(
       
   228                         new ObjectName(":*"),null).contains(moi3),true);
       
   229                 failed("queryMBeans(null,null) should have failed for faked//");
       
   230             } catch (IllegalArgumentException x) {
       
   231                 System.out.println("Received expected exception for faked//: "+x);
       
   232             }
       
   233 
       
   234             System.out.println("Checking queryNames(faked//*:*,null)");
       
   235             assertEquals(proxy.queryNames(new ObjectName("faked//*:*"),null).
       
   236                     contains(moi3.getObjectName()),true);
       
   237 
       
   238             System.out.println("Checking queryMBeans(faked//*:*,null)");
       
   239             assertEquals(proxy.queryMBeans(new ObjectName("faked//*:*"),null).
       
   240                     contains(moi3),true);
       
   241 
       
   242             proxy.unregisterMBean(moi3.getObjectName());
       
   243 
       
   244             // ADD NEW TESTS HERE ^^^
       
   245 
       
   246         } finally {
       
   247             srv.stop();
       
   248         }
       
   249 
       
   250         if (failure != null)
       
   251             throw new Exception(failure);
       
   252 
       
   253 
       
   254     }
       
   255     private static void assertEquals(Object x, Object y) {
       
   256         if (!equal(x, y))
       
   257             failed("expected " + string(x) + "; got " + string(y));
       
   258     }
       
   259 
       
   260     private static boolean equal(Object x, Object y) {
       
   261         if (x == y)
       
   262             return true;
       
   263         if (x == null || y == null)
       
   264             return false;
       
   265         if (x.getClass().isArray())
       
   266             return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
       
   267         return x.equals(y);
       
   268     }
       
   269 
       
   270     private static String string(Object x) {
       
   271         String s = Arrays.deepToString(new Object[] {x});
       
   272         return s.substring(1, s.length() - 1);
       
   273     }
       
   274 
       
   275 
       
   276     private static void failed(String why) {
       
   277         failure = why;
       
   278         new Throwable("FAILED: " + why).printStackTrace(System.out);
       
   279     }
       
   280 
       
   281     public static void main(String[] args) throws Exception {
       
   282         testRegister();
       
   283     }
       
   284 }