ObjectNameTemplateTest.java 12.1 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 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*
 * 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 %M% %I%
 * @bug 6675526
 * @summary Test MBeans named with @ObjectNameTemplate
 * @author Jean-Francois Denise
 * @run main/othervm ObjectNameTemplateTest
 */
import java.lang.management.ManagementFactory;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.ImmutableDescriptor;
import javax.management.InvalidAttributeValueException;
import javax.management.JMX;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MXBean;
import javax.management.MBean;
import javax.management.ManagedAttribute;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ObjectNameTemplate;
import javax.management.ReflectionException;
import javax.management.StandardMBean;

public class ObjectNameTemplateTest {

    private static MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    private static final String NAME_TEMPLATE_MULTI =
            "com.example:type=MultiStdCache,name={Name}";
    private static final String NAME_TEMPLATE_MONO =
            "com.example:{Type}={TypeValue}";
    private static final String NAME_TEMPLATE_QUOTED =
            "com.example:type=Quotted,name=\"{Name}\"";
    private static final String NAME_TEMPLATE_WRAPPED =
            "com.example:type=MgtInterface,id={Id}";
    private static final String NAME_TEMPLATE_FULL =
            "{Naming}";
    private static final String FULL_NAME = "com.example:type=NotAdvised";
    private static final String NAME1 = "toto1";
    private static final String NAME2 = "toto2";
    private static final String TYPE_KEY = "thisIsTheType";
    private static final String TYPE_VALUE = "aTypeValue";
    private static final String INVALID_NAME = "?,=*,\n, ";
    private static final int ID = 999;
    private static Object[] EMPTY_PARAMS = {};
    private static String[] EMPTY_SIGNATURE = {};
    private static final ObjectName OBJECTNAME_CACHE =
            ObjectName.valueOf("com.example:type=Cache");
    private static final ObjectName OBJECTNAME_SUBCACHE =
            ObjectName.valueOf("com.example:type=SubCache");
    private static final ObjectName OBJECTNAME_CACHEMX =
            ObjectName.valueOf("com.example:type=CacheMX");
    private static final ObjectName OBJECTNAME_SUBCACHEMX =
            ObjectName.valueOf("com.example:type=SubCacheMX");
    private static final ObjectName OBJECTNAME_DYNACACHE =
            ObjectName.valueOf("com.example:type=DynaCache");
    private static final ObjectName OBJECTNAME_STDCACHE =
            ObjectName.valueOf("com.example:type=StdCache");
    private static final ObjectName OBJECTNAME_STDCACHEMX =
            ObjectName.valueOf("com.example:type=StdCacheMX");
    private static final ObjectName OBJECTNAME_MULTI_1 =
            ObjectName.valueOf("com.example:" +
            "type=MultiStdCache,name=" + NAME1);
    private static final ObjectName OBJECTNAME_MULTI_2 =
            ObjectName.valueOf("com.example:" +
            "type=MultiStdCache,name=" + NAME2);
    private static final ObjectName OBJECTNAME_MONO =
            ObjectName.valueOf("com.example:" + TYPE_KEY + "=" +
            TYPE_VALUE);
    private static final ObjectName OBJECTNAME_QUOTED =
            ObjectName.valueOf("com.example:type=Quotted," +
            "name="+ObjectName.quote(INVALID_NAME));
    private static final ObjectName OBJECTNAME_WRAPPED_RESOURCE =
            ObjectName.valueOf("com.example:type=MgtInterface,id=" + ID);
    private static final ObjectName OBJECTNAME_FULL =
            ObjectName.valueOf(FULL_NAME);

    private static void test(Class<?> mbean, Object[] params,
            String[] signature, ObjectName name, String template)
            throws Exception {
        mbs.createMBean(mbean.getName(), null, params, signature);
        test(name, template);
        List<Class<?>> parameters = new ArrayList<Class<?>>();
        for (String sig : signature) {
            parameters.add(Class.forName(sig));
        }
        Class<?> classes[] = new Class<?>[parameters.size()];
        Constructor ctr = mbean.getConstructor(parameters.toArray(classes));
        Object inst = ctr.newInstance(params);
        test(inst, name, template);
    }

    private static void test(Object obj, ObjectName name, String template)
            throws Exception {
        mbs.registerMBean(obj, null);
        test(name, template);
    }

    private static void test(ObjectName name, String template)
            throws Exception {
        if (!mbs.isRegistered(name)) {
            throw new Exception("Wrong " + name + " name");
        }
        if (template != null && !mbs.getMBeanInfo(name).getDescriptor().
                getFieldValue("objectNameTemplate").equals(template)) {
            throw new Exception("Invalid Derscriptor");
        }
        mbs.unregisterMBean(name);
    }

