Path: blob/master/test/jdk/javax/management/remote/mandatory/provider/ProviderTest.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* @test ProviderTest.java25* @summary Tests jar services provider are called26*27* @run clean ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl28* @run build ProviderTest provider.JMXConnectorProviderImpl provider.JMXConnectorServerProviderImpl29* @run main ProviderTest30*/3132import java.net.MalformedURLException;3334import 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;404142import javax.management.MBeanServerConnection;43import javax.management.MBeanServerFactory;44import javax.management.MBeanServer;4546/*47* Tests jar services provider are called48*/49import provider.JMXConnectorProviderImpl;50import provider.JMXConnectorServerProviderImpl;51public class ProviderTest {5253public static void main(String[] args) throws Exception {54System.out.println("Starting ProviderTest");55MBeanServer mbs = MBeanServerFactory.newMBeanServer();5657// First do the test with a protocol handled by Service providers58JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");59dotest(url, mbs);6061boolean clientCalled = provider.JMXConnectorProviderImpl.called();62boolean serverCalled = provider.JMXConnectorServerProviderImpl.called();63boolean ok = clientCalled && serverCalled;64if (!ok) {65if (!clientCalled)66System.out.println("Client provider not called");67if (!serverCalled)68System.out.println("Server provider not called");69throw new RuntimeException("Test failed - see log for details");70}7172// The Service Provider doesn't handle IIOP. Default providers MUST73// be called, which may or may not support IIOP.74url = new JMXServiceURL("service:jmx:iiop://");75try {76dotest(url, mbs);77} catch (MalformedURLException e) {78try {79Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");80e.printStackTrace(System.out);81throw new RuntimeException("MalformedURLException throw but IIOP appears to be supported");82} catch (ClassNotFoundException expected) { }83System.out.println("MalformedURLException thrown, IIOP transport not supported");84}8586// Unsupported protocol.87JMXConnectorServer server = null;88JMXConnector client = null;89url =90new JMXServiceURL("service:jmx:unknown-protocol://");91try {92server =93JMXConnectorServerFactory.newJMXConnectorServer(url,94null,95mbs);96throw new RuntimeException("Exception not thrown.");97} catch (MalformedURLException e) {98System.out.println("Expected MalformedURLException thrown.");99}100101try {102client =103JMXConnectorFactory.newJMXConnector(url,104null);105throw new RuntimeException("Exception not thrown.");106} catch (MalformedURLException e) {107System.out.println("Expected MalformedURLException thrown.");108}109110//JMXConnectorProviderException111url =112new JMXServiceURL("service:jmx:throw-provider-exception://");113try {114server =115JMXConnectorServerFactory.newJMXConnectorServer(url,116null,117mbs);118throw new RuntimeException("Exception not thrown.");119} catch(JMXProviderException e) {120System.out.println("Expected JMXProviderException thrown.");121}122123try {124client =125JMXConnectorFactory.newJMXConnector(url,126null);127throw new RuntimeException("Exception not thrown.");128}catch(JMXProviderException e) {129System.out.println("Expected JMXProviderException thrown.");130}131132System.out.println("Test OK");133}134135private static void dotest(JMXServiceURL url, MBeanServer mbs)136throws Exception {137JMXConnectorServer server = null;138JMXConnector client = null;139140server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);141server.start();142JMXServiceURL outputAddr = server.getAddress();143System.out.println("Server started ["+ outputAddr+ "]");144145client = JMXConnectorFactory.newJMXConnector(outputAddr, null);146147client.connect();148System.out.println("Client connected");149150MBeanServerConnection connection151= client.getMBeanServerConnection();152153System.out.println(connection.getDefaultDomain());154}155}156157158