jdk/test/javax/management/remote/mandatory/loading/MethodResultTest.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4898478
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests client default class loader used before JSR 160 loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean MethodResultTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build MethodResultTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main MethodResultTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   This test checks that the class loader that is used to deserialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   the return values from remote MBean server operations is indeed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   one specified by the user.  The only MBean server operations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   matter are those than can return an arbitrary Object.  We don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   care about getMBeanCount or queryNames or whatever because their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   return values are always of classes loaded by the bootstrap loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   But for the operations getAttribute, getAttributes, setAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
   and invoke, the return value can include any Java class.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
   also true of getMBeanInfo, since the return value can be an exotic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
   subclass of MBeanInfo, or a ModelMBeanInfo that refers to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
   arbitrary Object.  The JMX Remote API spec requires that these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
   return values be deserialized using the class loader supplied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
   the user (default is context class loader).  In particular it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
   not be deserialized using the system class loader, which it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   with RMI unless special precautions are taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class MethodResultTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Class thisClass = MethodResultTest.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Class exoticClass = Exotic.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        String exoticClassName = Exotic.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ClassLoader testClassLoader = thisClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!(testClassLoader instanceof URLClassLoader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.out.println("TEST INVALID: Not loaded by a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                               "URLClassLoader: " + testClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        URLClassLoader tcl = (URLClassLoader) testClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        URL[] urls = tcl.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ClassLoader shadowLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            new ShadowLoader(urls, testClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                             new String[] {exoticClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                           ExoticMBeanInfo.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                           ExoticException.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Class cl = shadowLoader.loadClass(exoticClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (cl == exoticClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("TEST INVALID: Shadow class loader loaded " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                               "same class as test class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Thread.currentThread().setContextClassLoader(shadowLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ObjectName on = new ObjectName("a:b=c");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        mbs.createMBean(Thing.class.getName(), on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        final String[] protos = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (int i = 0; i < protos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                ok &= test(protos[i], mbs, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                System.out.println("TEST FAILED WITH EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static boolean test(String proto, MBeanServer mbs, ObjectName on)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("Testing for protocol " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        JMXConnectorServer cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        JMXServiceURL url = new JMXServiceURL(proto, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                                 mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("System does not recognize URL: " + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                               "; ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        JMXConnector client = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object getAttributeExotic = mbsc.getAttribute(on, "Exotic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        AttributeList getAttrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            mbsc.getAttributes(on, new String[] {"Exotic"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        AttributeList setAttrs = new AttributeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        setAttrs.add(new Attribute("Exotic", new Exotic()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        setAttrs = mbsc.setAttributes(on, setAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Object invokeExotic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        MBeanInfo exoticMBI = mbsc.getMBeanInfo(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        mbsc.setAttribute(on, new Attribute("Exception", Boolean.TRUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            getAttributeException, setAttributeException, invokeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                mbsc.getAttribute(on, "Exotic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw noException("getAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                getAttributeException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                mbsc.setAttribute(on, new Attribute("Exotic", new Exotic()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw noException("setAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                setAttributeException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                throw noException("invoke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                invokeException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            mbsc.setAttribute(on, new Attribute("Exception", Boolean.FALSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ok &= checkAttrs("getAttributes", getAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ok &= checkAttrs("setAttributes", setAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ok &= checkType("getAttribute", getAttributeExotic, Exotic.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        ok &= checkType("getAttributes", attrValue(getAttrs), Exotic.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        ok &= checkType("setAttributes", attrValue(setAttrs), Exotic.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ok &= checkType("invoke", invokeExotic, Exotic.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ok &= checkType("getMBeanInfo", exoticMBI, ExoticMBeanInfo.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ok &= checkExceptionType("getAttribute", getAttributeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                 ExoticException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ok &= checkExceptionType("setAttribute", setAttributeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                 ExoticException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ok &= checkExceptionType("invoke", invokeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                 ExoticException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.out.println("Test passes for protocol " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static Exception noException(String what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            "Operation " + what + " returned when exception expected";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return new IllegalStateException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static Object attrValue(AttributeList attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return ((Attribute) attrs.get(0)).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private static boolean checkType(String what, Object object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                     Class wrongClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return checkType(what, object, wrongClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static boolean checkType(String what, Object object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                     Class wrongClass, boolean isException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        final String type = isException ? "exception" : "object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        final String rendered = isException ? "thrown" : "returned";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        System.out.println("For " + type + " " + rendered + " by " + what +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                           ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (wrongClass.isInstance(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println("TEST FAILS: " + type + " loaded by test " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                               "classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        String className = object.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (!className.equals(wrongClass.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            System.out.println("TEST FAILS: " + rendered + " " + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                               " has wrong class name: " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        System.out.println("Test passes: " + rendered + " " + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                           " has same class name but is not same class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static boolean checkExceptionType(String what, Exception exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                              Class wrongClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (!(exception instanceof MBeanException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            System.out.println("Exception thrown by " + what + " is not an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                               MBeanException.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                               ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            exception.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        exception = ((MBeanException) exception).getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return checkType(what, exception, wrongClass, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static boolean checkAttrs(String what, AttributeList attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (attrs.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            System.out.println("TEST FAILS: list returned by " + what +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               " does not have size 1: " + attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Attribute attr = (Attribute) attrs.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (!"Exotic".equals(attr.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            System.out.println("TEST FAILS: " + what + " returned wrong " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                               "attribute: " + attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static class Thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            extends StandardMBean implements ThingMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public Thing() throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            super(ThingMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        public Exotic getExotic() throws ExoticException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new ExoticException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return new Exotic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public void setExotic(Exotic x) throws ExoticException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                throw new ExoticException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public Exotic anExotic() throws ExoticException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                throw new ExoticException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return new Exotic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public void cacheMBeanInfo(MBeanInfo mbi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (mbi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                mbi = new ExoticMBeanInfo(mbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            super.cacheMBeanInfo(mbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public void setException(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            this.exception = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        private boolean exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static interface ThingMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        public Exotic getExotic() throws ExoticException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public void setExotic(Exotic x) throws ExoticException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        public Exotic anExotic() throws ExoticException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public void setException(boolean x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static class Exotic implements Serializable {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static class ExoticException extends Exception {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public static class ExoticMBeanInfo extends MBeanInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public ExoticMBeanInfo(MBeanInfo mbi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            super(mbi.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                  mbi.getDescription(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                  mbi.getAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                  mbi.getConstructors(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                  mbi.getOperations(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                  mbi.getNotifications());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static class ShadowLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        ShadowLoader(URL[] urls, ClassLoader realLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                     String[] shadowClassNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            super(urls, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            this.realLoader = realLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            this.shadowClassNames = Arrays.asList(shadowClassNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        protected Class findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (shadowClassNames.contains(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return super.findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                return realLoader.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private final ClassLoader realLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        private final List shadowClassNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}