jdk/test/javax/management/mxbean/StandardMBeanOverrideTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 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
       
    26  * @bug 6283045
       
    27  * @summary Test the correctness of immutableInfo field in MBeanInfo descriptor
       
    28  *          when overriding the methods cacheMBeanInfo, getCachedMBeanInfo,
       
    29  *          getMBeanInfo and getNotificationInfo in StandardMBean and
       
    30  *          StandardEmitterMBean.
       
    31  * @author Luis-Miguel Alventosa
       
    32  * @run clean StandardMBeanOverrideTest
       
    33  * @run build StandardMBeanOverrideTest
       
    34  * @run main StandardMBeanOverrideTest
       
    35  */
       
    36 
       
    37 import java.io.*;
       
    38 import java.lang.management.*;
       
    39 import javax.management.*;
       
    40 import javax.management.openmbean.*;
       
    41 
       
    42 public class StandardMBeanOverrideTest {
       
    43 
       
    44     private static Object testInstances[] = {
       
    45         new TestClass0(false),
       
    46         new TestClass1(false),
       
    47         new TestClass2(false),
       
    48         new TestClass3(false),
       
    49         new TestClass4(false),
       
    50         new TestClass5(false),
       
    51         new TestClass6(false),
       
    52         new TestClass7(false),
       
    53         new TestClass8(false),
       
    54         new TestClass9(false),
       
    55         new TestClass0(true),
       
    56         new TestClass1(true),
       
    57         new TestClass2(true),
       
    58         new TestClass3(true),
       
    59         new TestClass4(true),
       
    60         new TestClass5(true),
       
    61         new TestClass6(true),
       
    62         new TestClass7(true),
       
    63         new TestClass8(true),
       
    64         new TestClass9(true),
       
    65     };
       
    66 
       
    67     public interface ImmutableInfo {
       
    68     }
       
    69 
       
    70     public interface NonImmutableInfo {
       
    71     }
       
    72 
       
    73     public interface TestInterface {
       
    74     }
       
    75 
       
    76     public static class TestClass0
       
    77         extends StandardMBean
       
    78         implements TestInterface, ImmutableInfo {
       
    79         public TestClass0(boolean mxbean) {
       
    80             super(TestInterface.class, mxbean);
       
    81         }
       
    82     }
       
    83 
       
    84     public static class TestClass1
       
    85         extends StandardMBean
       
    86         implements TestInterface, NonImmutableInfo {
       
    87         public TestClass1(boolean mxbean) {
       
    88             super(TestInterface.class, mxbean);
       
    89         }
       
    90         protected void cacheMBeanInfo(MBeanInfo info) {
       
    91             super.cacheMBeanInfo(info);
       
    92         }
       
    93     }
       
    94 
       
    95     public static class TestClass2
       
    96         extends StandardMBean
       
    97         implements TestInterface, NonImmutableInfo {
       
    98         public TestClass2(boolean mxbean) {
       
    99             super(TestInterface.class, mxbean);
       
   100         }
       
   101         protected MBeanInfo getCachedMBeanInfo() {
       
   102             return super.getCachedMBeanInfo();
       
   103         }
       
   104     }
       
   105 
       
   106     public static class TestClass3
       
   107         extends StandardMBean
       
   108         implements TestInterface, NonImmutableInfo {
       
   109         public TestClass3(boolean mxbean) {
       
   110             super(TestInterface.class, mxbean);
       
   111         }
       
   112         public MBeanInfo getMBeanInfo() {
       
   113             return super.getMBeanInfo();
       
   114         }
       
   115     }
       
   116 
       
   117     public static class TestClass4
       
   118         extends StandardMBean
       
   119         implements TestInterface, ImmutableInfo {
       
   120         public TestClass4(boolean mxbean) {
       
   121             super(TestInterface.class, mxbean);
       
   122         }
       
   123         public MBeanNotificationInfo[] getNotificationInfo() {
       
   124             return new MBeanNotificationInfo[0];
       
   125         }
       
   126     }
       
   127 
       
   128     public static class TestClass5
       
   129         extends StandardEmitterMBean
       
   130         implements TestInterface, ImmutableInfo {
       
   131         public TestClass5(boolean mxbean) {
       
   132             super(TestInterface.class, mxbean,
       
   133                   new NotificationBroadcasterSupport());
       
   134         }
       
   135     }
       
   136 
       
   137     public static class TestClass6
       
   138         extends StandardEmitterMBean
       
   139         implements TestInterface, NonImmutableInfo {
       
   140         public TestClass6(boolean mxbean) {
       
   141             super(TestInterface.class, mxbean,
       
   142                   new NotificationBroadcasterSupport());
       
   143         }
       
   144         protected void cacheMBeanInfo(MBeanInfo info) {
       
   145             super.cacheMBeanInfo(info);
       
   146         }
       
   147     }
       
   148 
       
   149     public static class TestClass7
       
   150         extends StandardEmitterMBean
       
   151         implements TestInterface, NonImmutableInfo {
       
   152         public TestClass7(boolean mxbean) {
       
   153             super(TestInterface.class, mxbean,
       
   154                   new NotificationBroadcasterSupport());
       
   155         }
       
   156         protected MBeanInfo getCachedMBeanInfo() {
       
   157             return super.getCachedMBeanInfo();
       
   158         }
       
   159     }
       
   160 
       
   161     public static class TestClass8
       
   162         extends StandardEmitterMBean
       
   163         implements TestInterface, NonImmutableInfo {
       
   164         public TestClass8(boolean mxbean) {
       
   165             super(TestInterface.class, mxbean,
       
   166                   new NotificationBroadcasterSupport());
       
   167         }
       
   168         public MBeanInfo getMBeanInfo() {
       
   169             return super.getMBeanInfo();
       
   170         }
       
   171     }
       
   172 
       
   173     public static class TestClass9
       
   174         extends StandardEmitterMBean
       
   175         implements TestInterface, NonImmutableInfo {
       
   176         public TestClass9(boolean mxbean) {
       
   177             super(TestInterface.class, mxbean,
       
   178                   new NotificationBroadcasterSupport());
       
   179         }
       
   180         public MBeanNotificationInfo[] getNotificationInfo() {
       
   181             return new MBeanNotificationInfo[0];
       
   182         }
       
   183     }
       
   184 
       
   185     public static void main(String[] args) throws Exception {
       
   186 
       
   187         int error = 0;
       
   188 
       
   189         // Instantiate the MBean server
       
   190         //
       
   191         echo("\n>>> Create the MBean server");
       
   192         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
       
   193 
       
   194         // Get default domain
       
   195         //
       
   196         echo("\n>>> Get the MBean server's default domain");
       
   197         String domain = mbs.getDefaultDomain();
       
   198         echo("\tDefault Domain = " + domain);
       
   199 
       
   200         for (int i = 0; i < testInstances.length; i++) {
       
   201             // Create and register the TestClass MBean
       
   202             //
       
   203             String cn = testInstances[i].getClass().getName();
       
   204             String ons = domain + ":type=" + cn + ",name=" + i;
       
   205             echo("\n>>> Create the " + cn +
       
   206                  " MBean within the MBeanServer");
       
   207             echo("\tObjectName = " + ons);
       
   208             ObjectName on = ObjectName.getInstance(ons);
       
   209             mbs.registerMBean(testInstances[i], on);
       
   210 
       
   211             // Check immutableInfo field in descriptor
       
   212             //
       
   213             MBeanInfo mbi = mbs.getMBeanInfo(on);
       
   214             Descriptor d = mbi.getDescriptor();
       
   215             echo("MBeanInfo descriptor = " + d);
       
   216             if (d == null || d.getFieldNames().length == 0) {
       
   217                 error++;
       
   218                 echo("Descriptor is null or empty");
       
   219                 continue;
       
   220             }
       
   221             if (testInstances[i] instanceof ImmutableInfo) {
       
   222                 if ("true".equals(d.getFieldValue("immutableInfo"))) {
       
   223                     echo("OK: immutableInfo field is true");
       
   224                 } else {
       
   225                     echo("KO: immutableInfo field should be true");
       
   226                     error++;
       
   227                 }
       
   228                 continue;
       
   229             }
       
   230             if (testInstances[i] instanceof NonImmutableInfo) {
       
   231                 if ("false".equals(d.getFieldValue("immutableInfo"))) {
       
   232                     echo("OK: immutableInfo field is false");
       
   233                 } else {
       
   234                     echo("KO: immutableInfo field should be false");
       
   235                     error++;
       
   236                 }
       
   237                 continue;
       
   238             }
       
   239         }
       
   240 
       
   241         if (error > 0) {
       
   242             echo("\nTest failed! " + error + " errors.\n");
       
   243             throw new IllegalArgumentException("Test failed");
       
   244         } else {
       
   245             echo("\nTest passed!\n");
       
   246         }
       
   247     }
       
   248 
       
   249     private static void echo(String msg) {
       
   250         System.out.println(msg);
       
   251     }
       
   252 }