Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/CloseFailedClientTest.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* @test25* @bug 492188826* @summary Tests that we do not get a NullPointException.27* @author Shanliang JIANG28*29* @run clean CloseFailedClientTest30* @run build CloseFailedClientTest31* @run main CloseFailedClientTest32*/3334import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;3940/**41* Try to connect a client to a no-existing or not started server,42* expected to receive an IOException.43*44*/45public class CloseFailedClientTest {46/**47* we use a fix port on which we hope no server is running,48* or a server running on it will give an IOException when our49* clients try to connect to it.50* The port 999 is specified in51* http://www.iana.org/assignments/port-numbers52* as:53* garcon 999/tcp54* applix 999/udp Applix ac55* puprouter 999/tcp56* puprouter 999/udp57*58* If the test fails because a server runs on this port and does59* not give back an IOException, we can change to another one like60* 999961*/62private static final int port = 999;6364private static final String[] protocols = {"rmi", "iiop", "jmxmp"};6566public static void main(String[] args) throws Exception {67System.out.println("Test to close a failed client.");6869boolean ok = true;70for (int i = 0; i < protocols.length; i++) {71try {72if (!test(protocols[i])) {73System.out.println("Test failed for " + protocols[i]);74ok = false;75} else {76System.out.println("Test successed for " + protocols[i]);77}78} catch (Exception e) {79System.out.println("Test failed for " + protocols[i]);80e.printStackTrace(System.out);81ok = false;82}83}8485if (ok) {86System.out.println("Test passed");87return;88} else {89System.out.println("TEST FAILED");90System.exit(1);91}92}939495private static boolean test(String proto)96throws Exception {97System.out.println("Test for protocol " + proto);98JMXServiceURL url = new JMXServiceURL(proto, null, port);99100JMXConnector connector;101JMXConnectorServer server;102103for (int i=0; i<20; i++) {104// no server105try {106connector = JMXConnectorFactory.newJMXConnector(url, null);107} catch (MalformedURLException e) {108System.out.println("Skipping unsupported URL " + url);109return true;110}111112try {113connector.connect();114115throw new RuntimeException("Do not get expected IOEeption.");116} catch (IOException e) {117// OK, the expected IOException is thrown.");118}119120// close the connector client121connector.close();122123// with server but not started124try {125server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, null);126} catch (MalformedURLException e) {127System.out.println("Skipping unsupported URL " + url);128return true;129}130131connector = JMXConnectorFactory.newJMXConnector(url, null);132133try {134connector.connect();135136throw new RuntimeException("Do not get expected IOEeption.");137} catch (IOException e) {138// OK, the expected IOException is thrown.");139}140141// close the connector client142connector.close();143}144145return true;146}147}148149150