J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 CloseUnconnectedTest.java 1.1 03/08/28 |
| 26 | * @bug 1234567 |
| 27 | * @summary Open, connect then close multi-connectors. |
| 28 | * @author Shanliang JIANG |
| 29 | * @run clean MultiOpenCloseTest |
| 30 | * @run build MultiOpenCloseTest |
| 31 | * @run main MultiOpenCloseTest |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * Test that we can create a connection, call connect() on it, |
| 36 | * then close it without anything surprising happening. |
| 37 | */ |
| 38 | |
| 39 | import java.net.MalformedURLException; |
| 40 | import java.io.IOException; |
| 41 | |
| 42 | import javax.management.*; |
| 43 | import javax.management.remote.*; |
| 44 | |
| 45 | public class MultiOpenCloseTest { |
| 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("Open, connect then close multi-connectors."); |
| 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 | return; |
| 71 | } else { |
| 72 | System.out.println("TEST FAILED"); |
| 73 | System.exit(1); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private static boolean test(String proto) |
| 78 | throws Exception { |
| 79 | System.out.println("Test for protocol " + proto); |
| 80 | JMXServiceURL u = new JMXServiceURL(proto, null, 0); |
| 81 | JMXConnectorServer s; |
| 82 | try { |
| 83 | s = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs); |
| 84 | } catch (MalformedURLException e) { |
| 85 | System.out.println("Skipping unsupported URL " + u); |
| 86 | return true; |
| 87 | } |
| 88 | s.start(); |
| 89 | JMXServiceURL a = s.getAddress(); |
| 90 | |
| 91 | final int MAX_ITERS = 10; |
| 92 | System.out.println("Looping for " + MAX_ITERS + "iterations..."); |
| 93 | |
| 94 | for (int i=0; i<MAX_ITERS; i++) { |
| 95 | JMXConnector c = JMXConnectorFactory.newJMXConnector(a, null); |
| 96 | c.connect(null); |
| 97 | c.close(); |
| 98 | } |
| 99 | |
| 100 | JMXConnector[] cs = new JMXConnector[MAX_ITERS]; |
| 101 | for (int i=0; i<MAX_ITERS; i++) { |
| 102 | cs[i] = JMXConnectorFactory.newJMXConnector(a, null); |
| 103 | cs[i].connect(null); |
| 104 | } |
| 105 | |
| 106 | for (int i=0; i<MAX_ITERS; i++) { |
| 107 | cs[i].close(); |
| 108 | } |
| 109 | |
| 110 | try { |
| 111 | Thread.sleep(100); |
| 112 | } catch (Exception ee) { |
| 113 | // should not |
| 114 | } |
| 115 | |
| 116 | // check state |
| 117 | for (int i=0; i<MAX_ITERS; i++) { |
| 118 | try { |
| 119 | cs[i].getMBeanServerConnection(null); |
| 120 | // no exception |
| 121 | System.out.println("Did not get an IOException as expected, failed to close a client."); |
| 122 | return false; |
| 123 | } catch (IOException ioe) { |
| 124 | // as expected |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | s.stop(); |
| 129 | return true; |
| 130 | } |
| 131 | } |