Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/ConcurrentModificationTest.java
41155 views
/*1* Copyright (c) 2012, 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*/2223/*24* @test25* @bug 712036526* @summary test on Concurrent Modification27* @author Shanliang JIANG28*29* @run main ConcurrentModificationTest30*/3132import java.net.MalformedURLException;33import java.util.ConcurrentModificationException;34import javax.management.MBeanServer;35import javax.management.MBeanServerConnection;36import javax.management.MBeanServerFactory;37import javax.management.Notification;38import javax.management.NotificationListener;39import javax.management.ObjectName;40import javax.management.remote.JMXConnector;41import javax.management.remote.JMXConnectorFactory;42import javax.management.remote.JMXConnectorServer;43import javax.management.remote.JMXConnectorServerFactory;44import javax.management.remote.JMXServiceURL;4546/**47*48*/49public class ConcurrentModificationTest {50private static final String[] protocols = {"rmi", "iiop", "jmxmp"};51private static int number = 100;5253private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();54private static ObjectName delegateName;55private static ObjectName[] timerNames = new ObjectName[number];56private static NotificationListener[] listeners = new NotificationListener[number];5758private static Throwable uncaughtException = null;5960public static void main(String[] args) throws Exception {61System.out.println(">>> test on Concurrent Modification.");6263Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {64@Override65public void uncaughtException(Thread t, Throwable e) {66e.printStackTrace();67if (e instanceof ConcurrentModificationException) {68uncaughtException = e;69}70}71});7273delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");74for (int i=0; i<number; i++) {75timerNames[i] = new ObjectName("MBean:name=Timer"+i);76listeners[i] = new NotificationListener() {77@Override78public void handleNotification(Notification notification, Object handback) {79// nothing80}81};82}83String errors = "";8485for (int i = 0; i < protocols.length; i++) {86uncaughtException = null;87System.out.println(">>> Test for protocol " + protocols[i]);88test(protocols[i]);89if (uncaughtException != null) {90if ("".equals(errors)) {91errors = "Failed to " + protocols[i] + ": "+uncaughtException;92} else {93errors = errors+", failed to " + protocols[i] + ": "+uncaughtException;94}95System.out.println(">>> FAILED for protocol " + protocols[i]);96} else {97System.out.println(">>> PASSED for protocol " + protocols[i]);98}99}100101if ("".equals(errors)) {102System.out.println("All Passed!");103} else {104System.out.println("!!!!!! Failed.");105106throw new RuntimeException(errors);107}108}109110private static void test(String proto) throws Exception {111JMXServiceURL u = new JMXServiceURL(proto, null, 0);112JMXConnectorServer server;113JMXConnector client;114115try {116server =117JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);118server.start();119JMXServiceURL addr = server.getAddress();120client = JMXConnectorFactory.connect(addr, null);121} catch (MalformedURLException e) {122System.out.println(">>> not support: " + proto);123return;124}125126final MBeanServerConnection mserver = client.getMBeanServerConnection();127128int count = 0;129boolean adding = true;130while (uncaughtException == null && count++ < 10) {131for (int i = 0; i < number; i++) {132listenerOp(mserver, listeners[i], adding);133mbeanOp(mserver, timerNames[i], adding);134}135adding = !adding;136}137138if (uncaughtException != null) { // clean139for (int i = 0; i < number; i++) {140try {141mbeanOp(mserver, timerNames[i], false);142} catch (Exception e) {143}144}145}146client.close();147server.stop();148}149150private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)151throws Exception {152if (adding) {153mserver.createMBean("javax.management.timer.Timer", name);154} else {155mserver.unregisterMBean(name);156}157}158159private static void listenerOp(MBeanServerConnection mserver, NotificationListener listener, boolean adding)160throws Exception {161if (adding) {162mserver.addNotificationListener(delegateName, listener, null, null);163} else {164mserver.removeNotificationListener(delegateName, listener);165}166}167}168169170