jdk/test/javax/management/modelmbean/RequiredModelMBeanMethodTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700 (2010-05-25)
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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 4950756
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that RequiredModelMBean.invoke will not invoke methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * from the RequiredModelMBean class itself if they are not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * ModelMBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean RequiredModelMBeanMethodTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build RequiredModelMBeanMethodTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main RequiredModelMBeanMethodTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.modelmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * We do the same test with a number of different operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * - A plain operation that is directed to the managed resource in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * usual way.  We give it some parameters so we can test that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * class loading logic for signature parameters is reasonable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * - An operation (removeNotificationListener) that is directed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * RequiredModelMBean itself.  We use this particular operation because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * it will throw an exception, which allows us to check that it did in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * fact execute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * - An operation (load()) that has the same signature as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * RequiredModelMBean operation but is directed to the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * because of a "class" field in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * - An operation (store()) that has the same signature as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * RequiredModelMBean operation but is directed to the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * because of a "targetObject" field in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * In each case we check that the operation does not work if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * in the ModelMBeanInfo, and does work if it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class RequiredModelMBeanMethodTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Descriptor tralalaDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                "name=tralala",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                "descriptorType=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "role=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                "targetType=ObjectReference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Method tralalaMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Resource.class.getMethod("tralala",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                     new Class[] {int.class, Resource.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ModelMBeanOperationInfo tralalaInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            new ModelMBeanOperationInfo("tralala descr", tralalaMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                        tralalaDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Method remACNLMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            RequiredModelMBean.class.getMethod("removeAttributeChangeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                               new Class[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                   NotificationListener.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                                   String.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                               });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ModelMBeanOperationInfo remACNLInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            new ModelMBeanOperationInfo("remACNL descr", remACNLMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Descriptor loadDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                "name=load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                "descriptorType=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                "role=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "targetType=ObjectReference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "class=" + Resource.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ModelMBeanOperationInfo loadInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new ModelMBeanOperationInfo("load", "load descr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                        new MBeanParameterInfo[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                        "void", ModelMBeanOperationInfo.ACTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                        loadDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Descriptor storeDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            new DescriptorSupport(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "name=store",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "descriptorType=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                "role=operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                "targetType=ObjectReference",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        storeDescriptor.setField("targetObject", resource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ModelMBeanOperationInfo storeInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            new ModelMBeanOperationInfo("store", "store descr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                        new MBeanParameterInfo[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                        "void", ModelMBeanOperationInfo.ACTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                        storeDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ModelMBeanInfo emptyMMBI =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            new ModelMBeanInfoSupport(Resource.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                      "empty descr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                      null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ModelMBean emptyMMB = new RequiredModelMBean(emptyMMBI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        emptyMMB.setManagedResource(resource, "ObjectReference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ObjectName emptyMMBName = new ObjectName("test:type=Empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        mbs.registerMBean(emptyMMB, emptyMMBName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println("Testing that we cannot call methods not in the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           "ModelMBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            boolean thisok = test(mbs, emptyMMBName, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (thisok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                System.out.println("...OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("TEST FAILED: Caught exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ModelMBeanOperationInfo[] opInfos = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            tralalaInfo, remACNLInfo, loadInfo, storeInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ModelMBeanInfo fullMMBI =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            new ModelMBeanInfoSupport(Resource.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                      "full descr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                      null, null, opInfos, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ModelMBean fullMMB = new RequiredModelMBean(fullMMBI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        fullMMB.setManagedResource(resource, "ObjectReference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ObjectName fullMMBName = new ObjectName("test:type=Full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        mbs.registerMBean(fullMMB, fullMMBName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        System.out.println("Testing that we can call methods in the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                           "ModelMBeanInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.out.println("  and that \"class\" or \"targetObject\" in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                           "descriptor directs methods to resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            boolean thisok = test(mbs, fullMMBName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (thisok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.out.println("...OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.out.println("TEST FAILED: Caught exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (!resource.loadCalled || !resource.storeCalled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                System.out.println("TEST FAILED: not called:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                   (resource.loadCalled ? "" : " load") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                   (resource.storeCalled ? "" : " store"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // Test the invoke("class.method") form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.out.println("Testing invoke(\"class.method\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            resource.loadCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            mbs.invoke(fullMMBName, Resource.class.getName() + ".load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                       null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (!resource.loadCalled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                System.out.println("TEST FAILED: load not called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                mbs.invoke(fullMMBName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                           RequiredModelMBean.class.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                           ".removeAttributeChangeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                           new Object[] {boringListener, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                           new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                   NotificationListener.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                   String.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                           });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                System.out.println("TEST FAILED: removeNotificationListener" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                   " returned successfully but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                   "should not have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                final Exception target = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (target instanceof ListenerNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    // OK: there is no such listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static boolean test(MBeanServer mbs, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                boolean shouldWork)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        final String[] names = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            "tralala",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            "removeAttributeChangeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            "load",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            "store",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            boolean thisok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    String tralala = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        mbs.invoke(name, names[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                   new Object[] {new Integer(5), resource},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                   new String[] {"int",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                                 Resource.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    if (!"tralala".equals(tralala)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        System.out.println("TEST FAILED: tralala returned: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                           tralala);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        mbs.invoke(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                   names[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                   new Object[] {boringListener, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                   new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                       NotificationListener.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                       String.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                   });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        System.out.println("TEST FAILED: " + names[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                           " returned successfully but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                           "should not have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        final Exception target = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        if (target instanceof ListenerNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            // OK: there is no such listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    mbs.invoke(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                               names[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                               new Object[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                               new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                thisok = shouldWork;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (!shouldWork) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    System.out.println("TEST FAILED: " + names[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                       " worked but should not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (shouldWork) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    System.out.println("TEST FAILED: " + names[i] + ": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    Exception target = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (!(target instanceof ServiceNotFoundException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        System.out.println("TEST FAILED: " + names[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                           ": wrong exception: " + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                System.out.println("TEST FAILED: " + names[i] + ": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (thisok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                System.out.println("OK: " + names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public static class Resource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public String tralala(int x, Resource y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (x != 5 || y != this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return "wrong params: " + x + " " + y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return "tralala";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        public void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            loadCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public void store() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            storeCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        boolean loadCalled, storeCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private static Resource resource = new Resource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static NotificationListener boringListener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}