blob: addd2a8e086c488f4db7e5779a6ee8a47084568d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4921888
27 * @summary Tests that we do not get a NullPointException.
28 * @author Shanliang JIANG
29 * @run clean CloseFailedClientTest
30 * @run build CloseFailedClientTest
31 * @run main CloseFailedClientTest
32 */
33
34import java.net.MalformedURLException;
35import java.io.IOException;
36
37import javax.management.*;
38import javax.management.remote.*;
39
40/**
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 our
49 * clients try to connect to it.
50 * The port 999 is specified in
51 * http://www.iana.org/assignments/port-numbers
52 * as:
53 * garcon 999/tcp
54 * applix 999/udp Applix ac
55 * puprouter 999/tcp
56 * puprouter 999/udp
57 *
58 * If the test fails because a server runs on this port and does
59 * not give back an IOException, we can change to another one like
60 * 9999
61 */
62 private static final int port = 999;
63
64 private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
65
66 public static void main(String[] args) throws Exception {
67 System.out.println("Test to close a failed client.");
68
69 boolean ok = true;
70 for (int i = 0; i < protocols.length; i++) {
71 try {
72 if (!test(protocols[i])) {
73 System.out.println("Test failed for " + protocols[i]);
74 ok = false;
75 } else {
76 System.out.println("Test successed for " + protocols[i]);
77 }
78 } catch (Exception e) {
79 System.out.println("Test failed for " + protocols[i]);
80 e.printStackTrace(System.out);
81 ok = false;
82 }
83 }
84
85 if (ok) {
86 System.out.println("Test passed");
87 return;
88 } else {
89 System.out.println("TEST FAILED");
90 System.exit(1);
91 }
92 }
93
94
95 private static boolean test(String proto)
96 throws Exception {
97 System.out.println("Test for protocol " + proto);
98 JMXServiceURL url = new JMXServiceURL(proto, null, port);
99
100 JMXConnector connector;
101 JMXConnectorServer server;
102
103 for (int i=0; i<20; i++) {
104 // no server
105 try {
106 connector = JMXConnectorFactory.newJMXConnector(url, null);
107 } catch (MalformedURLException e) {
108 System.out.println("Skipping unsupported URL " + url);
109 return true;
110 }
111
112 try {
113 connector.connect();
114
115 throw new RuntimeException("Do not get expected IOEeption.");
116 } catch (IOException e) {
117 // OK, the expected IOException is thrown.");
118 }
119
120 // close the connector client
121 connector.close();
122
123 // with server but not started
124 try {
125 server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, null);
126 } catch (MalformedURLException e) {
127 System.out.println("Skipping unsupported URL " + url);
128 return true;
129 }
130
131 connector = JMXConnectorFactory.newJMXConnector(url, null);
132
133 try {
134 connector.connect();
135
136 throw new RuntimeException("Do not get expected IOEeption.");
137 } catch (IOException e) {
138 // OK, the expected IOException is thrown.");
139 }
140
141 // close the connector client
142 connector.close();
143 }
144
145 return true;
146 }
147}