Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/CloseServerTest.java
41159 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 483864026* @summary test server close in different conditions.27* @author Shanliang JIANG28*29* @run clean CloseServerTest30* @run build CloseServerTest31* @run main CloseServerTest32*/3334import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;3940public class CloseServerTest {41private static final String[] protocols = {"rmi", "iiop", "jmxmp"};42private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4344public static void main(String[] args) {45System.out.println(">>> Tests for closing a server.");4647boolean ok = true;48for (int i = 0; i < protocols.length; i++) {49try {50if (!test(protocols[i])) {51System.out.println(">>> Test failed for " + protocols[i]);52ok = false;53} else {54System.out.println(">>> Test successed for " + protocols[i]);55}56} catch (Exception e) {57System.out.println(">>> Test failed for " + protocols[i]);58e.printStackTrace(System.out);59ok = false;60}61}6263if (ok) {64System.out.println(">>> Test passed");65} else {66System.out.println(">>> TEST FAILED");67System.exit(1);68}69}7071private static boolean test(String proto)72throws Exception {73System.out.println(">>> Test for protocol " + proto);74JMXServiceURL u = new JMXServiceURL(proto, null, 0);75JMXConnectorServer server;76JMXServiceURL addr;77JMXConnector client;78MBeanServerConnection mserver;7980final ObjectName delegateName =81new ObjectName("JMImplementation:type=MBeanServerDelegate");82final NotificationListener dummyListener = new NotificationListener() {83public void handleNotification(Notification n, Object o) {84// do nothing85return;86}87};8889try {90// open and close91System.out.println(">>> Open and close a server.");9293server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);94server.stop();9596// open, start then close97System.out.println(">>> Open, start and close a server.");9899server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);100server.start();101server.stop();102103// with a client, but close the server first104System.out.println(">>> Open, start a server, create a client, close the server then the client.");105106server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);107server.start();108109addr = server.getAddress();110client = JMXConnectorFactory.newJMXConnector(addr, null);111client.connect(null);112113server.stop();114115try {116client.close();117} catch (Exception ee) {118// OK, the server has been closed119}120121// with a client, but close the client first122System.out.println(">>> Open, start a server, create a client, close the client then server.");123server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);124server.start();125126addr = server.getAddress();127client = JMXConnectorFactory.newJMXConnector(addr, null);128client.connect(null);129130client.close();131132server.stop();133134// with a client listener, but close the server first135System.out.println(">>> Open, start a server, create a client, add a listener, close the server then the client.");136server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);137server.start();138139addr = server.getAddress();140client = JMXConnectorFactory.newJMXConnector(addr, null);141client.connect(null);142143mserver = client.getMBeanServerConnection();144mserver.addNotificationListener(delegateName, dummyListener, null, null);145146server.stop();147148try {149client.close();150} catch (Exception e) {151// ok, it is because the server has been closed.152}153154// with a client listener, but close the client first155System.out.println(">>> Open, start a server, create a client, add a listener, close the client then the server.");156server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);157server.start();158159addr = server.getAddress();160client = JMXConnectorFactory.newJMXConnector(addr, null);161client.connect(null);162163mserver = client.getMBeanServerConnection();164mserver.addNotificationListener(delegateName, dummyListener, null, null);165166client.close();167server.stop();168} catch (MalformedURLException e) {169System.out.println(">>> Skipping unsupported URL " + u);170return true;171}172173return true;174}175}176177178