jdk/test/javax/management/namespace/NullObjectNameTest.java
author emcmanus
Fri, 07 Nov 2008 11:48:07 +0100
changeset 1570 4165709c91e3
parent 1227 4546977d0d66
child 1709 392dd6db361a
permissions -rw-r--r--
5072267: A way to communicate client context such as locale to the JMX server Summary: Support for client contexts and also for localization of descriptions Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     1
/*
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     4
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     8
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    14
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    18
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    21
 * have any questions.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    22
 */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    23
/*
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    24
 * @test NullObjectNameTest.java
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    25
 * @summary Test that null ObjectName are correctly handled in namespaces.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    26
 * @author Daniel Fuchs
1227
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1156
diff changeset
    27
 * @bug 5072476
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    28
 * @run clean NullObjectNameTest Wombat WombatMBean
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    29
 * @compile -XDignore.symbol.file=true  NullObjectNameTest.java
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    30
 * @run build NullObjectNameTest Wombat WombatMBean
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    31
 * @run main NullObjectNameTest
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    32
 */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    33
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    34
import com.sun.jmx.namespace.RoutingServerProxy;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    35
import java.lang.management.ManagementFactory;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    36
import java.util.Arrays;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    37
import java.util.logging.Logger;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    38
import javax.management.MBeanRegistrationException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    39
import javax.management.MBeanServer;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    41
import javax.management.NotCompliantMBeanException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    42
import javax.management.ObjectInstance;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    43
import javax.management.ObjectName;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    44
import javax.management.namespace.JMXNamespaces;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    45
import javax.management.namespace.JMXRemoteNamespace;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    46
import javax.management.namespace.JMXNamespace;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorServer;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorServerFactory;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    49
import javax.management.remote.JMXServiceURL;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    50
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    51
/**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    52
 * Class NullObjectNameTest
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    53
 * @author Sun Microsystems, 2005 - All rights reserved.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    54
 */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    55
