jdk/test/javax/management/eventService/SubUnsubTest.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2008 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 SubUnsubTest
       
    26  * @bug 6736611
       
    27  * @summary Test not to remove other listeners when calling unsubscribe
       
    28  * @author Shanliang JIANG
       
    29  * @run clean SubUnsubTest
       
    30  * @run build SubUnsubTest
       
    31  * @run main SubUnsubTest
       
    32  */
       
    33 
       
    34 import java.lang.management.ManagementFactory;
       
    35 import javax.management.MBeanServer;
       
    36 import javax.management.Notification;
       
    37 import javax.management.NotificationFilter;
       
    38 import javax.management.NotificationBroadcasterSupport;
       
    39 import javax.management.NotificationListener;
       
    40 import javax.management.ObjectName;
       
    41 import javax.management.event.EventSubscriber;
       
    42 import javax.management.event.EventClient;
       
    43 public class SubUnsubTest {
       
    44     private static class CountListener implements NotificationListener {
       
    45         volatile int count;
       
    46 
       
    47         public void handleNotification(Notification n, Object h) {
       
    48             count++;
       
    49         }
       
    50     }
       
    51 
       
    52     public static interface SenderMBean {}
       
    53 
       
    54     public static class Sender extends NotificationBroadcasterSupport
       
    55             implements SenderMBean {
       
    56         void send() {
       
    57             Notification n = new Notification("type", this, 1L);
       
    58             sendNotification(n);
       
    59         }
       
    60     }
       
    61 
       
    62     public static void main(String[] args) throws Exception {
       
    63         System.out.println("Testing EventSubscriber-unsubscribe method.");
       
    64 
       
    65         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
       
    66         ObjectName name1 = new ObjectName("d:type=Sender,id=1");
       
    67         ObjectName name2 = new ObjectName("d:type=Sender,id=2");
       
    68         ObjectName pattern = new ObjectName("d:type=Sender,*");
       
    69         Sender sender1 = new Sender();
       
    70         Sender sender2 = new Sender();
       
    71         mbs.registerMBean(sender1, name1);
       
    72         mbs.registerMBean(sender2, name2);
       
    73 
       
    74         EventSubscriber sub = EventSubscriber.getEventSubscriber(mbs);
       
    75 
       
    76         System.out.println("Single subscribe covering both MBeans");
       
    77         CountListener listener = new CountListener();
       
    78 
       
    79         System.out.println("Subscribing and adding listeners ...");
       
    80         sub.subscribe(pattern, listener, null, null);
       
    81         sub.subscribe(name2, listener, null, null);
       
    82         mbs.addNotificationListener(name2, listener, null, null);
       
    83 
       
    84         sender1.send();
       
    85         sender2.send();
       
    86         if (listener.count != 4) {
       
    87             throw new RuntimeException("Do not receive all notifications: "+
       
    88                     "Expect 4, got "+listener.count);
       
    89         }
       
    90 
       
    91         System.out.println("Unsubscribe the listener with the pattern.");
       
    92         sub.unsubscribe(pattern, listener);
       
    93         listener.count = 0;
       
    94         sender1.send();
       
    95         sender2.send();
       
    96         if (listener.count != 2) {
       
    97             throw new RuntimeException("The method unsubscribe removes wrong listeners.");
       
    98         }
       
    99 
       
   100         System.out.println("Unsubscribe the listener with the ObjectName.");
       
   101         sub.unsubscribe(name2, listener);
       
   102         listener.count = 0;
       
   103         sender1.send();
       
   104         sender2.send();
       
   105         if (listener.count != 1) {
       
   106             throw new RuntimeException("The method unsubscribe removes wrong listeners.");
       
   107         }
       
   108 
       
   109         System.out.println("Subscribe twice to same MBean with same listener " +
       
   110                 "but different handback.");
       
   111         sub.subscribe(name1, listener, null, new Object());
       
   112         sub.subscribe(name1, listener, null, new Object());
       
   113         listener.count = 0;
       
   114 
       
   115         sub.unsubscribe(name1, listener);
       
   116         sender1.send();
       
   117         if (listener.count > 0) {
       
   118             throw new RuntimeException("EventSubscriber: the method unsubscribe" +
       
   119                     " does not remove a listener which was subscribed 2 times.");
       
   120         }
       
   121 
       
   122         System.out.println("Bye bye!");
       
   123         return;
       
   124     }
       
   125 }