NullDomainObjectNameTest.java 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
/*
 * @test NullDomainObjectNameTest.java
 * @summary Test that null domains are correctly handled in namespaces.
 * @author Daniel Fuchs
 * @run clean NullDomainObjectNameTest Wombat WombatMBean
 * @compile -XDignore.symbol.file=true  NullDomainObjectNameTest.java
 * @run build NullDomainObjectNameTest Wombat WombatMBean
 * @run main NullDomainObjectNameTest
 */

import com.sun.jmx.namespace.RoutingServerProxy;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.logging.Logger;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.namespace.JMXNamespaces;
import javax.management.namespace.JMXRemoteNamespace;
import javax.management.namespace.JMXNamespace;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

/**
 * Class NullDomainObjectNameTest
 * @author Sun Microsystems, 2005 - All rights reserved.
 */
public class NullDomainObjectNameTest {

    /**
     * A logger for this class.
     **/
    private static final Logger LOG =
            Logger.getLogger(NullDomainObjectNameTest.class.getName());

    /** Creates a new instance of NullDomainObjectNameTest */
    public NullDomainObjectNameTest() {
    }

    public static class MyWombat
            extends Wombat {
        public MyWombat() throws NotCompliantMBeanException {
            super();
        }
        @Override
        public ObjectName preRegister(MBeanServer server, ObjectName name)
        throws Exception {

            if (name == null)
                name = new ObjectName(":type=Wombat");

            return super.preRegister(server, name);
        }

    }

    static String failure=null;

