Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/AddRemoveTest.java
41155 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @bug 4838640 491719427* @summary test on add/remove NotificationListener28* @author Shanliang JIANG29*30* @run clean AddRemoveTest31* @run build AddRemoveTest32* @run main AddRemoveTest33*/3435import java.net.MalformedURLException;36import java.io.IOException;3738import javax.management.*;39import javax.management.remote.*;4041public class AddRemoveTest {42private static final String[] protocols = {"rmi", "iiop", "jmxmp"};43private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4445public static void main(String[] args) {46System.out.println(">>> test on add/remove NotificationListener.");4748boolean ok = true;49for (int i = 0; i < protocols.length; i++) {50try {51if (!test(protocols[i])) {52System.out.println(">>> Test failed for " + protocols[i]);53ok = false;54} else {55System.out.println(">>> Test successed for " + protocols[i]);56}57} catch (Exception e) {58System.out.println(">>> Test failed for " + protocols[i]);59e.printStackTrace(System.out);60ok = false;61}62}6364if (ok) {65System.out.println(">>> Test passed");66} else {67System.out.println(">>> TEST FAILED");68System.exit(1);69}70}7172private static boolean test(String proto)73throws Exception {74System.out.println(">>> Test for protocol " + proto);75JMXServiceURL u = new JMXServiceURL(proto, null, 0);76JMXConnectorServer server;77JMXServiceURL addr;78JMXConnector client;79MBeanServerConnection mserver;8081final ObjectName delegateName =82new ObjectName("JMImplementation:type=MBeanServerDelegate");83final NotificationListener dummyListener = new NotificationListener() {84public void handleNotification(Notification n, Object o) {85// do nothing86return;87}88};8990try {91// with a client listener, but close the server first92server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);93server.start();9495addr = server.getAddress();96client = JMXConnectorFactory.newJMXConnector(addr, null);97client.connect(null);9899mserver = client.getMBeanServerConnection();100String s1 = "1";101String s2 = "2";102String s3 = "3";103104for (int i=0; i<3; i++) {105mserver.addNotificationListener(delegateName, dummyListener, null, s1);106mserver.addNotificationListener(delegateName, dummyListener, null, s2);107mserver.addNotificationListener(delegateName, dummyListener, null, s3);108109mserver.removeNotificationListener(delegateName, dummyListener, null, s3);110mserver.removeNotificationListener(delegateName, dummyListener, null, s2);111mserver.removeNotificationListener(delegateName, dummyListener, null, s1);112}113114for (int i=0; i<3; i++) {115mserver.addNotificationListener(delegateName, dummyListener, null, s1);116mserver.addNotificationListener(delegateName, dummyListener, null, s2);117mserver.addNotificationListener(delegateName, dummyListener, null, s3);118119mserver.removeNotificationListener(delegateName, dummyListener);120121try {122mserver.removeNotificationListener(delegateName, dummyListener, null, s1);123System.out.println("Failed to remove a listener.");124125// no expected exception126return false;127} catch (ListenerNotFoundException lne) {128// As expected.129}130}131client.close();132133server.stop();134135} catch (MalformedURLException e) {136System.out.println(">>> Skipping unsupported URL " + u);137return true;138}139140return true;141}142}143144145