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