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