1 /*
   2  * Copyright (c) 2003, 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 /*
  26  * @test FailedConnectionTest 1.1 03/11/26
  27  * @bug 4939578
  28  * @summary test to get an IOException.
  29  * @author Shanliang JIANG
  30  * @run clean FailedConnectionTest
  31  * @run build FailedConnectionTest
  32  * @run main FailedConnectionTest
  33  */
  34 
  35 import java.net.MalformedURLException;
  36 import java.io.IOException;
  37 import java.util.HashMap;
  38 
  39 import javax.management.*;
  40 import javax.management.remote.*;
  41 
  42 public class FailedConnectionTest {
  43     private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
  44     private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  45 
  46     public static void main(String[] args) {
  47         System.out.println(">>> test to get an IOException when calling"+
  48                           " getConnectionID on a closed connection.");
  49 
  50         boolean ok = true;
  51         for (int i = 0; i < protocols.length; i++) {
  52             try {
  53                 if (!test(protocols[i])) {
  54                     System.out.println(">>> Test failed for " + protocols[i]);
  55                     ok = false;
  56                 } else {
  57                     System.out.println(">>> Test successed for " + protocols[i]);
  58                 }
  59             } catch (Exception e) {
  60                 System.out.println(">>> Test failed for " + protocols[i]);
  61                 e.printStackTrace(System.out);
  62                 ok = false;
  63             }
  64         }
  65 
  66         if (ok) {
  67             System.out.println(">>> Test passed");
  68         } else {
  69             System.out.println(">>> TEST FAILED");
  70             System.exit(1);
  71         }
  72     }
  73 
  74     private static boolean test(String proto)
  75             throws Exception {
  76         System.out.println(">>> Test for protocol " + proto);
  77 
  78         JMXServiceURL u = null;
  79         JMXConnectorServer server = null;
  80 
  81         try {
  82             u = new JMXServiceURL(proto, null, 0);
  83             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
  84         } catch (MalformedURLException e) {
  85             System.out.println("Skipping unsupported URL " + proto);
  86             return true;
  87         }
  88 
  89         server.start();
  90 
  91         JMXServiceURL addr = server.getAddress();
  92 
  93         HashMap env = new HashMap(1);
  94         env.put("jmx.remote.x.client.connection.check.period", "0");
  95 
  96         JMXConnector client = JMXConnectorFactory.connect(addr, env);
  97         server.stop();
  98         Thread.sleep(1000);
  99         try {
 100             client.getConnectionId();
 101 
 102             System.out.println("Do not get expected IOException, failed.");
 103             return false;
 104         } catch (IOException ioe) {
 105             // Good
 106             return true;
 107         }
 108     }
 109 }