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