Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/FailedConnectionTest.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*/222324/*25* @test FailedConnectionTest 1.1 03/11/2626* @bug 493957827* @summary test to get an IOException.28* @author Shanliang JIANG29*30* @run clean FailedConnectionTest31* @run build FailedConnectionTest32* @run main FailedConnectionTest33*/3435import java.net.MalformedURLException;36import java.io.IOException;37import java.util.HashMap;3839import javax.management.*;40import javax.management.remote.*;4142public class FailedConnectionTest {43private static final String[] protocols = {"rmi", "iiop", "jmxmp"};44private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4546public static void main(String[] args) {47System.out.println(">>> test to get an IOException when calling"+48" getConnectionID on a closed connection.");4950boolean ok = true;51for (int i = 0; i < protocols.length; i++) {52try {53if (!test(protocols[i])) {54System.out.println(">>> Test failed for " + protocols[i]);55ok = false;56} else {57System.out.println(">>> Test successed for " + protocols[i]);58}59} catch (Exception e) {60System.out.println(">>> Test failed for " + protocols[i]);61e.printStackTrace(System.out);62ok = false;63}64}6566if (ok) {67System.out.println(">>> Test passed");68} else {69System.out.println(">>> TEST FAILED");70System.exit(1);71}72}7374private static boolean test(String proto)75throws Exception {76System.out.println(">>> Test for protocol " + proto);7778JMXServiceURL u = null;79JMXConnectorServer server = null;8081try {82u = new JMXServiceURL(proto, null, 0);83server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);84} catch (MalformedURLException e) {85System.out.println("Skipping unsupported URL " + proto);86return true;87}8889server.start();9091JMXServiceURL addr = server.getAddress();9293HashMap env = new HashMap(1);94env.put("jmx.remote.x.client.connection.check.period", "0");9596JMXConnector client = JMXConnectorFactory.connect(addr, env);97server.stop();98Thread.sleep(1000);99try {100client.getConnectionId();101102System.out.println("Do not get expected IOException, failed.");103return false;104} catch (IOException ioe) {105// Good106return true;107}108}109}110111112