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