public class NullObjectNameTest {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    56
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    57
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    58
     * A logger for this class.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    59
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    60
    private static final Logger LOG =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    61
            Logger.getLogger(NullObjectNameTest.class.getName());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    62
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    63
    /** Creates a new instance of NullObjectNameTest */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    64
    public NullObjectNameTest() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    65
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    66
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    67
    public static class MyWombat
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    68
            extends Wombat {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    69
        public MyWombat() throws NotCompliantMBeanException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    70
            super();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    71
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    72
        @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    73
        public ObjectName preRegister(MBeanServer server, ObjectName name)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    74
        throws Exception {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    75
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    76
            if (name == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    77
                name = new ObjectName(":type=Wombat");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    78
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    79
            return super.preRegister(server, name);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    80
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    81
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    82
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    83
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    84
    static String failure=null;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    85
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    86
    public static void testRegister() throws Exception {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    87
        final MBeanServer top = ManagementFactory.getPlatformMBeanServer();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    88
        final MBeanServer sub = MBeanServerFactory.createMBeanServer();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    89
        final JMXServiceURL url = new JMXServiceURL("rmi",null,0);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    90
        final JMXConnectorServer srv =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    91
                JMXConnectorServerFactory.newJMXConnectorServer(url,null,sub);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    92
        srv.start();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    93
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    94
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    95
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    96
            // Create a namespace rmi// that points to 'sub' and flows through
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    97
            // a JMXRemoteNamespace connected to 'srv'
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    98
            // The namespace rmi// will accept createMBean, but not registerMBean.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    99
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   100
            final JMXRemoteNamespace rmiHandler = JMXRemoteNamespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   101
                    newJMXRemoteNamespace(srv.getAddress(),null);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   102
            top.registerMBean(rmiHandler,JMXNamespaces.getNamespaceObjectName("rmi"));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   103
            top.invoke(JMXNamespaces.getNamespaceObjectName("rmi"),
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   104
                    "connect", null, null);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   105
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   106
            // Create a namespace direct// that points to 'sub' and flows
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   107
            // through a direct reference to 'sub'.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   108
            // The namespace direct// will accept createMBean, and registerMBean.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   109
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   110
            final JMXNamespace directHandler = new JMXNamespace(sub);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   111
            top.registerMBean(directHandler,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   112
                    JMXNamespaces.getNamespaceObjectName("direct"));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   113
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   114
            // Now cd to each of the created namespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   115
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   116
            MBeanServer cdrmi = JMXNamespaces.narrowToNamespace(top,"rmi");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   117
            MBeanServer cddirect = JMXNamespaces.narrowToNamespace(top,"direct");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   118
            boolean ok = false;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   119
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   120
            // Check that calling createMBean with a null ObjectName fails
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   121
            // gracefully for namespace rmi// (we can't add rmi// to a null
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   122
            // ObjectName.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   123
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   124
            // TODO: do this test for all createMBean flavors!
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   125
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   126
                final ObjectInstance moi1 =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   127
                        cdrmi.createMBean(MyWombat.class.getName(),null);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   128
                System.out.println(moi1.getObjectName().toString()+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   129
                        ": created through rmi//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   130
                cddirect.unregisterMBean(moi1.getObjectName());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   131
                failed("expected MBeanRegistrationException");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   132
            } catch (MBeanRegistrationException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   133
                System.out.println("Received expected exception: " + x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   134
                if (!(x.getCause() instanceof IllegalArgumentException)) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   135
                    System.err.println("Bad wrapped exception: "+ x.getCause());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   136
                    failed("expected IllegalArgumentException");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   137
                }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   138
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   139
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   140
            // Check that calling refgisterMBean with a null ObjectName fails
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   141
            // gracefully for namespace direct// (we can't add direct// to a null
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   142
            // ObjectName.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   143
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   144
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   145
                final ObjectInstance moi2 =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   146
                        cddirect.registerMBean(new MyWombat(), (ObjectName)null);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   147
                System.out.println(moi2.getObjectName().toString()+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   148
                        ": created through direct//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   149
                cdrmi.unregisterMBean(moi2.getObjectName());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   150
                failed("expected MBeanRegistrationException");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   151
            } catch (MBeanRegistrationException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   152
                System.out.println("Received expected exception: " + x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   153
                if (!(x.getCause() instanceof IllegalArgumentException)) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   154
                    System.err.println("Bad wrapped exception: "+ x.getCause());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   155
                    failed("expected IllegalArgumentException");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   156
                }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   157
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   158
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   159
            // Now artificially pretend that 'sub' is contained in a faked//
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   160
            // namespace.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   161
            // We should be able to use 'null' in registerMBean/createMBean in
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   162
            // this case.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   163
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   164
            RoutingServerProxy proxy =
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1227
diff changeset
   165
                    new RoutingServerProxy(sub, "", "faked", true);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   166
            final ObjectInstance moi3 =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   167
                    proxy.registerMBean(new MyWombat(),null);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   168
            System.out.println(moi3.getObjectName().toString()+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   169
                    ": created through faked//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   170
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   171
            // Now check that null is correctly handled (accepted or rejected)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   172
            // in queries for each of the above configs.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   173
            //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   174
            ObjectName wombat = moi3.getObjectName().withDomain(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   175
                    moi3.getObjectName().getDomain().substring("faked//".length()));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   176
            ObjectInstance moi = new ObjectInstance(wombat,moi3.getClassName());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   177
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   178
            System.out.println("Checking queryNames(null,null) with rmi//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   179
            assertEquals(cdrmi.queryNames(null,null).contains(wombat),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   180
            System.out.println("Checking queryNames(null,null) with direct//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   181
            assertEquals(cddirect.queryNames(null,null).contains(wombat),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   182
            System.out.println("Checking queryMBeans(null,null) with rmi//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   183
            assertEquals(cdrmi.queryMBeans(null,null).contains(moi),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   184
            System.out.println("Checking queryMBeans(null,null) with direct//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   185
            assertEquals(cddirect.queryMBeans(null,null).contains(moi),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   186
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   187
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   188
                System.out.println("Checking queryNames(null,null) with faked//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   189
                assertEquals(proxy.queryNames(null,null).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   190
                        contains(moi3.getObjectName()),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   191
                failed("queryNames(null,null) should have failed for faked//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   192
            } catch (IllegalArgumentException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   193
                System.out.println("Received expected exception for faked//: "+x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   194
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   195
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   196
                System.out.println("Checking queryMBeans(null,null) with faked//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   197
                assertEquals(proxy.queryMBeans(null,null).contains(moi3),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   198
                failed("queryMBeans(null,null) should have failed for faked//");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   199
            } catch (IllegalArgumentException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   200
                System.out.println("Received expected exception for faked//: "+x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   201
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   202
            System.out.println("Checking queryNames(faked//*:*,null)");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   203
            assertEquals(proxy.queryNames(new ObjectName("faked//*:*"),null).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   204
                    contains(moi3.getObjectName()),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   205
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   206
            System.out.println("Checking queryMBeans(faked//*:*,null)");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   207
            assertEquals(proxy.queryMBeans(new ObjectName("faked//*:*"),null).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   208
                    contains(moi3),true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   209
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   210
            proxy.unregisterMBean(moi3.getObjectName());
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   211
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   212
            // ADD NEW TESTS HERE ^^^
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   213
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   214
        } finally {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   215
            srv.stop();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   216
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   217
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   218
        if (failure != null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   219
            throw new Exception(failure);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   220
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   221
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   222
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   223
    private static void assertEquals(Object x, Object y) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   224
        if (!equal(x, y))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   225
            failed("expected " + string(x) + "; got " + string(y));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   226
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   227
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   228
    private static boolean equal(Object x, Object y) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   229
        if (x == y)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   230
            return true;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   231
        if (x == null || y == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   232
            return false;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   233
        if (x.getClass().isArray())
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   234
            return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   235
        return x.equals(y);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   236
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   237
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   238
    private static String string(Object x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   239
        String s = Arrays.deepToString(new Object[] {x});
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   240
        return s.substring(1, s.length() - 1);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   241
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   242
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   243
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   244
    private static void failed(String why) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   245
        failure = why;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   246
        new Throwable("FAILED: " + why).printStackTrace(System.out);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   247
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   248
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   249
    public static void main(String[] args) throws Exception {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   250
        testRegister();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   251
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   252
}