LeadingSeparatorsTest.java 8.9 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 158 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
/*
 * 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 LeadingSeparatorsTest.java
 * @summary Test that the semantics of a leading // in ObjectName is respected.
 * @author Daniel Fuchs
 * @bug 5072476
 * @run clean LeadingSeparatorsTest Wombat WombatMBean
 * @compile -XDignore.symbol.file=true  LeadingSeparatorsTest.java
 * @run build LeadingSeparatorsTest Wombat WombatMBean
 * @run main LeadingSeparatorsTest
 */

import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.util.logging.Logger;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.NotCompliantMBeanException;
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 LeadingSeparatorsTest
 * @author Sun Microsystems, 2005 - All rights reserved.
 */
public class LeadingSeparatorsTest {

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

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

    public static interface MyWombatMBean extends WombatMBean {
        public Set<ObjectName> untrue(ObjectName pat) throws Exception;
    }
    public static class MyWombat
            extends Wombat implements MyWombatMBean {
        public MyWombat() throws NotCompliantMBeanException {
            super(MyWombatMBean.class);
        }

        public Set<ObjectName> untrue(ObjectName pat) throws Exception {
            final Set<ObjectName> res=listMatching(pat.withDomain("*"));
            final Set<ObjectName> untrue = new HashSet<ObjectName>();
            for (ObjectName a:res) {
                untrue.add(a.withDomain(pat.getDomain()+"//"+a.getDomain()));
            }
            return untrue;
        }
    }

    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"));

            final ObjectName n1 = new ObjectName("//direct//w:type=Wombat");
            final ObjectName n2 = new ObjectName("direct//w:type=Wombat");
            final ObjectName n3 = new ObjectName("//rmi//w:type=Wombat");
            final ObjectName n4 = new ObjectName("rmi//w:type=Wombat");

            // register wombat using an object name with a leading //
            final Object     obj = new MyWombat();
            // check that returned object name doesn't have the leading //
            assertEquals(n2,top.registerMBean(obj, n1).getObjectName());
            System.out.println(n1+" registered");

            // check that the registered Wombat can be accessed with all its
            // names.
            System.out.println(n2+" mood is: "+top.getAttribute(n2, "Mood"));
            System.out.println(n1+" mood is: "+top.getAttribute(n1, "Mood"));
            System.out.println(n4+" mood is: "+top.getAttribute(n4, "Mood"));
            System.out.println(n3+" mood is: "+top.getAttribute(n3, "Mood"));

            // call listMatching. The result should not contain any prefix.
            final Set<ObjectName> res = (Set<ObjectName>)
                    top.invoke(n3, "listMatching",
                    // remove rmi// from rmi//*:*
                    JMXNamespaces.deepReplaceHeadNamespace(
                    new Object[] {ObjectName.WILDCARD.withDomain("rmi//*")},
                    "rmi", ""), new String[] {ObjectName.class.getName()});

            // add rmi// prefix to all names in res.
            final Set<ObjectName> res1 =
                   JMXNamespaces.deepReplaceHeadNamespace(res, "", "rmi");
            System.out.println("got: "+res1);

            // compute expected result
            final Set<ObjectName> res2 = sub.queryNames(null,null);
            final Set<ObjectName> res3 = new HashSet<ObjectName>();
            for (ObjectName o:res2) {
               res3.add(o.withDomain("rmi//"+o.getDomain()));
            }
            System.out.println("expected: "+res3);
            assertEquals(res1, res3);

            // invoke "untrue(//niark//niark:*)"
            // should return a set were all ObjectNames begin with
            // //niark//niark//
            //
            final Set<ObjectName> res4 = (Set<ObjectName>)
                    top.invoke(n3, "untrue",
                    // remove niark//niark : should remove nothing since
                    // our ObjectName begins with a leading //
                    JMXNamespaces.deepReplaceHeadNamespace(
                    new Object[] {
                       ObjectName.WILDCARD.withDomain("//niark//niark")},
                    "niark//niark", ""),
                    new String[] {ObjectName.class.getName()});
            System.out.println("got: "+res4);

            // add rmi// should add nothing since the returned names have a
            // leading //
            //
            final Set<ObjectName> res5 =
                   JMXNamespaces.deepReplaceHeadNamespace(res4, "", "rmi");
            System.out.println("got#2: "+res5);

            // compute expected result
            final Set<ObjectName> res6 = new HashSet<ObjectName>();
            for (ObjectName o:res2) {
               res6.add(o.withDomain("//niark//niark//"+o.getDomain()));
            }
            System.out.println("expected: "+res6);

            // both res4 and res5 should be equals to the expected result.
            assertEquals(res4, res6);
            assertEquals(res5, res6);

        } 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();
    }
}