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