jdk/test/javax/management/eventService/EventManagerTest.java
changeset 1004 5ba8217eb504
child 1247 b4c26443dee5
equal deleted inserted replaced
1003:b2f6b7e00c29 1004:5ba8217eb504
       
     1 /*
       
     2  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test EventManagerTest.java 1.8 08/01/22
       
    26  * @bug 5108776
       
    27  * @summary Basic test for EventManager.
       
    28  * @author Shanliang JIANG
       
    29  * @run clean EventManagerTest
       
    30  * @run build EventManagerTest
       
    31  * @run main EventManagerTest
       
    32  */
       
    33 
       
    34 import javax.management.MBeanNotificationInfo;
       
    35 import javax.management.MBeanServer;
       
    36 import javax.management.MBeanServerConnection;
       
    37 import javax.management.MBeanServerFactory;
       
    38 import javax.management.Notification;
       
    39 import javax.management.NotificationBroadcasterSupport;
       
    40 import javax.management.NotificationListener;
       
    41 import javax.management.ObjectName;
       
    42 import javax.management.event.*;
       
    43 import javax.management.remote.JMXConnector;
       
    44 import javax.management.remote.JMXConnectorFactory;
       
    45 import javax.management.remote.JMXConnectorServer;
       
    46 import javax.management.remote.JMXConnectorServerFactory;
       
    47 import javax.management.remote.JMXServiceURL;
       
    48 
       
    49 /**
       
    50  *
       
    51  */
       
    52 public class EventManagerTest {
       
    53     private static MBeanServer mbeanServer;
       
    54     private static ObjectName emitter;
       
    55     private static JMXServiceURL url;
       
    56     private static JMXConnectorServer server;
       
    57     private static JMXConnector conn;
       
    58     private static MBeanServerConnection client;
       
    59 
       
    60     /**
       
    61      * @param args the command line arguments
       
    62      */
       
    63     public static void main(String[] args) throws Exception {
       
    64         System.out.println(">>> EventManagerTest-main basic tests ...");
       
    65         mbeanServer = MBeanServerFactory.createMBeanServer();
       
    66 
       
    67         // for 1.5
       
    68         if (System.getProperty("java.version").startsWith("1.5") &&
       
    69                 !mbeanServer.isRegistered(EventClientDelegateMBean.OBJECT_NAME)) {
       
    70             System.out.print("Working on "+System.getProperty("java.version")+
       
    71                     " register "+EventClientDelegateMBean.OBJECT_NAME);
       
    72 
       
    73             mbeanServer.registerMBean(EventClientDelegate.
       
    74                     getEventClientDelegate(mbeanServer),
       
    75                     EventClientDelegateMBean.OBJECT_NAME);
       
    76         }
       
    77 
       
    78         emitter = new ObjectName("Default:name=NotificationEmitter");
       
    79 
       
    80         url = new JMXServiceURL("rmi", null, 0) ;
       
    81         server =
       
    82                 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
       
    83         server.start();
       
    84 
       
    85         url = server.getAddress();
       
    86         conn = JMXConnectorFactory.connect(url, null);
       
    87         client = conn.getMBeanServerConnection();
       
    88 
       
    89         mbeanServer.registerMBean(new NotificationEmitter(), emitter);
       
    90 
       
    91         boolean succeed;
       
    92 
       
    93         System.out.println(">>> EventManagerTest-main: using the fetching EventRelay...");
       
    94         succeed = test(new EventClient(client));
       
    95 
       
    96         System.out.println(">>> EventManagerTest-main: using the pushing EventRelay...");
       
    97         EventClientDelegateMBean ecd = EventClientDelegate.getProxy(client);
       
    98         succeed &= test(new EventClient(ecd,
       
    99                 new RMIPushEventRelay(ecd),
       
   100                 null, null,
       
   101                 EventClient.DEFAULT_LEASE_TIMEOUT));
       
   102 
       
   103         conn.close();
       
   104         server.stop();
       
   105 
       
   106         if (succeed) {
       
   107             System.out.println(">>> EventManagerTest-main: PASSE!");
       
   108         } else {
       
   109             System.out.println("\n>>> EventManagerTest-main: FAILED!");
       
   110             System.exit(1);
       
   111         }
       
   112     }
       
   113 
       
   114     public static boolean test(EventClient efClient) throws Exception {
       
   115         // add listener from the client side
       
   116         Listener listener = new Listener();
       
   117         efClient.subscribe(emitter, listener, null, null);
       
   118 
       
   119         // ask to send notifs
       
   120         Object[] params = new Object[] {new Integer(sendNB)};
       
   121         String[] signatures = new String[] {"java.lang.Integer"};
       
   122         client.invoke(emitter, "sendNotifications", params, signatures);
       
   123 
       
   124         // waiting
       
   125         long toWait = 6000;
       
   126         long stopTime = System.currentTimeMillis() + toWait;
       
   127 
       
   128         synchronized(listener) {
       
   129             while(listener.received < sendNB && toWait > 0) {
       
   130                 listener.wait(toWait);
       
   131                 toWait = stopTime - System.currentTimeMillis();
       
   132             }
       
   133         }
       
   134 
       
   135         // clean
       
   136         System.out.println(">>> EventManagerTest-test: cleaning...");
       
   137         efClient.unsubscribe(emitter, listener);
       
   138         efClient.close();
       
   139 
       
   140         if (listener.received != sendNB) {
       
   141             System.out.println(">>> EventManagerTest-test: FAILED! Expected to receive "+sendNB+", but got "+listener.received);
       
   142 
       
   143             return false;
       
   144         } else if (listener.seqErr > 0) {
       
   145             System.out.println(">>> EventManagerTest-test: FAILED! The receiving sequence is not correct.");
       
   146 
       
   147             return false;
       
   148         } else {
       
   149             System.out.println(">>> EventManagerTest-test: got all expected "+listener.received);
       
   150             return true;
       
   151         }
       
   152     }
       
   153 
       
   154     private static class Listener implements NotificationListener {
       
   155         public int received = 0;
       
   156         public int seqErr = 0;
       
   157 
       
   158         private long lastSeq = -1;
       
   159 
       
   160         public void handleNotification(Notification notif, Object handback) {
       
   161             if (!myType.equals(notif.getType())) {
       
   162                 System.out.println(">>> EventManagerTest-Listener: got unexpected notif: "+notif);
       
   163                 System.exit(1);
       
   164             }
       
   165 
       
   166             if (lastSeq == -1) {
       
   167                 lastSeq = notif.getSequenceNumber();
       
   168             } else if (notif.getSequenceNumber() - lastSeq++ != 1) {
       
   169                 seqErr++;
       
   170             }
       
   171 
       
   172             //System.out.println(">>> EventManagerTest-Listener: got notif "+notif.getSequenceNumber());
       
   173 
       
   174             synchronized(this) {
       
   175                 if (++received >= sendNB) {
       
   176                     this.notify();
       
   177                 }
       
   178             }
       
   179         }
       
   180     }
       
   181 
       
   182     public static class NotificationEmitter extends NotificationBroadcasterSupport
       
   183             implements NotificationEmitterMBean {
       
   184 
       
   185         public MBeanNotificationInfo[] getNotificationInfo() {
       
   186             final String[] ntfTypes = {myType};
       
   187 
       
   188             final MBeanNotificationInfo[] ntfInfoArray  = {
       
   189                 new MBeanNotificationInfo(ntfTypes,
       
   190                         "javax.management.Notification",
       
   191                         "Notifications sent by the NotificationEmitter")};
       
   192 
       
   193                         return ntfInfoArray;
       
   194         }
       
   195 
       
   196         /**
       
   197          * Send Notification objects.
       
   198          *
       
   199          * @param nb The number of notifications to send
       
   200          */
       
   201         public void sendNotifications(Integer nb) {
       
   202             Notification notif;
       
   203             for (int i=1; i<=nb.intValue(); i++) {
       
   204                 notif = new Notification(myType, this, count++);
       
   205                 notif.setUserData("jsl");
       
   206                 //System.out.println(">>> EventManagerService-NotificationEmitter-sendNotifications: "+i);
       
   207 
       
   208                 sendNotification(notif);
       
   209             }
       
   210         }
       
   211     }
       
   212 
       
   213     public interface NotificationEmitterMBean {
       
   214         public void sendNotifications(Integer nb);
       
   215     }
       
   216 
       
   217     private static int sendNB = 120;
       
   218     private static long count = 0;
       
   219 
       
   220     private static final String myType = "notification.my_notification";
       
   221 }