Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/MultiOpenCloseTest.java
41159 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*/2223/*24* @test CloseUnconnectedTest.java 1.1 03/08/2825* @bug 123456726* @summary Open, connect then close multi-connectors.27* @author Shanliang JIANG28*29* @run clean MultiOpenCloseTest30* @run build MultiOpenCloseTest31* @run main MultiOpenCloseTest32*/3334/*35* Test that we can create a connection, call connect() on it,36* then close it without anything surprising happening.37*/3839import java.net.MalformedURLException;40import java.io.IOException;4142import javax.management.*;43import javax.management.remote.*;4445public class MultiOpenCloseTest {46private static final String[] protocols = {"rmi", "iiop", "jmxmp"};47private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4849public static void main(String[] args) {50System.out.println("Open, connect then close multi-connectors.");5152boolean ok = true;53for (int i = 0; i < protocols.length; i++) {54try {55if (!test(protocols[i])) {56System.out.println("Test failed for " + protocols[i]);57ok = false;58} else {59System.out.println("Test successed for " + protocols[i]);60}61} catch (Exception e) {62System.out.println("Test failed for " + protocols[i]);63e.printStackTrace(System.out);64ok = false;65}66}6768if (ok) {69System.out.println("Test passed");70return;71} else {72System.out.println("TEST FAILED");73System.exit(1);74}75}7677private static boolean test(String proto)78throws Exception {79System.out.println("Test for protocol " + proto);80JMXServiceURL u = new JMXServiceURL(proto, null, 0);81JMXConnectorServer s;82try {83s = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);84} catch (MalformedURLException e) {85System.out.println("Skipping unsupported URL " + u);86return true;87}88s.start();89JMXServiceURL a = s.getAddress();9091final int MAX_ITERS = 10;92System.out.println("Looping for " + MAX_ITERS + "iterations...");9394for (int i=0; i<MAX_ITERS; i++) {95JMXConnector c = JMXConnectorFactory.newJMXConnector(a, null);96c.connect(null);97c.close();98}99100JMXConnector[] cs = new JMXConnector[MAX_ITERS];101for (int i=0; i<MAX_ITERS; i++) {102cs[i] = JMXConnectorFactory.newJMXConnector(a, null);103cs[i].connect(null);104}105106for (int i=0; i<MAX_ITERS; i++) {107cs[i].close();108}109110try {111Thread.sleep(100);112} catch (Exception ee) {113// should not114}115116// check state117for (int i=0; i<MAX_ITERS; i++) {118try {119cs[i].getMBeanServerConnection(null);120// no exception121System.out.println("Did not get an IOException as expected, failed to close a client.");122return false;123} catch (IOException ioe) {124// as expected125}126}127128s.stop();129return true;130}131}132133134