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 /* 26 * @test 27 * @bug 4838640 4917194 28 * @summary test on add/remove NotificationListener 29 * @author Shanliang JIANG 30 * @modules java.management 31 * @run clean AddRemoveTest 32 * @run build AddRemoveTest 33 * @run main AddRemoveTest 34 */ 35 36 import java.net.MalformedURLException; 37 import java.io.IOException; 38 39 import javax.management.*; 40 import javax.management.remote.*; 41 42 public class AddRemoveTest { 43 private static final String[] protocols = {"rmi", "iiop", "jmxmp"}; 44 private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 45 46 public static void main(String[] args) { 47 System.out.println(">>> test on add/remove NotificationListener."); 48 49 boolean ok = true; 50 for (int i = 0; i < protocols.length; i++) { 51 try { 52 if (!test(protocols[i])) { 53 System.out.println(">>> Test failed for " + protocols[i]); 54 ok = false; 55 } else { 56 System.out.println(">>> Test successed for " + protocols[i]); 57 } 58 } catch (Exception e) { 59 System.out.println(">>> Test failed for " + protocols[i]); 60 e.printStackTrace(System.out); 61 ok = false; 62 } 63 } 64 65 if (ok) { 66 System.out.println(">>> Test passed"); 67 } else { 68 System.out.println(">>> TEST FAILED"); 69 System.exit(1); 70 } 71 } 72 73 private static boolean test(String proto) 74 throws Exception { 75 System.out.println(">>> Test for protocol " + proto); 76 JMXServiceURL u = new JMXServiceURL(proto, null, 0); 77 JMXConnectorServer server; 78 JMXServiceURL addr; 79 JMXConnector client; 80 MBeanServerConnection mserver; 81 82 final ObjectName delegateName = 83 new ObjectName("JMImplementation:type=MBeanServerDelegate"); 84 final NotificationListener dummyListener = new NotificationListener() { 85 public void handleNotification(Notification n, Object o) { 86 // do nothing 87 return; 88 } 89 }; 90 91 try { 92 // with a client listener, but close the server first 93 server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs); 94 server.start(); 95 96 addr = server.getAddress(); 97 client = JMXConnectorFactory.newJMXConnector(addr, null); 98 client.connect(null); 99 100 mserver = client.getMBeanServerConnection(); 101 String s1 = "1"; 102 String s2 = "2"; 103 String s3 = "3"; 104 105 for (int i=0; i<3; i++) { 106 mserver.addNotificationListener(delegateName, dummyListener, null, s1); 107 mserver.addNotificationListener(delegateName, dummyListener, null, s2); 108 mserver.addNotificationListener(delegateName, dummyListener, null, s3); 109 110 mserver.removeNotificationListener(delegateName, dummyListener, null, s3); 111 mserver.removeNotificationListener(delegateName, dummyListener, null, s2); 112 mserver.removeNotificationListener(delegateName, dummyListener, null, s1); 113 } 114 115 for (int i=0; i<3; i++) { 116 mserver.addNotificationListener(delegateName, dummyListener, null, s1); 117 mserver.addNotificationListener(delegateName, dummyListener, null, s2); 118 mserver.addNotificationListener(delegateName, dummyListener, null, s3); 119 120 mserver.removeNotificationListener(delegateName, dummyListener); 121 122 try { 123 mserver.removeNotificationListener(delegateName, dummyListener, null, s1); 124 System.out.println("Failed to remove a listener."); 125 126 // no expected exception 127 return false; 128 } catch (ListenerNotFoundException lne) { 129 // As expected. 130 } 131 } 132 client.close(); 133 134 server.stop(); 135 136 } catch (MalformedURLException e) { 137 System.out.println(">>> Skipping unsupported URL " + u); 138 return true; 139 } 140 141 return true; 142 } 143 }