FindMethodTest.java 14.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 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/*
 * 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
 * @bug 6287328
 * @summary Add methods to StandardMBean to retrieve a method based on
 * MBean{Attribute|Operation}Info
 * @author Jean-Francois Denise
 * @run main FindMethodTest
 */

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.management.MBean;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.ManagedAttribute;
import javax.management.ManagedOperation;
import javax.management.ObjectName;
import javax.management.StandardMBean;

public class FindMethodTest {

    private static MBeanServer server =
            ManagementFactory.getPlatformMBeanServer();

    private static Map<String, Set<Method>> expectedMapping =
            new HashMap<String, Set<Method>>();
    private static Set<Method> STATE_SET = new HashSet<Method>();
    private static Set<Method> ENABLED_SET = new HashSet<Method>();
    private static Set<Method> DOIT_SET = new HashSet<Method>();
    private static Set<Method> STATUS_SET = new HashSet<Method>();
    private static Set<Method> HEAPMEMORYUSAGE_SET = new HashSet<Method>();
    private static Set<Method> THREADINFO_SET = new HashSet<Method>();
    private static Set<Method> DOIT_ANNOTATED_SET = new HashSet<Method>();
    private static Set<Method> IT_ANNOTATED_SET = new HashSet<Method>();
    private static HashSet<Set<Method>> TEST_MBEAN_SET =
            new HashSet<Set<Method>>();
    private static HashSet<Set<Method>> ANNOTATED_MBEAN_SET =
            new HashSet<Set<Method>>();
    private static HashSet<Set<Method>> MEMORY_MBEAN_SET =
            new HashSet<Set<Method>>();
    private static HashSet<Set<Method>> THREAD_MBEAN_SET =
            new HashSet<Set<Method>>();

    public interface TestMBean {

        public void doIt();

        public void setState(String str);

        public String getState();

        public boolean isEnabled();

        public void setStatus(int i);
    }

    public interface FaultyTestMBean {

        public void doIt(String doIt);

        public long getState();

        public void setEnabled(boolean b);

        public int getStatus();

        public String setWrong(int i);
    }

    @MBean
    public static class AnnotatedTest {
        @ManagedOperation
        public void doItAnnotated() {

        }

        public void dontDoIt() {

        }

        @ManagedAttribute
        public String getItAnnotated() {
            return null;
        }
        @ManagedAttribute
        public void setItAnnotated(String str) {

        }

        public String getItNot() {
            return null;
        }

    }

    static class Test implements TestMBean {

        public void doIt() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setState(String str) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getState() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isEnabled() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void setStatus(int i) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }


    static {
        try {
            ENABLED_SET.add(TestMBean.class.getDeclaredMethod("isEnabled"));

            STATE_SET.add(TestMBean.class.getDeclaredMethod("getState"));
            STATE_SET.add(TestMBean.class.getDeclaredMethod("setState",
                    String.class));
            STATUS_SET.add(TestMBean.class.getDeclaredMethod("setStatus",
                    int.class));

            DOIT_SET.add(TestMBean.class.getDeclaredMethod("doIt"));

            DOIT_ANNOTATED_SET.add(AnnotatedTest.class.getDeclaredMethod("doItAnnotated"));

            IT_ANNOTATED_SET.add(AnnotatedTest.class.getDeclaredMethod("getItAnnotated"));
            IT_ANNOTATED_SET.add(AnnotatedTest.class.getDeclaredMethod("setItAnnotated", String.class));

            THREADINFO_SET.add(ThreadMXBean.class.getDeclaredMethod("dumpAllThreads", boolean.class,
                    boolean.class));

            HEAPMEMORYUSAGE_SET.add(MemoryMXBean.class.getDeclaredMethod("getHeapMemoryUsage"));

            TEST_MBEAN_SET.add(ENABLED_SET);
            TEST_MBEAN_SET.add(STATE_SET);
            TEST_MBEAN_SET.add(STATUS_SET);
            TEST_MBEAN_SET.add(DOIT_SET);

            ANNOTATED_MBEAN_SET.add(DOIT_ANNOTATED_SET);
            ANNOTATED_MBEAN_SET.add(IT_ANNOTATED_SET);

            MEMORY_MBEAN_SET.add(HEAPMEMORYUSAGE_SET);

            THREAD_MBEAN_SET.add(THREADINFO_SET);

            expectedMapping.put("State", STATE_SET);
            expectedMapping.put("Enabled", ENABLED_SET);
            expectedMapping.put("Status", STATUS_SET);
            expectedMapping.put("doIt", DOIT_SET);
            expectedMapping.put("HeapMemoryUsage", HEAPMEMORYUSAGE_SET);
            expectedMapping.put("dumpAllThreads", THREADINFO_SET);
            expectedMapping.put("doItAnnotated", DOIT_ANNOTATED_SET);
            expectedMapping.put("ItAnnotated", IT_ANNOTATED_SET);

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("Initialization failed");
        }
    }

