1 /*
2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4955222
27  * @summary Test that the releaseMBeanServer(MBeanServer mbeanServer) method
28  *          throws IllegalArgumentException as expected
29  * @author Luis-Miguel Alventosa
30  *
31  * @run clean ReleaseMBeanServerTest
32  * @run build ReleaseMBeanServerTest
33  * @run main ReleaseMBeanServerTest
34  */
35 
36 /* Check that the releaseMBeanServer(MBeanServer mbeanServer) method throws
37    IllegalArgumentException if mbeanServer was not generated by one of the
38    createMBeanServer methods, or if releaseMBeanServer was already called
39    on it. */
40 
41 import javax.management.MBeanServer;
42 import javax.management.MBeanServerFactory;
43 
44 public class ReleaseMBeanServerTest {
45 
46     private static final String DOMAIN = "TestDomain";
47 
main(String[] args)48     public static void main(String[] args) throws Exception {
49 
50         System.out.println("--------------------------------------" +
51                            "-----------------------------------------");
52         System.out.println("- Testing IllegalArgumentException in " +
53                            "MBeanServerFactory.releaseMBeanServer() -");
54         System.out.println("--------------------------------------" +
55                            "-----------------------------------------");
56 
57         System.out.println("TEST_0: Call releaseMBeanServer() with " +
58                            "a null MBeanServer reference.");
59         try {
60             MBeanServerFactory.releaseMBeanServer(null);
61             System.err.println("Didn't get expected IllegalArgumentException!");
62             System.exit(1);
63         } catch (IllegalArgumentException e) {
64             System.out.println("Got expected IllegalArgumentException!");
65         }
66 
67         System.out.println("TEST_1: Call releaseMBeanServer() with an " +
68                            "MBeanServer reference corresponding to an " +
69                            "MBeanServer created using newMBeanServer().");
70         MBeanServer mbs1 = MBeanServerFactory.newMBeanServer();
71         try {
72             MBeanServerFactory.releaseMBeanServer(mbs1);
73             System.err.println("Didn't get expected IllegalArgumentException!");
74             System.exit(1);
75         } catch (IllegalArgumentException e) {
76             System.out.println("Got expected IllegalArgumentException!");
77         }
78 
79         System.out.println("TEST_2: Call releaseMBeanServer() with an " +
80                            "MBeanServer reference corresponding to an " +
81                            "MBeanServer created using newMBeanServer(String).");
82         MBeanServer mbs2 = MBeanServerFactory.newMBeanServer(DOMAIN);
83         try {
84             MBeanServerFactory.releaseMBeanServer(mbs2);
85             System.err.println("Didn't get expected IllegalArgumentException!");
86             System.exit(1);
87         } catch (IllegalArgumentException e) {
88             System.out.println("Got expected IllegalArgumentException!");
89         }
90 
91         System.out.println("TEST_3: Call releaseMBeanServer() twice with an " +
92                            "MBeanServer reference corresponding to an MBean" +
93                            "Server created using createMBeanServer().");
94         MBeanServer mbs3 = MBeanServerFactory.createMBeanServer();
95         MBeanServerFactory.releaseMBeanServer(mbs3);
96         try {
97             MBeanServerFactory.releaseMBeanServer(mbs3);
98             System.err.println("Didn't get expected IllegalArgumentException!");
99             System.exit(1);
100         } catch (IllegalArgumentException e) {
101             System.out.println("Got expected IllegalArgumentException!");
102         }
103 
104         System.out.println("TEST_4: Call releaseMBeanServer() twice with an " +
105                            "MBeanServer reference corresponding to an MBean" +
106                            "Server created using createMBeanServer(String).");
107         MBeanServer mbs4 = MBeanServerFactory.createMBeanServer(DOMAIN);
108         MBeanServerFactory.releaseMBeanServer(mbs4);
109         try {
110             MBeanServerFactory.releaseMBeanServer(mbs4);
111             System.err.println("Didn't get expected IllegalArgumentException!");
112             System.exit(1);
113         } catch (IllegalArgumentException e) {
114             System.out.println("Got expected IllegalArgumentException!");
115         }
116     }
117 }
118