jdk/test/javax/management/namespace/NamespaceCreationTest.java
changeset 1156 bbc2d15aaf7a
child 1227 4546977d0d66
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     1 /*
       
     2  * Copyright 2008 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 NamespaceCreationTest.java
       
    26  * @summary General JMXNamespace test.
       
    27  * @author Daniel Fuchs
       
    28  * @run clean NamespaceCreationTest Wombat WombatMBean
       
    29  * @run build NamespaceCreationTest Wombat WombatMBean
       
    30  * @run main NamespaceCreationTest
       
    31  */
       
    32 
       
    33 
       
    34 import java.util.Collections;
       
    35 import java.util.Map;
       
    36 import javax.management.MBeanRegistration;
       
    37 import javax.management.MBeanRegistrationException;
       
    38 import javax.management.MBeanServer;
       
    39 import javax.management.MBeanServerFactory;
       
    40 import javax.management.ObjectInstance;
       
    41 import javax.management.ObjectName;
       
    42 import javax.management.RuntimeMBeanException;
       
    43 import javax.management.namespace.JMXNamespace;
       
    44 import javax.management.namespace.JMXNamespaces;
       
    45 
       
    46 /**
       
    47  * Test simple creation/registration of namespace.
       
    48  *
       
    49  */
       
    50 public class NamespaceCreationTest {
       
    51     private static Map<String,Object> emptyEnvMap() {
       
    52         return Collections.emptyMap();
       
    53     }
       
    54 
       
    55 
       
    56     public static class LocalNamespace extends JMXNamespace {
       
    57 
       
    58         public LocalNamespace() {
       
    59             super(MBeanServerFactory.newMBeanServer());
       
    60         }
       
    61 
       
    62     }
       
    63 
       
    64     private static MBeanServer newMBeanServer() {
       
    65         return MBeanServerFactory.newMBeanServer();
       
    66     }
       
    67 
       
    68     public static interface ThingMBean {}
       
    69     public static class Thing implements ThingMBean, MBeanRegistration {
       
    70         public ObjectName preRegister(MBeanServer server, ObjectName name)
       
    71                 throws Exception {
       
    72             if (name == null) return new ObjectName(":type=Thing");
       
    73             else return name;
       
    74         }
       
    75         public void postRegister(Boolean registrationDone) {
       
    76         }
       
    77 
       
    78         public void preDeregister() throws Exception {
       
    79         }
       
    80         public void postDeregister() {
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * Test that it is possible to create a dummy MBean with a null
       
    86      * ObjectName - this is just a sanity check - as there are already
       
    87      * other JMX tests that check that.
       
    88      *
       
    89      * @throws java.lang.Exception
       
    90      */
       
    91     public static void testCreateWithNull() throws Exception {
       
    92         final MBeanServer server = newMBeanServer();
       
    93         final ObjectInstance oi = server.registerMBean(new Thing(),null);
       
    94         server.unregisterMBean(oi.getObjectName());
       
    95         System.out.println("testCreateWithNull PASSED");
       
    96     }
       
    97 
       
    98     /**
       
    99      * Check that we can register a JMXNamespace MBean, using its standard
       
   100      * ObjectName.
       
   101      * @throws java.lang.Exception
       
   102      */
       
   103     public static void testGoodObjectName() throws Exception {
       
   104         MBeanServer server = newMBeanServer();
       
   105         final ObjectName name =
       
   106                 JMXNamespaces.getNamespaceObjectName("gloups");
       
   107         final ObjectInstance oi =
       
   108                 server.registerMBean(new LocalNamespace(),name);
       
   109         System.out.println("Succesfully registered namespace: "+name);
       
   110         try {
       
   111             if (! name.equals(oi.getObjectName()))
       
   112                 throw new RuntimeException("testGoodObjectName: TEST failed: " +
       
   113                         "namespace registered as: "+
       
   114                     oi.getObjectName()+" expected: "+name);
       
   115         } finally {
       
   116             server.unregisterMBean(oi.getObjectName());
       
   117         }
       
   118         System.out.println("Succesfully unregistered namespace: "+name);
       
   119         System.out.println("testGoodObjectName PASSED");
       
   120     }
       
   121 
       
   122     /**
       
   123      * Check that we cannot register a JMXNamespace MBean, if we don't use
       
   124      * its standard ObjectName.
       
   125      * @throws java.lang.Exception
       
   126      */
       
   127     public static void testBadObjectName() throws Exception {
       
   128         MBeanServer server = newMBeanServer();
       
   129         Throwable exp = null;
       
   130         final ObjectName name = new ObjectName("d:k=v");
       
   131         try {
       
   132             server.registerMBean(new LocalNamespace(),name);
       
   133             System.out.println("testBadObjectName: " +
       
   134                     "Error: MBean registered, no exception thrown.");
       
   135         } catch(RuntimeMBeanException x) {
       
   136             exp = x.getCause();
       
   137         } catch(Exception x) {
       
   138             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   139                     "expected RuntimeMBeanException - got "+
       
   140                     x);
       
   141         }
       
   142         if (exp == null)  server.unregisterMBean(name);
       
   143         if (exp == null)
       
   144             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   145                     "expected IllegalArgumentException - got none");
       
   146         if (!(exp instanceof IllegalArgumentException))
       
   147             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   148                     "expected IllegalArgumentException - got "+
       
   149                     exp.toString(),exp);
       
   150         System.out.println("Got expected exception: "+exp);
       
   151         System.out.println("testBadObjectName PASSED");
       
   152     }
       
   153 
       
   154     /**
       
   155      * Check that we cannot register a Wombat MBean in a namespace that does
       
   156      * not exists.
       
   157      *
       
   158      * @throws java.lang.Exception
       
   159      */
       
   160     public static void testBadNamespace() throws Exception {
       
   161         MBeanServer server = newMBeanServer();
       
   162         Throwable exp = null;
       
   163         final ObjectName name = new ObjectName("glips//d:k=v");
       
   164 
       
   165         try {
       
   166             server.registerMBean(new Wombat(),name);
       
   167             System.out.println("testBadNamespace: " +
       
   168                     "Error: MBean registered, no exception thrown.");
       
   169         } catch(MBeanRegistrationException x) {
       
   170             exp = x.getCause();
       
   171         } catch(Exception x) {
       
   172             throw new RuntimeException("testBadNamespace: TEST failed: " +
       
   173                     "expected MBeanRegistrationException - got "+
       
   174                     x);
       
   175         }
       
   176         if (exp == null)  server.unregisterMBean(name);
       
   177         if (exp == null)
       
   178             throw new RuntimeException("testBadNamespace: TEST failed: " +
       
   179                     "expected IllegalArgumentException - got none");
       
   180         if (!(exp instanceof IllegalArgumentException))
       
   181             throw new RuntimeException("testBadNamespace: TEST failed: " +
       
   182                     "expected IllegalArgumentException - got "+
       
   183                     exp.toString(),exp);
       
   184         System.out.println("Got expected exception: "+exp);
       
   185         System.out.println("testBadNamespace PASSED");
       
   186     }
       
   187 
       
   188     /**
       
   189      * Check that we cannot register a Wombat MBean with a domain name
       
   190      * that ends with //. This is reserved for namespaces.
       
   191      *
       
   192      * @throws java.lang.Exception
       
   193      */
       
   194     public static void testBadDomain() throws Exception {
       
   195         MBeanServer server = newMBeanServer();
       
   196         Throwable exp = null;
       
   197         final ObjectName name = new ObjectName("glups//:k=v");
       
   198 
       
   199         try {
       
   200             server.registerMBean(new Wombat(),name);
       
   201             System.out.println("testBadDomain: Error: MBean registered, no exception thrown.");
       
   202         } catch(RuntimeMBeanException x) {
       
   203             exp = x.getCause();
       
   204         } catch(Exception x) {
       
   205             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   206                     "expected RuntimeMBeanException - got "+
       
   207                     x);
       
   208         }
       
   209         if (exp == null)  server.unregisterMBean(name);
       
   210         if (exp == null)
       
   211             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   212                     "expected IllegalArgumentException - got none");
       
   213         if (!(exp instanceof IllegalArgumentException))
       
   214             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   215                     "expected IllegalArgumentException - got "+
       
   216                     exp.toString(),exp);
       
   217         System.out.println("Got expected exception: "+exp);
       
   218         System.out.println("testBadDomain PASSED");
       
   219     }
       
   220 
       
   221     /**
       
   222      * Check that we cannot register a Wombat MBean as if it were a
       
   223      * JMXNamespace. Only JMXNamespace MBeans can have JMX Namespace names.
       
   224      * @throws java.lang.Exception
       
   225      */
       
   226     public static void testBadClassName() throws Exception {
       
   227         MBeanServer server = newMBeanServer();
       
   228         Throwable exp = null;
       
   229         final ObjectName name =
       
   230                 JMXNamespaces.getNamespaceObjectName("glops");
       
   231         try {
       
   232             server.registerMBean(new Wombat(),name);
       
   233             System.out.println("testBadClassName: " +
       
   234                     "Error: MBean registered, no exception thrown.");
       
   235         } catch(RuntimeMBeanException x) {
       
   236             exp = x.getCause();
       
   237         } catch(Exception x) {
       
   238             throw new RuntimeException("testBadClassName: TEST failed: " +
       
   239                     "expected RuntimeMBeanException - got "+
       
   240                     x);
       
   241         }
       
   242         if (exp == null)  server.unregisterMBean(name);
       
   243         if (exp == null)
       
   244             throw new RuntimeException("testBadClassName: TEST failed: " +
       
   245                     "expected IllegalArgumentException - got none");
       
   246         if (!(exp instanceof IllegalArgumentException))
       
   247             throw new RuntimeException("testBadClassName: TEST failed: " +
       
   248                     "expected IllegalArgumentException - got "+
       
   249                     exp.toString(),exp);
       
   250         System.out.println("Got expected exception: "+exp);
       
   251         System.out.println("testBadClassName PASSED");
       
   252     }
       
   253 
       
   254     public static void main(String... args) throws Exception {
       
   255         testCreateWithNull();
       
   256         testGoodObjectName();
       
   257         testBadObjectName();
       
   258         testBadNamespace();
       
   259         testBadDomain();
       
   260         testBadClassName();
       
   261     }
       
   262 }