1 /*
   2  * Copyright (c) 2004, 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 5018593
  27  * @summary Test that calling getMBeansFromURL(url) with a bogus URL on a
  28  *          given MLet instance throws a ServiceNotFoundException exception
  29  *          with a non null cause.
  30  * @author Luis-Miguel Alventosa
  31  *
  32  * @run clean GetMBeansFromURLTest
  33  * @run build GetMBeansFromURLTest
  34  * @run main GetMBeansFromURLTest
  35  */
  36 
  37 import javax.management.MBeanServer;
  38 import javax.management.MBeanServerFactory;
  39 import javax.management.ObjectName;
  40 import javax.management.ServiceNotFoundException;
  41 import javax.management.loading.MLet;
  42 
  43 public class GetMBeansFromURLTest {
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         boolean error = false;
  48 
  49         // Instantiate the MBean server
  50         //
  51         System.out.println("Create the MBean server");
  52         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  53 
  54         // Instantiate an MLet
  55         //
  56         System.out.println("Create the MLet");
  57         MLet mlet = new MLet();
  58 
  59         // Register the MLet MBean with the MBeanServer
  60         //
  61         System.out.println("Register the MLet MBean");
  62         ObjectName mletObjectName = new ObjectName("Test:type=MLet");
  63         mbs.registerMBean(mlet, mletObjectName);
  64 
  65         // Call getMBeansFromURL
  66         //
  67         System.out.println("Call mlet.getMBeansFromURL(<url>)");
  68         try {
  69             mlet.getMBeansFromURL("bogus://whatever");
  70             System.out.println("TEST FAILED: Expected " +
  71                                ServiceNotFoundException.class +
  72                                " exception not thrown.");
  73             error = true;
  74         } catch (ServiceNotFoundException e) {
  75             if (e.getCause() == null) {
  76                 System.out.println("TEST FAILED: Got null cause in " +
  77                                    ServiceNotFoundException.class +
  78                                    " exception.");
  79                 error = true;
  80             } else {
  81                 System.out.println("TEST PASSED: Got non-null cause in " +
  82                                    ServiceNotFoundException.class +
  83                                    " exception.");
  84                 error = false;
  85             }
  86             e.printStackTrace(System.out);
  87         }
  88 
  89         // Unregister the MLet MBean
  90         //
  91         System.out.println("Unregister the MLet MBean");
  92         mbs.unregisterMBean(mletObjectName);
  93 
  94         // Release MBean server
  95         //
  96         System.out.println("Release the MBean server");
  97         MBeanServerFactory.releaseMBeanServer(mbs);
  98 
  99         // End Test
 100         //
 101         System.out.println("Bye! Bye!");
 102         if (error) System.exit(1);
 103     }
 104 }