jdk/test/javax/management/namespace/NullObjectNameTest.java
changeset 1156 bbc2d15aaf7a
child 1227 4546977d0d66
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 NullObjectNameTest.java
       
    25  * @summary Test that null ObjectName are correctly handled in namespaces.
       
    26  * @author Daniel Fuchs
       
    27  * @run clean NullObjectNameTest Wombat WombatMBean
       
    28  * @compile -XDignore.symbol.file=true  NullObjectNameTest.java
       
    29  * @run build NullObjectNameTest Wombat WombatMBean
       
    30  * @run main NullObjectNameTest
       
    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 NullObjectNameTest
       
    52  * @author Sun Microsystems, 2005 - All rights reserved.
       
    53  */
       
    54 public class NullObjectNameTest {
       
    55 
       
    56     /**
       
    57      * A logger for this class.
       
    58      **/
       
    59     private static final Logger LOG =
       
    60             Logger.getLogger(NullObjectNameTest.class.getName());
       
    61 
       
    62     /** Creates a new instance of NullObjectNameTest */
       
    63     public NullObjectNameTest() {
       
    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(),null);
       
   101             top.registerMBean(rmiHandler,JMXNamespaces.getNamespaceObjectName("rmi"));
       
   102             top.invoke(JMXNamespaces.getNamespaceObjectName("rmi"),
       
   103                     "connect", null, null);
       
   104 
       
   105             // Create a namespace direct// that points to 'sub' and flows
       
   106             // through a direct reference to 'sub'.
       
   107             // The namespace direct// will accept createMBean, and registerMBean.
       
   108             //
       
   109             final JMXNamespace directHandler = new JMXNamespace(sub);
       
   110             top.registerMBean(directHandler,
       
   111                     JMXNamespaces.getNamespaceObjectName("direct"));
       
   112 
       
   113             // Now cd to each of the created namespace.
       
   114             //
       
   115             MBeanServer cdrmi = JMXNamespaces.narrowToNamespace(top,"rmi");
       
   116             MBeanServer cddirect = JMXNamespaces.narrowToNamespace(top,"direct");
       
   117             boolean ok = false;
       
   118 
       
   119             // Check that calling createMBean with a null ObjectName fails
       
   120             // gracefully for namespace rmi// (we can't add rmi// to a null
       
   121             // ObjectName.
       
   122             //
       
   123             // TODO: do this test for all createMBean flavors!
       
   124             try {
       
   125                 final ObjectInstance moi1 =
       
   126                         cdrmi.createMBean(MyWombat.class.getName(),null);
       
   127                 System.out.println(moi1.getObjectName().toString()+
       
   128                         ": created through rmi//");
       
   129                 cddirect.unregisterMBean(moi1.getObjectName());
       
   130                 failed("expected MBeanRegistrationException");
       
   131             } catch (MBeanRegistrationException x) {
       
   132                 System.out.println("Received expected exception: " + x);
       
   133                 if (!(x.getCause() instanceof IllegalArgumentException)) {
       
   134                     System.err.println("Bad wrapped exception: "+ x.getCause());
       
   135                     failed("expected IllegalArgumentException");
       
   136                 }
       
   137             }
       
   138 
       
   139             // Check that calling refgisterMBean with a null ObjectName fails
       
   140             // gracefully for namespace direct// (we can't add direct// to a null
       
   141             // ObjectName.
       
   142             //
       
   143             try {
       
   144                 final ObjectInstance moi2 =
       
   145                         cddirect.registerMBean(new MyWombat(), (ObjectName)null);
       
   146                 System.out.println(moi2.getObjectName().toString()+
       
   147                         ": created through direct//");
       
   148                 cdrmi.unregisterMBean(moi2.getObjectName());
       
   149                 failed("expected MBeanRegistrationException");
       
   150             } catch (MBeanRegistrationException x) {
       
   151                 System.out.println("Received expected exception: " + x);
       
   152                 if (!(x.getCause() instanceof IllegalArgumentException)) {
       
   153                     System.err.println("Bad wrapped exception: "+ x.getCause());
       
   154                     failed("expected IllegalArgumentException");
       
   155                 }
       
   156             }
       
   157 
       
   158             // Now artificially pretend that 'sub' is contained in a faked//
       
   159             // namespace.
       
   160             // We should be able to use 'null' in registerMBean/createMBean in
       
   161             // this case.
       
   162             //
       
   163             RoutingServerProxy proxy =
       
   164                     new RoutingServerProxy(sub,"","faked",false);
       
   165             final ObjectInstance moi3 =
       
   166                     proxy.registerMBean(new MyWombat(),null);
       
   167             System.out.println(moi3.getObjectName().toString()+
       
   168                     ": created through faked//");
       
   169 
       
   170             // Now check that null is correctly handled (accepted or rejected)
       
   171             // in queries for each of the above configs.
       
   172             //
       
   173             ObjectName wombat = moi3.getObjectName().withDomain(
       
   174                     moi3.getObjectName().getDomain().substring("faked//".length()));
       
   175             ObjectInstance moi = new ObjectInstance(wombat,moi3.getClassName());
       
   176 
       
   177             System.out.println("Checking queryNames(null,null) with rmi//");
       
   178             assertEquals(cdrmi.queryNames(null,null).contains(wombat),true);
       
   179             System.out.println("Checking queryNames(null,null) with direct//");
       
   180             assertEquals(cddirect.queryNames(null,null).contains(wombat),true);
       
   181             System.out.println("Checking queryMBeans(null,null) with rmi//");
       
   182             assertEquals(cdrmi.queryMBeans(null,null).contains(moi),true);
       
   183             System.out.println("Checking queryMBeans(null,null) with direct//");
       
   184             assertEquals(cddirect.queryMBeans(null,null).contains(moi),true);
       
   185 
       
   186             try {
       
   187                 System.out.println("Checking queryNames(null,null) with faked//");
       
   188                 assertEquals(proxy.queryNames(null,null).
       
   189                         contains(moi3.getObjectName()),true);
       
   190                 failed("queryNames(null,null) should have failed for faked//");
       
   191             } catch (IllegalArgumentException x) {
       
   192                 System.out.println("Received expected exception for faked//: "+x);
       
   193             }
       
   194             try {
       
   195                 System.out.println("Checking queryMBeans(null,null) with faked//");
       
   196                 assertEquals(proxy.queryMBeans(null,null).contains(moi3),true);
       
   197                 failed("queryMBeans(null,null) should have failed for faked//");
       
   198             } catch (IllegalArgumentException x) {
       
   199                 System.out.println("Received expected exception for faked//: "+x);
       
   200             }
       
   201             System.out.println("Checking queryNames(faked//*:*,null)");
       
   202             assertEquals(proxy.queryNames(new ObjectName("faked//*:*"),null).
       
   203                     contains(moi3.getObjectName()),true);
       
   204 
       
   205             System.out.println("Checking queryMBeans(faked//*:*,null)");
       
   206             assertEquals(proxy.queryMBeans(new ObjectName("faked//*:*"),null).
       
   207                     contains(moi3),true);
       
   208 
       
   209             proxy.unregisterMBean(moi3.getObjectName());
       
   210 
       
   211             // ADD NEW TESTS HERE ^^^
       
   212 
       
   213         } finally {
       
   214             srv.stop();
       
   215         }
       
   216 
       
   217         if (failure != null)
       
   218             throw new Exception(failure);
       
   219 
       
   220 
       
   221     }
       
   222     private static void assertEquals(Object x, Object y) {
       
   223         if (!equal(x, y))
       
   224             failed("expected " + string(x) + "; got " + string(y));
       
   225     }
       
   226 
       
   227     private static boolean equal(Object x, Object y) {
       
   228         if (x == y)
       
   229             return true;
       
   230         if (x == null || y == null)
       
   231             return false;
       
   232         if (x.getClass().isArray())
       
   233             return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
       
   234         return x.equals(y);
       
   235     }
       
   236 
       
   237     private static String string(Object x) {
       
   238         String s = Arrays.deepToString(new Object[] {x});
       
   239         return s.substring(1, s.length() - 1);
       
   240     }
       
   241 
       
   242 
       
   243     private static void failed(String why) {
       
   244         failure = why;
       
   245         new Throwable("FAILED: " + why).printStackTrace(System.out);
       
   246     }
       
   247 
       
   248     public static void main(String[] args) throws Exception {
       
   249         testRegister();
       
   250     }
       
   251 }