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