Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/AddRemoveTest.java
38867 views
/*1* Copyright (c) 2003, 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* @run clean AddRemoveTest30* @run build AddRemoveTest31* @run main AddRemoveTest32*/3334import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;3940public class AddRemoveTest {41private static final String[] protocols = {"rmi", "iiop", "jmxmp"};42private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4344public static void main(String[] args) {45System.out.println(">>> test on add/remove NotificationListener.");4647boolean ok = true;48for (int i = 0; i < protocols.length; i++) {49try {50if (!test(protocols[i])) {51System.out.println(">>> Test failed for " + protocols[i]);52ok = false;53} else {54System.out.println(">>> Test successed for " + protocols[i]);55}56} catch (Exception e) {57System.out.println(">>> Test failed for " + protocols[i]);58e.printStackTrace(System.out);59ok = false;60}61}6263if (ok) {64System.out.println(">>> Test passed");65} else {66System.out.println(">>> TEST FAILED");67System.exit(1);68}69}7071private static boolean test(String proto)72throws Exception {73System.out.println(">>> Test for protocol " + proto);74JMXServiceURL u = new JMXServiceURL(proto, null, 0);75JMXConnectorServer server;76JMXServiceURL addr;77JMXConnector client;78MBeanServerConnection mserver;7980final ObjectName delegateName =81new ObjectName("JMImplementation:type=MBeanServerDelegate");82final NotificationListener dummyListener = new NotificationListener() {83public void handleNotification(Notification n, Object o) {84// do nothing85return;86}87};8889try {90// with a client listener, but close the server first91server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);92server.start();9394addr = server.getAddress();95client = JMXConnectorFactory.newJMXConnector(addr, null);96client.connect(null);9798mserver = client.getMBeanServerConnection();99String s1 = "1";100String s2 = "2";101String s3 = "3";102103for (int i=0; i<3; i++) {104mserver.addNotificationListener(delegateName, dummyListener, null, s1);105mserver.addNotificationListener(delegateName, dummyListener, null, s2);106mserver.addNotificationListener(delegateName, dummyListener, null, s3);107108mserver.removeNotificationListener(delegateName, dummyListener, null, s3);109mserver.removeNotificationListener(delegateName, dummyListener, null, s2);110mserver.removeNotificationListener(delegateName, dummyListener, null, s1);111}112113for (int i=0; i<3; i++) {114mserver.addNotificationListener(delegateName, dummyListener, null, s1);115mserver.addNotificationListener(delegateName, dummyListener, null, s2);116mserver.addNotificationListener(delegateName, dummyListener, null, s3);117118mserver.removeNotificationListener(delegateName, dummyListener);119120try {121mserver.removeNotificationListener(delegateName, dummyListener, null, s1);122System.out.println("Failed to remove a listener.");123124// no expected exception125return false;126} catch (ListenerNotFoundException lne) {127// As expected.128}129}130client.close();131132server.stop();133134} catch (MalformedURLException e) {135System.out.println(">>> Skipping unsupported URL " + u);136return true;137}138139return true;140}141}142143144