    public static void testRegister() throws Exception {
        final MBeanServer top = ManagementFactory.getPlatformMBeanServer();
        final MBeanServer sub = MBeanServerFactory.createMBeanServer();
        final JMXServiceURL url = new JMXServiceURL("rmi",null,0);
        final JMXConnectorServer srv =
                JMXConnectorServerFactory.newJMXConnectorServer(url,null,sub);
        srv.start();

        try {

            // Create a namespace rmi// that points to 'sub' and flows through
            // a JMXRemoteNamespace connected to 'srv'
            // The namespace rmi// will accept createMBean, but not registerMBean.
            //
            final JMXRemoteNamespace rmiHandler = JMXRemoteNamespace.
                    newJMXRemoteNamespace(srv.getAddress(),
                    null);
            top.registerMBean(rmiHandler,JMXNamespaces.getNamespaceObjectName("rmi"));
            top.invoke(JMXNamespaces.getNamespaceObjectName("rmi"),
                    "connect", null, null);

            // Create a namespace direct// that points to 'sub' and flows
            // through a direct reference to 'sub'.
            // The namespace direct// will accept createMBean, and registerMBean.
            //
            final JMXNamespace directHandler = new JMXNamespace(sub);
            top.registerMBean(directHandler,
                    JMXNamespaces.getNamespaceObjectName("direct"));

            // Now cd to each of the created namespace.
            //
            MBeanServer cdrmi = JMXNamespaces.narrowToNamespace(top,"rmi");
            MBeanServer cddirect = JMXNamespaces.narrowToNamespace(top,"direct");
            boolean ok = false;

            // Check that calling createMBean with a null domain works
            // for namespace rmi//
            //
            try {
                final ObjectInstance moi1 =
                        cdrmi.createMBean(MyWombat.class.getName(),
                        new ObjectName(":type=Wombat"));
                System.out.println(moi1.getObjectName().toString()+
                        ": created through rmi//");
                assertEquals(moi1.getObjectName().getDomain(),
                        cddirect.getDefaultDomain());
                cddirect.unregisterMBean(moi1.getObjectName());
            } catch (MBeanRegistrationException x) {
                System.out.println("Received unexpected exception: " + x);
                failed("Received unexpected exception: " + x);
            }

            // Check that calling refgisterMBean with a null domain works
            // for namespace direct//
            //
            try {
                final ObjectInstance moi2 =
                        cddirect.registerMBean(new MyWombat(),
                        new ObjectName(":type=Wombat"));
                System.out.println(moi2.getObjectName().toString()+
                        ": created through direct//");
                assertEquals(moi2.getObjectName().getDomain(),
                        cdrmi.getDefaultDomain());
                cdrmi.unregisterMBean(moi2.getObjectName());
            } catch (MBeanRegistrationException x) {
                System.out.println("Received unexpected exception: " + x);
                failed("Received unexpected exception: " + x);
            }

            // Now artificially pretend that 'sub' is contained in a faked//
            // namespace.
            //
            RoutingServerProxy proxy =
158
                    new RoutingServerProxy(sub, "", "faked", true);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

            // These should fail because the ObjectName doesn't start
            // with "faked//"
            try {
                final ObjectInstance moi3 =
                    proxy.registerMBean(new MyWombat(),
                    new ObjectName(":type=Wombat"));
                System.out.println(moi3.getObjectName().toString()+
                    ": created through faked//");
                failed("expected MBeanRegistrationException");
            } catch (MBeanRegistrationException x) {
                System.out.println("Received expected exception: " + x);
                if (!(x.getCause() instanceof IllegalArgumentException)) {
                    System.err.println("Bad wrapped exception: "+ x.getCause());
                    failed("expected IllegalArgumentException");
                }
            }

            // null should work with "faked//"
            final ObjectInstance moi3 =
                    proxy.registerMBean(new MyWombat(),null);
            assertEquals(moi3.getObjectName().getDomain(),
                         "faked//"+sub.getDefaultDomain());

            System.out.println(moi3.getObjectName().toString() +
                    ": created through faked//");

            // Now check that null is correctly handled (accepted or rejected)
            // in queries for each of the above configs.
            //
            ObjectName wombat = moi3.getObjectName().withDomain(
                    moi3.getObjectName().getDomain().substring("faked//".length()));
            ObjectInstance moi = new ObjectInstance(wombat,moi3.getClassName());

            System.out.println("Checking queryNames(" +
                    "new ObjectName(\":*\"),null) with rmi//");
            assertEquals(cdrmi.queryNames(
                    new ObjectName(":*"),null).contains(wombat),true);
            System.out.println("Checking queryNames(" +
                    "new ObjectName(\":*\"),null) with direct//");
            assertEquals(cddirect.queryNames(
                    new ObjectName(":*"),null).contains(wombat),true);
            System.out.println("Checking queryMBeans(" +
                    "new ObjectName(\":*\"),null) with rmi//");
            assertEquals(cdrmi.queryMBeans(
                    new ObjectName(":*"),null).contains(moi),true);
            System.out.println("Checking queryMBeans(" +
                    "new ObjectName(\":*\"),null) with direct//");
            assertEquals(cddirect.queryMBeans(
                    new ObjectName(":*"),null).contains(moi),true);

            // These should fail because the ObjectName doesn't start
            // with "faked//"
            try {
                System.out.println("Checking queryNames(" +
                    "new ObjectName(\":*\"),null) with faked//");
                assertEquals(proxy.queryNames(
                        new ObjectName(":*"),null).
                        contains(moi3.getObjectName()),true);
                failed("queryNames(null,null) should have failed for faked//");
            } catch (IllegalArgumentException x) {
                System.out.println("Received expected exception for faked//: "+x);
            }
            // These should fail because the ObjectName doesn't start
            // with "faked//"
            try {
                System.out.println("Checking queryMBeans(" +
                    "new ObjectName(\":*\"),null) with faked//");
                assertEquals(proxy.queryMBeans(
                        new ObjectName(":*"),null).contains(moi3),true);
                failed("queryMBeans(null,null) should have failed for faked//");
            } catch (IllegalArgumentException x) {
                System.out.println("Received expected exception for faked//: "+x);
            }

            System.out.println("Checking queryNames(faked//*:*,null)");
            assertEquals(proxy.queryNames(new ObjectName("faked//*:*"),null).
                    contains(moi3.getObjectName()),true);

            System.out.println("Checking queryMBeans(faked//*:*,null)");
            assertEquals(proxy.queryMBeans(new ObjectName("faked//*:*"),null).
                    contains(moi3),true);

            proxy.unregisterMBean(moi3.getObjectName());

            // ADD NEW TESTS HERE ^^^

        } finally {
            srv.stop();
        }

        if (failure != null)
            throw new Exception(failure);


    }
    private static void assertEquals(Object x, Object y) {
        if (!equal(x, y))
            failed("expected " + string(x) + "; got " + string(y));
    }

    private static boolean equal(Object x, Object y) {
        if (x == y)
            return true;
        if (x == null || y == null)
            return false;
        if (x.getClass().isArray())
            return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
        return x.equals(y);
    }

    private static String string(Object x) {
        String s = Arrays.deepToString(new Object[] {x});
        return s.substring(1, s.length() - 1);
    }


    private static void failed(String why) {
        failure = why;
        new Throwable("FAILED: " + why).printStackTrace(System.out);
    }

    public static void main(String[] args) throws Exception {
        testRegister();
    }
}