CloseServerTest.java revision 469:8f52c4d1d934
1/*
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 4838640
27 * @summary test server close in different conditions.
28 * @author Shanliang JIANG
29 * @run clean CloseServerTest
30 * @run build CloseServerTest
31 * @run main CloseServerTest
32 */
33
34import com.sun.jmx.remote.util.EnvHelp;
35import java.net.MalformedURLException;
36
37import java.util.Arrays;
38import java.util.Collections;
39import java.util.List;
40import java.util.Map;
41import javax.management.*;
42import javax.management.remote.*;
43import javax.management.remote.rmi.RMIConnectorServer;
44
45public class CloseServerTest {
46    private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
47    private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
48
49    public static void main(String[] args) {
50        System.out.println(">>> Tests for closing a server.");
51
52        boolean ok = true;
53        for (int i = 0; i < protocols.length; i++) {
54            try {
55                if (!test(protocols[i])) {
56                    System.out.println(">>> Test failed for " + protocols[i]);
57                    ok = false;
58                } else {
59                    System.out.println(">>> Test successed for " + protocols[i]);
60                }
61            } catch (Exception e) {
62                System.out.println(">>> Test failed for " + protocols[i]);
63                e.printStackTrace(System.out);
64                ok = false;
65            }
66        }
67
68        if (ok) {
69            System.out.println(">>> Test passed");
70        } else {
71            System.out.println(">>> TEST FAILED");
72            System.exit(1);
73        }
74    }
75
76    private static boolean test(String proto)
77            throws Exception {
78        System.out.println(">>> Test for protocol " + proto);
79        JMXServiceURL u = new JMXServiceURL(proto, null, 0);
80        JMXConnectorServer server;
81        JMXServiceURL addr;
82        JMXConnector client;
83        MBeanServerConnection mserver;
84
85        final ObjectName delegateName =
86                    new ObjectName("JMImplementation:type=MBeanServerDelegate");
87        final NotificationListener dummyListener = new NotificationListener() {
88                public void handleNotification(Notification n, Object o) {
89                    // do nothing
90                    return;
91                }
92            };
93
94        try {
95            // open and close
96            System.out.println(">>> Open and close a server.");
97
98            server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
99            server.stop();
100
101            // open, start then close
102            System.out.println(">>> Open, start and close a server.");
103
104            server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
105            server.start();
106            server.stop();
107
108            // with a client, but close the server first
109            System.out.println(">>> Open, start a server, create a client, close the server then the client.");
110
111            server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
112            server.start();
113
114            addr = server.getAddress();
115            client = JMXConnectorFactory.newJMXConnector(addr, null);
116            client.connect(null);
117
118            server.stop();
119
120            try {
121                client.close();
122            } catch (Exception ee) {
123                // OK, the server has been closed
124            }
125
126            // with a client, but close the client first
127            System.out.println(">>> Open, start a server, create a client, close the client then server.");
128            server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
129            server.start();
130
131            addr = server.getAddress();
132            client = JMXConnectorFactory.newJMXConnector(addr, null);
133            client.connect(null);
134
135            client.close();
136
137            server.stop();
138
139            List<Map<String, String>> envs = Arrays.asList(
140                    Collections.singletonMap(
141                        RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE, "false"),
142                    Collections.singletonMap(
143                        RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE, "true"));
144
145            for (Map<String, String> env : envs) {
146                    System.out.println(
147                            ">>>>>>>> " + RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE +
148                            " = " + env.get(RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE));
149
150                // with a client listener, but close the server first
151                System.out.println(">>> Open, start a server, create a client, " +
152                        "add a listener, close the server then the client.");
153                server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);
154                server.start();
155
156                addr = server.getAddress();
157                client = JMXConnectorFactory.newJMXConnector(addr, null);
158                client.connect(null);
159
160                mserver = client.getMBeanServerConnection();
161                mserver.addNotificationListener(delegateName, dummyListener, null, null);
162
163                server.stop();
164
165                try {
166                    client.close();
167                } catch (Exception e) {
168                    // ok, it is because the server has been closed.
169                }
170
171                // with a client listener, but close the client first
172                System.out.println(">>> Open, start a server, create a client, " +
173                        "add a listener, close the client then the server.");
174                server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);
175                server.start();
176
177                addr = server.getAddress();
178                client = JMXConnectorFactory.newJMXConnector(addr, null);
179                client.connect(null);
180
181                mserver = client.getMBeanServerConnection();
182                mserver.addNotificationListener(delegateName, dummyListener, null, null);
183
184                client.close();
185                server.stop();
186            }
187        } catch (MalformedURLException e) {
188            System.out.println(">>> Skipping unsupported URL " + u);
189            return true;
190        }
191
192        return true;
193    }
194}
195