1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4921888
  27  * @summary Tests that we do not get a NullPointException.
  28  * @author Shanliang JIANG
  29  *
  30  * @run clean CloseFailedClientTest
  31  * @run build CloseFailedClientTest
  32  * @run main CloseFailedClientTest
  33  */
  34 
  35 import java.net.MalformedURLException;
  36 import java.io.IOException;
  37 
  38 import javax.management.*;
  39 import javax.management.remote.*;
  40 
  41 /**
  42  * Try to connect a client to a no-existing or not started server,
  43  * expected to receive an IOException.
  44  *
  45  */
  46 public class CloseFailedClientTest {
  47     /**
  48      * we use a fix port on which we hope no server is running,
  49      * or a server running on it will give an IOException when our
  50      * clients try to connect to it.
  51      * The port 999 is specified in
  52      * http://www.iana.org/assignments/port-numbers
  53      * as:
  54      * garcon           999/tcp
  55      * applix           999/udp        Applix ac
  56      * puprouter        999/tcp
  57      * puprouter        999/udp
  58      *
  59      * If the test fails because a server runs on this port and does
  60      * not give back an IOException, we can change to another one like
  61      * 9999
  62      */
  63     private static final int port = 999;
  64 
  65     private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
  66 
  67     public static void main(String[] args) throws Exception {
  68         System.out.println("Test to close a failed client.");
  69 
  70         boolean ok = true;
  71         for (int i = 0; i < protocols.length; i++) {
  72             try {
  73                 if (!test(protocols[i])) {
  74                     System.out.println("Test failed for " + protocols[i]);
  75                     ok = false;
  76                 } else {
  77                     System.out.println("Test successed for " + protocols[i]);
  78                 }
  79             } catch (Exception e) {
  80                 System.out.println("Test failed for " + protocols[i]);
  81                 e.printStackTrace(System.out);
  82                 ok = false;
  83             }
  84         }
  85 
  86         if (ok) {
  87             System.out.println("Test passed");
  88             return;
  89         } else {
  90             System.out.println("TEST FAILED");
  91             System.exit(1);
  92         }
  93     }
  94 
  95 
  96     private static boolean test(String proto)
  97             throws Exception {
  98         System.out.println("Test for protocol " + proto);
  99         JMXServiceURL url = new JMXServiceURL(proto, null, port);
 100 
 101         JMXConnector connector;
 102         JMXConnectorServer server;
 103 
 104         for (int i=0; i<20; i++) {
 105             // no server
 106             try {
 107                 connector = JMXConnectorFactory.newJMXConnector(url, null);
 108             } catch (MalformedURLException e) {
 109                 System.out.println("Skipping unsupported URL " + url);
 110                 return true;
 111             }
 112 
 113             try {
 114                 connector.connect();
 115 
 116                 throw new RuntimeException("Do not get expected IOEeption.");
 117             } catch (IOException e) {
 118                 // OK, the expected IOException is thrown.");
 119             }
 120 
 121             // close the connector client
 122             connector.close();
 123 
 124             // with server but not started
 125             try {
 126                 server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, null);
 127             } catch (MalformedURLException e) {
 128                 System.out.println("Skipping unsupported URL " + url);
 129                 return true;
 130             }
 131 
 132             connector = JMXConnectorFactory.newJMXConnector(url, null);
 133 
 134             try {
 135                 connector.connect();
 136 
 137                 throw new RuntimeException("Do not get expected IOEeption.");
 138             } catch (IOException e) {
 139                 // OK, the expected IOException is thrown.");
 140             }
 141 
 142             // close the connector client
 143             connector.close();
 144         }
 145 
 146         return true;
 147     }
 148 }