jdk/test/javax/management/remote/mandatory/loading/MissingClassTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1004 5ba8217eb504
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 4915825 4921009 4934965 4977469
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests behavior when client or server gets object of unknown class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean MissingClassTest SingleClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build MissingClassTest SingleClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main MissingClassTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  Tests that clients and servers react correctly when they receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  objects of unknown classes.  This can happen easily due to version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  skew or missing jar files on one end or the other.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  behaviour of causing a connection to die because of the resultant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  IOException is not acceptable!  We try sending attributes and invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  parameters to the server of classes it doesn't know, and we try
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  sending attributes, exceptions and notifications to the client of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  classes it doesn't know.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  We also test objects that are of known class but not serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  The test cases are similar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.loading.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.rmi.RMIConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class MissingClassTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int NNOTIFS = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static ClassLoader clientLoader, serverLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static Object serverUnknown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Exception clientUnknown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static ObjectName on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final Object[] NO_OBJECTS = new Object[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final String[] NO_STRINGS = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final Object unserializableObject = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println("Test that the client or server end of a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                           "connection does not fail if sent an object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                           "it cannot deserialize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        on = new ObjectName("test:type=Test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ClassLoader testLoader = MissingClassTest.class.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        clientLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            new SingleClassLoader("$ServerUnknown$", HashMap.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                  testLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        serverLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            new SingleClassLoader("$ClientUnknown$", Exception.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                  testLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        serverUnknown =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            clientLoader.loadClass("$ServerUnknown$").newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        clientUnknown = (Exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            serverLoader.loadClass("$ClientUnknown$").newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final String[] protos = {"rmi", /*"iiop",*/ "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // iiop commented out until bug 4935098 is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i = 0; i < protos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ok &= test(protos[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                System.out.println("TEST FAILED WITH EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static boolean test(String proto) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("Testing for proto " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        mbs.createMBean(Test.class.getName(), on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        JMXConnectorServer cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        JMXServiceURL url = new JMXServiceURL(proto, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Map serverMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        serverMap.put(JMXConnectorServerFactory.DEFAULT_CLASS_LOADER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                      serverLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // make sure no auto-close at server side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        serverMap.put("jmx.remote.x.server.connection.timeout", "888888888");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            cs = JMXConnectorServerFactory.newJMXConnectorServer(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                                 serverMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                                 mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("System does not recognize URL: " + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                               "; ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Map clientMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        clientMap.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                      clientLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.out.println("Connecting for client-unknown test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        JMXConnector client = JMXConnectorFactory.connect(addr, clientMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // add a listener to verify no failed notif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        CNListener cnListener = new CNListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        client.addConnectionNotificationListener(cnListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.out.println("Getting attribute with class unknown to client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            Object result = mbsc.getAttribute(on, "ClientUnknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("TEST FAILS: getAttribute for class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                               "unknown to client should fail, returned: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                               result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (cause instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                System.out.println("Success: got an IOException wrapping " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                   "a ClassNotFoundException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                System.out.println("TEST FAILS: Caught IOException (" + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                   ") but cause should be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                   "ClassNotFoundException: " + cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Set names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println("Provoke exception of unknown class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            mbsc.invoke(on, "throwClientUnknown", NO_OBJECTS, NO_STRINGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.println("TEST FAILS: did not get exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            Throwable wrapped = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (wrapped instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                System.out.println("Success: got an IOException wrapping " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                   "a ClassNotFoundException: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                   wrapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                System.out.println("TEST FAILS: Got IOException but cause " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                   "should be ClassNotFoundException: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (wrapped == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    System.out.println("(null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    wrapped.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.out.println("TEST FAILS: Got wrong exception: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                               "should be IOException with cause " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                               "ClassNotFoundException:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        ok &= notifyTest(client, mbsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            boolean setAttribute = (i == 0); // else invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            String what = setAttribute ? "setAttribute" : "invoke";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            System.out.println("Trying " + what +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                               " with class unknown to server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (setAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    mbsc.setAttribute(on, new Attribute("ServerUnknown",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                                        serverUnknown));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    mbsc.invoke(on, "useServerUnknown",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                new Object[] {serverUnknown},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                new String[] {"java.lang.Object"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                System.out.println("TEST FAILS: " + what + " with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                   "class unknown to server should fail " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                   "but did not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (cause instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    System.out.println("Success: got an IOException " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                       "wrapping a ClassNotFoundException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    System.out.println("TEST FAILS: Caught IOException (" + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                       ") but cause should be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                       "ClassNotFoundException: " + cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    e.printStackTrace(System.out); // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.out.println("Trying to get unserializable attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            mbsc.getAttribute(on, "Unserializable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            System.out.println("TEST FAILS: get unserializable worked " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                               "but should not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.out.println("Success: got an IOException: " + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                               " (cause: " + e.getCause() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        System.out.println("Trying to set unserializable attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            Attribute attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                new Attribute("Unserializable", unserializableObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            mbsc.setAttribute(on, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            System.out.println("TEST FAILS: set unserializable worked " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                               "but should not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            System.out.println("Success: got an IOException: " + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                               " (cause: " + e.getCause() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        System.out.println("Doing queryNames to ensure connection alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        names = mbsc.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.out.println("queryNames returned " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        System.out.println("Trying to throw unserializable exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            mbsc.invoke(on, "throwUnserializable", NO_OBJECTS, NO_STRINGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            System.out.println("TEST FAILS: throw unserializable worked " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                               "but should not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            System.out.println("Success: got an IOException: " + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                               " (cause: " + e.getCause() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        client.removeConnectionNotificationListener(cnListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ok = ok && !cnListener.failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            System.out.println("Test passed for protocol " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private static class TestListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        TestListener(LostListener ll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            this.lostListener = ll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            /* Connectors can handle unserializable notifications in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
               one of two ways.  Either they can arrange for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
               client to get a NotSerializableException from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
               fetchNotifications call (RMI connector), or they can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
               replace the unserializable notification by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
               JMXConnectionNotification.NOTIFS_LOST (JMXMP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
               connector).  The former case is handled by code within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
               the connector client which will end up sending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
               NOTIFS_LOST to our LostListener.  The logic here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
               handles the latter case by converting it into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
               former.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (n instanceof JMXConnectionNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                && n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                lostListener.handleNotification(n, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            synchronized (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (!n.getType().equals("interesting")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    || !n.getUserData().equals("known")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    System.out.println("TestListener received strange notif: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                       + notificationString(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    result.failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    result.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    result.knownCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    if (result.knownCount == NNOTIFS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        result.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        private LostListener lostListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private static class LostListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            synchronized (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                handle(n, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        private void handle(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (!(n instanceof JMXConnectionNotification)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                System.out.println("LostListener received strange notif: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                   notificationString(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                result.failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                result.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            JMXConnectionNotification jn = (JMXConnectionNotification) n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (!jn.getType().equals(jn.NOTIFS_LOST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                System.out.println("Ignoring JMXConnectionNotification: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                   notificationString(jn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            final String msg = jn.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if ((!msg.startsWith("Dropped ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                 || !msg.endsWith("classes were missing locally"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                && (!msg.startsWith("Not serializable: "))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                System.out.println("Surprising NOTIFS_LOST getMessage: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                   msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (!(jn.getUserData() instanceof Long)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                System.out.println("JMXConnectionNotification userData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                   "not a Long: " + jn.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                result.failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                int lost = ((Long) jn.getUserData()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                result.lostCount += lost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (result.lostCount == NNOTIFS*2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    result.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private static class TestFilter implements NotificationFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        public boolean isNotificationEnabled(Notification n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return (n.getType().equals("interesting"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private static class Result {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int knownCount, lostCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        boolean failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private static Result result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /* Send a bunch of notifications to exercise the logic to recover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
       from unknown notification classes.  We have four kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
       notifications: "known" ones are of a class known to the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
       and which match its filters; "unknown" ones are of a class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
       match the client's filters but that the client can't load;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
       "tricky" ones are unserializable; and "boring" notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
       are of a class that the client knows but that doesn't match its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
       filters.  We emit NNOTIFS notifications of each kind.  We do a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
       random shuffle on these 4*NNOTIFS notifications so it is likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
       that we will cover the various different cases in the logic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       Specifically, what we are testing here is the logic that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
       recovers from a fetchNotifications request that gets a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
       ClassNotFoundException.  Because the request can contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
       several notifications, the client doesn't know which of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
       generated the exception.  So it redoes a request that asks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
       just one notification.  We need to be sure that this works when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
       that one notification is of an unknown class and when it is of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
       a known class, and in both cases when there are filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
       notifications that are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
       We are also testing the behaviour in the server when it tries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
       to include an unserializable notification in the response to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
       fetchNotifications, and in the client when that happens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
       If the test succeeds, the listener should receive the NNOTIFS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
       "known" notifications, and the connection listener should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
       receive an indication of NNOTIFS lost notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
       representing the "unknown" notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
       We depend on some implementation-specific features here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
       1. The buffer size is sufficient to contain the 4*NNOTIFS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
       notifications which are all sent at once, before the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
       gets a chance to start receiving them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
       2. When one or more notifications are dropped because they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
       of unknown classes, the NOTIFS_LOST notification contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
       userData that is a Long with a count of the number dropped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
       3. If a notification is not serializable on the server, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
       client gets told about it somehow, rather than having it just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
       dropped on the floor.  The somehow might be through an RMI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
       exception, or it might be by the server replacing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
       unserializable notif by a JMXConnectionNotification.NOTIFS_LOST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private static boolean notifyTest(JMXConnector client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                      MBeanServerConnection mbsc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        System.out.println("Send notifications including unknown ones");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        result = new Result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        LostListener ll = new LostListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        client.addConnectionNotificationListener(ll, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        TestListener nl = new TestListener(ll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        mbsc.addNotificationListener(on, nl, new TestFilter(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        mbsc.invoke(on, "sendNotifs", NO_OBJECTS, NO_STRINGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // wait for the listeners to receive all their notifs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // or to fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        long deadline = System.currentTimeMillis() + 60000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        long remain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        while ((remain = deadline - System.currentTimeMillis()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            synchronized (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (result.failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    || (result.knownCount == NNOTIFS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        && result.lostCount == NNOTIFS*2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                result.wait(remain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (result.failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            System.out.println("TEST FAILS: Notification strangeness");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } else if (result.knownCount == NNOTIFS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                   && result.lostCount == NNOTIFS*2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            System.out.println("Success: received known notifications and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                               "got NOTIFS_LOST for unknown and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                               "unserializable ones");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            System.out.println("TEST FAILS: Timed out without receiving " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                               "all notifs: known=" + result.knownCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                               "; lost=" + result.lostCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public static interface TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        public Object getClientUnknown() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        public void throwClientUnknown() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        public void setServerUnknown(Object o) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        public void useServerUnknown(Object o) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        public Object getUnserializable() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        public void setUnserializable(Object un) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        public void throwUnserializable() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public void sendNotifs() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public static class Test extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            implements TestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        public Object getClientUnknown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return clientUnknown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        public void throwClientUnknown() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            throw clientUnknown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        public void setServerUnknown(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new IllegalArgumentException("setServerUnknown succeeded "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                               "but should not have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        public void useServerUnknown(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            throw new IllegalArgumentException("useServerUnknown succeeded "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                               "but should not have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public Object getUnserializable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return unserializableObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        public void setUnserializable(Object un) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            throw new IllegalArgumentException("setUnserializable succeeded " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                               "but should not have");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public void throwUnserializable() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new Exception() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                private Object unserializable = unserializableObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        public void sendNotifs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            /* We actually send the same four notification objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
               NNOTIFS times each.  This doesn't particularly matter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
               but note that the MBeanServer will replace "this" by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
               the sender's ObjectName the first time.  Since that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
               always the same, no problem.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            Notification known =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                new Notification("interesting", this, 1L, 1L, "known");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            known.setUserData("known");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            Notification unknown =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                new Notification("interesting", this, 1L, 1L, "unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            unknown.setUserData(clientUnknown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            Notification boring =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                new Notification("boring", this, 1L, 1L, "boring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            Notification tricky =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                new Notification("interesting", this, 1L, 1L, "tricky");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            tricky.setUserData(unserializableObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // check that the tricky notif is indeed unserializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                new ObjectOutputStream(new ByteArrayOutputStream())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    .writeObject(tricky);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                System.out.println("TEST INCORRECT: tricky notif is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                   "serializable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            } catch (NotSerializableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // OK: tricky notif is not serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                System.out.println("TEST INCORRECT: tricky notif " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                   "serialization check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            /* Now shuffle an imaginary deck of cards where K, U, T, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
               B (known, unknown, tricky, boring) each appear NNOTIFS times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
               We explicitly seed the random number generator so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
               can reproduce rare test failures if necessary.  We only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
               use a StringBuffer so we can print the shuffled deck --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
               otherwise we could just emit the notifications as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
               cards are placed.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            long seed = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            System.out.println("Random number seed is " + seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            Random r = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            int knownCount = NNOTIFS;   // remaining K cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            int unknownCount = NNOTIFS; // remaining U cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            int trickyCount = NNOTIFS;  // remaining T cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            int boringCount = NNOTIFS;  // remaining B cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            StringBuffer notifList = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            for (int i = NNOTIFS * 4; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                int rand = r.nextInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // use rand to pick a card from the remaining ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if ((rand -= knownCount) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    notifList.append('k');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    knownCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                } else if ((rand -= unknownCount) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    notifList.append('u');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    unknownCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                } else if ((rand -= trickyCount) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    notifList.append('t');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    trickyCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    notifList.append('b');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    boringCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (knownCount != 0 || unknownCount != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                || trickyCount != 0 || boringCount != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                System.out.println("TEST INCORRECT: Shuffle failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                   "known=" + knownCount +" unknown=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                   unknownCount + " tricky=" + trickyCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                   " boring=" + boringCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                   " deal=" + notifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            String notifs = notifList.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            System.out.println("Shuffle: " + notifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            for (int i = 0; i < NNOTIFS * 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                Notification n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                switch (notifs.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                case 'k': n = known; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                case 'u': n = unknown; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                case 't': n = tricky; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                case 'b': n = boring; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    System.out.println("TEST INCORRECT: Bad shuffle char: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                       notifs.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                sendNotification(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    private static String notificationString(Notification n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return n.getClass().getName() + "/" + n.getType() + " \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            n.getMessage() + "\" <" + n.getUserData() + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private static class CNListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        public void handleNotification(Notification n, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (n instanceof JMXConnectionNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                JMXConnectionNotification jn = (JMXConnectionNotification)n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        public boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
}