ProviderTest.java revision 699:810a95940b99
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 ProviderTest.java
26 * @summary Tests jar services provider are called
27 * @run clean ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl
28 * @run build ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl
29 * @run main ProviderTest
30 */
31
32import java.net.MalformedURLException;
33
34import javax.management.remote.JMXConnectorFactory;
35import javax.management.remote.JMXConnectorServerFactory;
36import javax.management.remote.JMXConnector;
37import javax.management.remote.JMXConnectorServer;
38import javax.management.remote.JMXServiceURL;
39import javax.management.remote.JMXProviderException;
40
41
42import javax.management.MBeanServerConnection;
43import javax.management.MBeanServerFactory;
44import javax.management.MBeanServer;
45
46/*
47 * Tests jar services provider are called
48 */
49import provider.JMXConnectorProviderImpl;
50import provider.JMXConnectorServerProviderImpl;
51public class ProviderTest {
52    public static void main(String[] args) throws Exception {
53        System.out.println("Starting ProviderTest");
54        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
55        //First do the test with a protocol handled by Service providers
56        JMXServiceURL url =
57            new JMXServiceURL("service:jmx:rmi://");
58
59        dotest(url, mbs);
60
61        boolean clientCalled = provider.JMXConnectorProviderImpl.called();
62        boolean serverCalled = provider.JMXConnectorServerProviderImpl.called();
63        boolean ok = clientCalled && serverCalled;
64        if (!ok) {
65            if (!clientCalled)
66                System.out.println("Client provider not called");
67            if (!serverCalled)
68                System.out.println("Server provider not called");
69            System.out.println("Test Failed");
70            System.exit(1);
71        }
72
73        //The Service Provider doesn't handle IIOP. Default providers MUST
74        //be called.
75        url =
76            new JMXServiceURL("service:jmx:iiop://");
77
78        dotest(url, mbs);
79
80        // Unsupported protocol.
81        JMXConnectorServer server = null;
82        JMXConnector client = null;
83        url =
84            new JMXServiceURL("service:jmx:unknown-protocol://");
85        try {
86            server =
87                JMXConnectorServerFactory.newJMXConnectorServer(url,
88                                                                null,
89                                                                mbs);
90            System.out.println("Exception not thrown.");
91            System.exit(1);
92        }catch(MalformedURLException e) {
93            System.out.println("Expected MalformedURLException thrown.");
94        }
95        catch(Exception e) {
96            e.printStackTrace();
97            System.out.println("Exception thrown : " + e);
98            System.exit(1);
99        }
100
101        try {
102            client =
103                JMXConnectorFactory.newJMXConnector(url,
104                                                    null);
105            System.out.println("Exception not thrown.");
106            System.exit(1);
107        }catch(MalformedURLException e) {
108            System.out.println("Expected MalformedURLException thrown.");
109        }
110        catch(Exception e) {
111            e.printStackTrace();
112            System.out.println("Exception thrown : " + e);
113            System.exit(1);
114        }
115
116        //JMXConnectorProviderException
117        url =
118            new JMXServiceURL("service:jmx:throw-provider-exception://");
119                try {
120            server =
121                JMXConnectorServerFactory.newJMXConnectorServer(url,
122                                                                null,
123                                                                mbs);
124            System.out.println("Exception not thrown.");
125            System.exit(1);
126        }catch(JMXProviderException e) {
127            System.out.println("Expected JMXProviderException thrown.");
128        }
129        catch(Exception e) {
130            e.printStackTrace();
131            System.out.println("Exception thrown : " + e);
132            System.exit(1);
133        }
134
135        try {
136            client =
137                JMXConnectorFactory.newJMXConnector(url,
138                                                    null);
139            System.out.println("Exception not thrown.");
140            System.exit(1);
141        }catch(JMXProviderException e) {
142            System.out.println("Expected JMXProviderException thrown.");
143        }
144        catch(Exception e) {
145            e.printStackTrace();
146            System.out.println("Exception thrown : " + e);
147            System.exit(1);
148        }
149
150        System.out.println("Test OK");
151        return;
152    }
153
154    private static void dotest(JMXServiceURL url, MBeanServer mbs)
155        throws Exception {
156        JMXConnectorServer server = null;
157        JMXConnector client = null;
158        try {
159            server =
160                JMXConnectorServerFactory.newJMXConnectorServer(url,
161                                                                null,
162                                                                mbs);
163        }catch(IllegalArgumentException e) {
164            e.printStackTrace();
165            System.exit(1);
166        }
167        server.start();
168        JMXServiceURL outputAddr = server.getAddress();
169        System.out.println("Server started ["+ outputAddr+ "]");
170
171        try {
172            client =
173                JMXConnectorFactory.newJMXConnector(outputAddr, null);
174        }catch(IllegalArgumentException e) {
175            e.printStackTrace();
176            System.exit(1);
177        }
178
179        client.connect();
180        System.out.println("Client connected");
181
182        MBeanServerConnection connection
183            = client.getMBeanServerConnection();
184
185        System.out.println(connection.getDefaultDomain());
186    }
187}
188