    private static void testMBean(ObjectName name, Class<?> itf,
            HashSet<Set<Method>> expectMappings)
            throws Exception {

        Set<Set<Method>> expectedMappings =
                (Set<Set<Method>>) expectMappings.clone();

        MBeanInfo info = server.getMBeanInfo(name);
        for (MBeanAttributeInfo attr : info.getAttributes()) {
            Set<Method> expected = expectedMapping.get(attr.getName());
            if (expected == null) {
                continue;
            }
            if (!expectedMappings.remove(expected)) {
                throw new Exception("The mapping to use is not the expected " +
                        "one for " + attr);
            }
            System.out.println("Expected : " + expected);
            Set<Method> found =
                    StandardMBean.findAttributeAccessors(itf, attr);
            System.out.println("Found : " + found);
            if (!found.equals(expected)) {
                throw new Exception("Mapping error.");
            }
        }
        for (MBeanOperationInfo op : info.getOperations()) {
            Set<Method> expected = expectedMapping.get(op.getName());
            if (expected == null) {
                continue;
            }
            if (!expectedMappings.remove(expected)) {
                throw new Exception("The mapping to use is not the expected " +
                        "one for " + op);
            }
            System.out.println("Expected : " + expected);
            Method method =
                    StandardMBean.findOperationMethod(itf, op);
            Set<Method> found = new HashSet<Method>();
            found.add(method);
            System.out.println("Found : " + found);
            if (!found.equals(expected)) {
                throw new Exception("Mapping error.");
            }
        }

        if (expectedMappings.size() != 0) {
            throw new Exception("Some mapping have not been found " +
                    expectedMappings);
        } else {
            System.out.println("All mappings have been found");
        }
    }

    public static void main(String[] args) throws Exception {
        // Positive tests
        Test t = new Test();
        ObjectName name = ObjectName.valueOf(":type=Test");
        server.registerMBean(t, name);
        AnnotatedTest at = new AnnotatedTest();
        ObjectName annotatedName = ObjectName.valueOf(":type=AnnotatedTest");
        server.registerMBean(at, annotatedName);

        testMBean(name, TestMBean.class, TEST_MBEAN_SET);

        testMBean(annotatedName, AnnotatedTest.class, ANNOTATED_MBEAN_SET);

        ObjectName memoryName =
                ObjectName.valueOf(ManagementFactory.MEMORY_MXBEAN_NAME);
        testMBean(memoryName, MemoryMXBean.class, MEMORY_MBEAN_SET);

        ObjectName threadName =
                ObjectName.valueOf(ManagementFactory.THREAD_MXBEAN_NAME);
        testMBean(threadName, ThreadMXBean.class, THREAD_MBEAN_SET);

        // Negative tests
        try {
            StandardMBean.findOperationMethod(null,
                    new MBeanOperationInfo("Test",
                    TestMBean.class.getDeclaredMethod("doIt")));
            throw new Exception("Expected exception not found");
        } catch (IllegalArgumentException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        try {
            StandardMBean.findOperationMethod(TestMBean.class, null);
            throw new Exception("Expected exception not found");
        } catch (IllegalArgumentException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        try {
            StandardMBean.findAttributeAccessors(null,
                    new MBeanAttributeInfo("Test", "Test",
                    TestMBean.class.getDeclaredMethod("getState"),
                    TestMBean.class.getDeclaredMethod("setState",
                    String.class)));
            throw new Exception("Expected exception not found");
        } catch (IllegalArgumentException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        try {
            StandardMBean.findAttributeAccessors(TestMBean.class, null);
            throw new Exception("Expected exception not found");
        } catch (IllegalArgumentException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        //Wrong operation signature
        try {
            StandardMBean.findOperationMethod(TestMBean.class,
                    new MBeanOperationInfo("FaultyTest",
                    FaultyTestMBean.class.getDeclaredMethod("doIt",
                    String.class)));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        //Wrong attribute accessor
        try {
            StandardMBean.findAttributeAccessors(TestMBean.class,
                    new MBeanAttributeInfo("FaultyTest", "FaultyTest", null,
                    FaultyTestMBean.class.getDeclaredMethod("setEnabled",
                    String.class)));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        //Wrong attribute type
        try {
            StandardMBean.findAttributeAccessors(TestMBean.class,
                    new MBeanAttributeInfo("State", "toto.FaultType",
                    "FaultyTest", true, true, false));
            throw new Exception("Expected exception not found");
        } catch (ClassNotFoundException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        //Wrong operation parameter type
        try {
            MBeanParameterInfo[] p = {new MBeanParameterInfo("p1",
                "toto.FaultType2", "FaultyParameter")
            };
            StandardMBean.findOperationMethod(TestMBean.class,
                    new MBeanOperationInfo("doIt", "FaultyMethod", p, "void",
                    0));
            throw new Exception("Expected exception not found");
        } catch (ClassNotFoundException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        // Check that not annotated attributes are not found
        try {
            StandardMBean.findAttributeAccessors(AnnotatedTest.class,
                    new MBeanAttributeInfo("ItNot", String.class.getName(),
                    "FaultyTest", true, false, false));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        // Check that not annotated operations are not found
        try {
            StandardMBean.findOperationMethod(AnnotatedTest.class,
                    new MBeanOperationInfo("dontDoIt","dontDoIt",null,
                    Void.TYPE.getName(),0));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
        // Check that wrong getter return type throws Exception
        try {
            StandardMBean.findAttributeAccessors(AnnotatedTest.class,
                    new MBeanAttributeInfo("ItAnnotated", Long.class.getName(),
                    "FaultyTest", true, false, false));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
         // Check that wrong setter return type throws Exception
        try {
            StandardMBean.findAttributeAccessors(FaultyTestMBean.class,
                    new MBeanAttributeInfo("Wrong", String.class.getName(),
                    "FaultyTest", true, true, false));
            throw new Exception("Expected exception not found");
        } catch (NoSuchMethodException ex) {
            System.out.println("OK received expected exception " + ex);
        }
    }
}