Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/CloseFailedClientTest.java
38867 views
/*1* Copyright (c) 2003, 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* @run clean CloseFailedClientTest29* @run build CloseFailedClientTest30* @run main CloseFailedClientTest31*/3233import java.net.MalformedURLException;34import java.io.IOException;3536import javax.management.*;37import javax.management.remote.*;3839/**40* Try to connect a client to a no-existing or not started server,41* expected to receive an IOException.42*43*/44public class CloseFailedClientTest {45/**46* we use a fix port on which we hope no server is running,47* or a server running on it will give an IOException when our48* clients try to connect to it.49* The port 999 is specified in50* http://www.iana.org/assignments/port-numbers51* as:52* garcon 999/tcp53* applix 999/udp Applix ac54* puprouter 999/tcp55* puprouter 999/udp56*57* If the test fails because a server runs on this port and does58* not give back an IOException, we can change to another one like59* 999960*/61private static final int port = 999;6263private static final String[] protocols = {"rmi", "iiop", "jmxmp"};6465public static void main(String[] args) throws Exception {66System.out.println("Test to close a failed client.");6768boolean ok = true;69for (int i = 0; i < protocols.length; i++) {70try {71if (!test(protocols[i])) {72System.out.println("Test failed for " + protocols[i]);73ok = false;74} else {75System.out.println("Test successed for " + protocols[i]);76}77} catch (Exception e) {78System.out.println("Test failed for " + protocols[i]);79e.printStackTrace(System.out);80ok = false;81}82}8384if (ok) {85System.out.println("Test passed");86return;87} else {88System.out.println("TEST FAILED");89System.exit(1);90}91}929394private static boolean test(String proto)95throws Exception {96System.out.println("Test for protocol " + proto);97JMXServiceURL url = new JMXServiceURL(proto, null, port);9899JMXConnector connector;100JMXConnectorServer server;101102for (int i=0; i<20; i++) {103// no server104try {105connector = JMXConnectorFactory.newJMXConnector(url, null);106} catch (MalformedURLException e) {107System.out.println("Skipping unsupported URL " + url);108return true;109}110111try {112connector.connect();113114throw new RuntimeException("Do not get expected IOEeption.");115} catch (IOException e) {116// OK, the expected IOException is thrown.");117}118119// close the connector client120connector.close();121122// with server but not started123try {124server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, null);125} catch (MalformedURLException e) {126System.out.println("Skipping unsupported URL " + url);127return true;128}129130connector = JMXConnectorFactory.newJMXConnector(url, null);131132try {133connector.connect();134135throw new RuntimeException("Do not get expected IOEeption.");136} catch (IOException e) {137// OK, the expected IOException is thrown.");138}139140// close the connector client141connector.close();142}143144return true;145}146}147148149