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 |
|
26 * @bug 5108776 |
|
27 * @summary Test that EventSubscriber.subscribe works |
|
28 * @author Eamonn McManus |
|
29 */ |
|
30 |
|
31 import java.lang.management.ManagementFactory; |
|
32 import javax.management.MBeanServer; |
|
33 import javax.management.Notification; |
|
34 import javax.management.NotificationBroadcasterSupport; |
|
35 import javax.management.NotificationFilter; |
|
36 import javax.management.NotificationListener; |
|
37 import javax.management.ObjectName; |
|
38 import javax.management.event.EventSubscriber; |
|
39 |
|
40 public class SubscribeTest { |
|
41 private static class CountListener implements NotificationListener { |
|
42 volatile int count; |
|
43 |
|
44 public void handleNotification(Notification n, Object h) { |
|
45 count++; |
|
46 } |
|
47 } |
|
48 |
|
49 private static class SwitchFilter implements NotificationFilter { |
|
50 volatile boolean enabled; |
|
51 |
|
52 public boolean isNotificationEnabled(Notification n) { |
|
53 return enabled; |
|
54 } |
|
55 } |
|
56 |
|
57 public static interface SenderMBean {} |
|
58 |
|
59 public static class Sender extends NotificationBroadcasterSupport |
|
60 implements SenderMBean { |
|
61 void send() { |
|
62 Notification n = new Notification("type", this, 1L); |
|
63 sendNotification(n); |
|
64 } |
|
65 } |
|
66 |
|
67 public static void main(String[] args) throws Exception { |
|
68 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
|
69 ObjectName name1 = new ObjectName("d:type=Sender,id=1"); |
|
70 ObjectName name2 = new ObjectName("d:type=Sender,id=2"); |
|
71 ObjectName pattern = new ObjectName("d:type=Sender,*"); |
|
72 Sender sender1 = new Sender(); |
|
73 Sender sender2 = new Sender(); |
|
74 mbs.registerMBean(sender1, name1); |
|
75 mbs.registerMBean(sender2, name2); |
|
76 |
|
77 EventSubscriber sub = EventSubscriber.getEventSubscriber(mbs); |
|
78 |
|
79 System.out.println("Single subscribe covering both MBeans"); |
|
80 CountListener listen1 = new CountListener(); |
|
81 sub.subscribe(pattern, listen1, null, null); |
|
82 sender1.send(); |
|
83 assertEquals("Notifs after sender1 send", 1, listen1.count); |
|
84 sender2.send(); |
|
85 assertEquals("Notifs after sender2 send", 2, listen1.count); |
|
86 |
|
87 System.out.println("Unsubscribe"); |
|
88 sub.unsubscribe(pattern, listen1); |
|
89 sender1.send(); |
|
90 assertEquals("Notifs after sender1 send", 2, listen1.count); |
|
91 |
|
92 System.out.println("Subscribe twice to same MBean with same listener " + |
|
93 "but different filters"); |
|
94 SwitchFilter filter1 = new SwitchFilter(); |
|
95 sub.subscribe(name1, listen1, null, null); |
|
96 sub.subscribe(name1, listen1, filter1, null); |
|
97 listen1.count = 0; |
|
98 sender1.send(); |
|
99 // switch is off, so only one notif expected |
|
100 assertEquals("Notifs after sender1 send", 1, listen1.count); |
|
101 filter1.enabled = true; |
|
102 sender1.send(); |
|
103 // switch is on, so two more notifs expected |
|
104 assertEquals("Notifs after sender1 send", 3, listen1.count); |
|
105 |
|
106 System.out.println("Remove those subscriptions"); |
|
107 sub.unsubscribe(name1, listen1); |
|
108 sender1.send(); |
|
109 assertEquals("Notifs after sender1 send", 3, listen1.count); |
|
110 } |
|
111 |
|
112 private static void assertEquals(String what, Object expected, Object actual) |
|
113 throws Exception { |
|
114 if (!equal(expected, actual)) { |
|
115 String msg = "Expected " + expected + "; got " + actual; |
|
116 throw new Exception("TEST FAILED: " + what + ": " + msg); |
|
117 } |
|
118 } |
|
119 |
|
120 private static boolean equal(Object x, Object y) { |
|
121 if (x == y) |
|
122 return true; |
|
123 if (x == null) |
|
124 return false; |
|
125 return (x.equals(y)); |
|
126 } |
|
127 } |
|