Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/ConcurrentModificationTest.java
38867 views
/*1* Copyright (c) 2012, 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*/2223/*24* @test25* @bug 712036526* @summary test on Concurrent Modification27* @author Shanliang JIANG28* @run main ConcurrentModificationTest29*/3031import java.net.MalformedURLException;32import java.util.ConcurrentModificationException;33import javax.management.MBeanServer;34import javax.management.MBeanServerConnection;35import javax.management.MBeanServerFactory;36import javax.management.Notification;37import javax.management.NotificationListener;38import javax.management.ObjectName;39import javax.management.remote.JMXConnector;40import javax.management.remote.JMXConnectorFactory;41import javax.management.remote.JMXConnectorServer;42import javax.management.remote.JMXConnectorServerFactory;43import javax.management.remote.JMXServiceURL;4445/**46*47*/48public class ConcurrentModificationTest {49private static final String[] protocols = {"rmi", "iiop", "jmxmp"};50private static int number = 100;5152private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();53private static ObjectName delegateName;54private static ObjectName[] timerNames = new ObjectName[number];55private static NotificationListener[] listeners = new NotificationListener[number];5657private static Throwable uncaughtException = null;5859public static void main(String[] args) throws Exception {60System.out.println(">>> test on Concurrent Modification.");6162Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {63@Override64public void uncaughtException(Thread t, Throwable e) {65e.printStackTrace();66if (e instanceof ConcurrentModificationException) {67uncaughtException = e;68}69}70});7172delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");73for (int i=0; i<number; i++) {74timerNames[i] = new ObjectName("MBean:name=Timer"+i);75listeners[i] = new NotificationListener() {76@Override77public void handleNotification(Notification notification, Object handback) {78// nothing79}80};81}82String errors = "";8384for (int i = 0; i < protocols.length; i++) {85uncaughtException = null;86System.out.println(">>> Test for protocol " + protocols[i]);87test(protocols[i]);88if (uncaughtException != null) {89if ("".equals(errors)) {90errors = "Failed to " + protocols[i] + ": "+uncaughtException;91} else {92errors = errors+", failed to " + protocols[i] + ": "+uncaughtException;93}94System.out.println(">>> FAILED for protocol " + protocols[i]);95} else {96System.out.println(">>> PASSED for protocol " + protocols[i]);97}98}99100if ("".equals(errors)) {101System.out.println("All Passed!");102} else {103System.out.println("!!!!!! Failed.");104105throw new RuntimeException(errors);106}107}108109private static void test(String proto) throws Exception {110JMXServiceURL u = new JMXServiceURL(proto, null, 0);111JMXConnectorServer server;112JMXConnector client;113114try {115server =116JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);117server.start();118JMXServiceURL addr = server.getAddress();119client = JMXConnectorFactory.connect(addr, null);120} catch (MalformedURLException e) {121System.out.println(">>> not support: " + proto);122return;123}124125final MBeanServerConnection mserver = client.getMBeanServerConnection();126127int count = 0;128boolean adding = true;129while (uncaughtException == null && count++ < 10) {130for (int i = 0; i < number; i++) {131listenerOp(mserver, listeners[i], adding);132mbeanOp(mserver, timerNames[i], adding);133}134adding = !adding;135}136137if (uncaughtException != null) { // clean138for (int i = 0; i < number; i++) {139try {140mbeanOp(mserver, timerNames[i], false);141} catch (Exception e) {142}143}144}145client.close();146server.stop();147}148149private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)150throws Exception {151if (adding) {152mserver.createMBean("javax.management.timer.Timer", name);153} else {154mserver.unregisterMBean(name);155}156}157158private static void listenerOp(MBeanServerConnection mserver, NotificationListener listener, boolean adding)159throws Exception {160if (adding) {161mserver.addNotificationListener(delegateName, listener, null, null);162} else {163mserver.removeNotificationListener(delegateName, listener);164}165}166}167168