J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-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 |
| 26 | * @bug 4934236 |
| 27 | * @summary Tests that NotificationBuffer is created when used. |
| 28 | * @author jfd@... |
| 29 | * @run clean NotificationBufferCreationTest NotificationSender |
| 30 | * @run build NotificationBufferCreationTest |
| 31 | * @run main NotificationBufferCreationTest |
| 32 | */ |
| 33 | import java.net.MalformedURLException; |
| 34 | |
| 35 | import javax.management.MBeanServerFactory; |
| 36 | import javax.management.MBeanServer; |
| 37 | import javax.management.MBeanServerConnection; |
| 38 | import javax.management.NotificationListener; |
| 39 | import javax.management.ObjectName; |
| 40 | import javax.management.Notification; |
| 41 | |
| 42 | import javax.management.remote.JMXConnector; |
| 43 | import javax.management.remote.JMXConnectorFactory; |
| 44 | import javax.management.remote.JMXConnectorServer; |
| 45 | import javax.management.remote.JMXConnectorServerFactory; |
| 46 | import javax.management.remote.JMXServiceURL; |
| 47 | |
| 48 | public class NotificationBufferCreationTest { |
| 49 | private static final MBeanServer mbs = |
| 50 | MBeanServerFactory.createMBeanServer(); |
| 51 | private static final String[] protocols = {"rmi", "iiop", "jmxmp"}; |
| 52 | public static void main(String[] args) { |
| 53 | try { |
| 54 | boolean error = false; |
| 55 | ObjectName notifierName = |
| 56 | new ObjectName("TestDomain:type=NotificationSender"); |
| 57 | |
| 58 | NotificationSender s = new NotificationSender(); |
| 59 | mbs.registerMBean(s, notifierName); |
| 60 | |
| 61 | for(int i = 0; i < protocols.length; i++) { |
| 62 | try { |
| 63 | System.out.println("dotest for " + protocols[i]); |
| 64 | dotest(protocols[i], s, notifierName); |
| 65 | }catch(Exception e) { |
| 66 | e.printStackTrace(); |
| 67 | error = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if(error) |
| 72 | System.exit(1); |
| 73 | |
| 74 | System.out.println("Test OK"); |
| 75 | |
| 76 | }catch(Exception e) { |
| 77 | e.printStackTrace(); |
| 78 | System.exit(1); |
| 79 | } |
| 80 | } |
| 81 | private static void dotest(String protocol, |
| 82 | NotificationSender s, |
| 83 | ObjectName notifierName) throws Exception { |
| 84 | JMXConnector client = null; |
| 85 | JMXConnectorServer server = null; |
| 86 | JMXServiceURL u = null; |
| 87 | try { |
| 88 | u = new JMXServiceURL(protocol, null, 0); |
| 89 | server = |
| 90 | JMXConnectorServerFactory.newJMXConnectorServer(u, |
| 91 | null, |
| 92 | mbs); |
| 93 | checkNotifier(s, 0, "new ConnectorServer"); |
| 94 | |
| 95 | server.start(); |
| 96 | |
| 97 | checkNotifier(s, 0, "ConnectorServer start"); |
| 98 | |
| 99 | JMXServiceURL addr = server.getAddress(); |
| 100 | client = JMXConnectorFactory.newJMXConnector(addr, null); |
| 101 | |
| 102 | checkNotifier(s, 0, "new Connector"); |
| 103 | |
| 104 | client.connect(null); |
| 105 | |
| 106 | checkNotifier(s, 0, "Connector connect"); |
| 107 | |
| 108 | MBeanServerConnection mbsc = client.getMBeanServerConnection(); |
| 109 | |
| 110 | final NotificationListener dummyListener = |
| 111 | new NotificationListener() { |
| 112 | public void handleNotification(Notification n, |
| 113 | Object o) { |
| 114 | // do nothing |
| 115 | return; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | mbsc.addNotificationListener(notifierName, |
| 120 | dummyListener, |
| 121 | null, |
| 122 | null); |
| 123 | |
| 124 | // 1 Listener is expected to be added by the ServerNotifForwader |
| 125 | checkNotifier(s, 1, "addNotificationListener"); |
| 126 | |
| 127 | mbsc.removeNotificationListener(notifierName, |
| 128 | dummyListener); |
| 129 | System.out.println("Test OK for " + protocol); |
| 130 | }catch(MalformedURLException e) { |
| 131 | System.out.println("Skipping URL " + u); |
| 132 | } |
| 133 | finally { |
| 134 | if(client != null) |
| 135 | client.close(); |
| 136 | if(server != null) |
| 137 | server.stop(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private static void checkNotifier(NotificationSender s, |
| 142 | int expectedListenerCount, |
| 143 | String msg) throws Exception { |
| 144 | int count = s.getListenerCount(); |
| 145 | if(count != expectedListenerCount) { |
| 146 | String errorMsg = "Invalid expected listener count [" + |
| 147 | expectedListenerCount + "], real [" + count +"] for " + msg; |
| 148 | System.out.println(errorMsg); |
| 149 | throw new Exception(errorMsg); |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | } |