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