Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/CloseServerTest.java
38867 views
/*1* Copyright (c) 2003, 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* @run clean CloseServerTest29* @run build CloseServerTest30* @run main CloseServerTest31*/3233import java.net.MalformedURLException;34import java.io.IOException;3536import javax.management.*;37import javax.management.remote.*;3839public class CloseServerTest {40private static final String[] protocols = {"rmi", "iiop", "jmxmp"};41private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4243public static void main(String[] args) {44System.out.println(">>> Tests for closing a server.");4546boolean ok = true;47for (int i = 0; i < protocols.length; i++) {48try {49if (!test(protocols[i])) {50System.out.println(">>> Test failed for " + protocols[i]);51ok = false;52} else {53System.out.println(">>> Test successed for " + protocols[i]);54}55} catch (Exception e) {56System.out.println(">>> Test failed for " + protocols[i]);57e.printStackTrace(System.out);58ok = false;59}60}6162if (ok) {63System.out.println(">>> Test passed");64} else {65System.out.println(">>> TEST FAILED");66System.exit(1);67}68}6970private static boolean test(String proto)71throws Exception {72System.out.println(">>> Test for protocol " + proto);73JMXServiceURL u = new JMXServiceURL(proto, null, 0);74JMXConnectorServer server;75JMXServiceURL addr;76JMXConnector client;77MBeanServerConnection mserver;7879final ObjectName delegateName =80new ObjectName("JMImplementation:type=MBeanServerDelegate");81final NotificationListener dummyListener = new NotificationListener() {82public void handleNotification(Notification n, Object o) {83// do nothing84return;85}86};8788try {89// open and close90System.out.println(">>> Open and close a server.");9192server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);93server.stop();9495// open, start then close96System.out.println(">>> Open, start and close a server.");9798server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);99server.start();100server.stop();101102// with a client, but close the server first103System.out.println(">>> Open, start a server, create a client, close the server then the client.");104105server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);106server.start();107108addr = server.getAddress();109client = JMXConnectorFactory.newJMXConnector(addr, null);110client.connect(null);111112server.stop();113114try {115client.close();116} catch (Exception ee) {117// OK, the server has been closed118}119120// with a client, but close the client first121System.out.println(">>> Open, start a server, create a client, close the client then server.");122server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);123server.start();124125addr = server.getAddress();126client = JMXConnectorFactory.newJMXConnector(addr, null);127client.connect(null);128129client.close();130131server.stop();132133// with a client listener, but close the server first134System.out.println(">>> Open, start a server, create a client, add a listener, close the server then the client.");135server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);136server.start();137138addr = server.getAddress();139client = JMXConnectorFactory.newJMXConnector(addr, null);140client.connect(null);141142mserver = client.getMBeanServerConnection();143mserver.addNotificationListener(delegateName, dummyListener, null, null);144145server.stop();146147try {148client.close();149} catch (Exception e) {150// ok, it is because the server has been closed.151}152153// with a client listener, but close the client first154System.out.println(">>> Open, start a server, create a client, add a listener, close the client then the server.");155server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);156server.start();157158addr = server.getAddress();159client = JMXConnectorFactory.newJMXConnector(addr, null);160client.connect(null);161162mserver = client.getMBeanServerConnection();163mserver.addNotificationListener(delegateName, dummyListener, null, null);164165client.close();166server.stop();167} catch (MalformedURLException e) {168System.out.println(">>> Skipping unsupported URL " + u);169return true;170}171172return true;173}174}175176177