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