    public static void main(String[] args) throws Exception {
        test(Cache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_CACHE,
                OBJECTNAME_CACHE.toString());

        test(CacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_CACHEMX,
                OBJECTNAME_CACHEMX.toString());

        test(SubCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_SUBCACHE,
                OBJECTNAME_SUBCACHE.toString());

        test(SubCacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_SUBCACHEMX,
                OBJECTNAME_SUBCACHEMX.toString());

        test(DynaCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_DYNACACHE,
                null);

        test(StdCacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_STDCACHEMX,
                OBJECTNAME_STDCACHEMX.toString());

        test(StdCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_STDCACHE,
                OBJECTNAME_STDCACHE.toString());
        String[] sig = {String.class.getName()};
        Object[] params = {NAME1};
        test(MultiStdCache.class, params, sig, OBJECTNAME_MULTI_1,
                NAME_TEMPLATE_MULTI);
        Object[] params2 = {NAME2};
        test(MultiStdCache.class, params2, sig, OBJECTNAME_MULTI_2,
                NAME_TEMPLATE_MULTI);

        test(MonoStdCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_MONO,
                NAME_TEMPLATE_MONO);

        test(Quoted.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_QUOTED,
                NAME_TEMPLATE_QUOTED);

        test(new StandardMBean(new WrappedResource(), MgtInterface.class),
                OBJECTNAME_WRAPPED_RESOURCE, NAME_TEMPLATE_WRAPPED);

        test(FullName.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_FULL,
                NAME_TEMPLATE_FULL);
        try {
            test(Wrong.class, EMPTY_PARAMS, EMPTY_SIGNATURE, null, null);
            throw new Exception("No treceived expected Exception");
        } catch (NotCompliantMBeanException ncex) {
            if (!(ncex.getCause() instanceof AttributeNotFoundException)) {
                throw new Exception("Invalid initCause");
            }
        }
    }

    @MBean
    @ObjectNameTemplate("{Naming}")
    public static class FullName {

        @ManagedAttribute
        public String getNaming() {
            return FULL_NAME;
        }
    }

    @ObjectNameTemplate("com.example:type=MgtInterface,id={Id}")
    public interface MgtInterface {

        public int getId();
    }

    public static class WrappedResource implements MgtInterface {

        public int getId() {
            return ID;
        }
    }

    @MBean
    @ObjectNameTemplate("com.example:type=Cache")
    public static class Cache {
    }

    @ObjectNameTemplate("com.example:type=SubCache")
    public static class SubCache extends Cache {
    }

    @MXBean
    @ObjectNameTemplate("com.example:type=CacheMX")
    public static class CacheMX {
    }

    @ObjectNameTemplate("com.example:type=SubCacheMX")
    public static class SubCacheMX extends CacheMX {
    }

    @ObjectNameTemplate("com.example:type=StdCache")
    public interface StdCacheMBean {
    }

    public static class StdCache implements StdCacheMBean {
    }

    @ObjectNameTemplate("com.example:type=StdCacheMX")
    public interface StdCacheMXBean {
    }

    public static class StdCacheMX implements StdCacheMXBean {
    }

    public static class DynaCache implements DynamicMBean {

        public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public AttributeList getAttributes(String[] attributes) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public AttributeList setAttributes(AttributeList attributes) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public MBeanInfo getMBeanInfo() {
            ImmutableDescriptor d = new ImmutableDescriptor(JMX.OBJECT_NAME_TEMPLATE + "=com.example:type=DynaCache");

            return new MBeanInfo("DynaCache", "Description", null, null, null, null, d);
        }
    }

    @ObjectNameTemplate("com.example:type=MultiStdCache,name={Name}")
    public interface MultiStdCacheMXBean {

        public String getName();
    }

    public static class MultiStdCache implements MultiStdCacheMXBean {

        private String name;

        public MultiStdCache(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    @ObjectNameTemplate("com.example:{Type}={TypeValue}")
    public interface MonoStdCacheMXBean {

        public String getTypeValue();

        public String getType();
    }

    public static class MonoStdCache implements MonoStdCacheMXBean {

        public String getTypeValue() {
            return TYPE_VALUE;
        }

        public String getType() {
            return TYPE_KEY;
        }
    }

    @ObjectNameTemplate("com.example:type=Quotted,name=\"{Name}\"")
    public interface QuottedMXBean {

        public String getName();
    }

    public static class Quoted implements QuottedMXBean {

        public String getName() {
            return INVALID_NAME;
        }
    }

    @ObjectNameTemplate("com.example:{Type}={TypeValue}, name={Name}")
    public interface WrongMXBean {

        public String getTypeValue();

        public String getType();
    }

    public static class Wrong implements WrongMXBean {

        public String getTypeValue() {
            return TYPE_VALUE;
        }

        public String getType() {
            return TYPE_KEY;
        }
    }
}