1 /*
   2  * Copyright (c) 2003, 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 4874819
  27  * @summary Test that MBeanInfo classes no longer throw an
  28  * IllegalArgumentException when attribute names, operation names, and
  29  * Java type names do not strictly follow the expected Java syntax.
  30  * @author Daniel Fuchs
  31  * @modules java.management
  32  * @run clean MustBeValidCommand
  33  * @run build MustBeValidCommand
  34  * @run main MustBeValidCommand
  35  */
  36 
  37 import javax.management.MBeanInfo;
  38 import javax.management.MBeanAttributeInfo;
  39 import javax.management.MBeanOperationInfo;
  40 import javax.management.MBeanParameterInfo;
  41 import javax.management.MBeanNotificationInfo;
  42 import javax.management.MBeanConstructorInfo;
  43 
  44 public class MustBeValidCommand {
  45 
  46     private static String[][] attributes = {
  47         { "Attribute with valid identifiers",
  48           "validType1","validNameAtt1" },
  49         { "Attribute with invalid type",
  50           "invalid-type",   "validNameAtt2" },
  51         { "Attribute with invalid name", "valid.type2",
  52           "invalid-name-att3" },
  53         { "Attribute with invalid name and type",
  54           "invalidtype[]","invalid.name.att4" }
  55     };
  56     private static String[][] constructors = {
  57         { "Constructor with valid name",
  58           "ValidConstructor1" },
  59         { "Constructor with invalid name",
  60           "invalid.Constructor2"},
  61         { "Constructor with invalid name",
  62           "invalid-constructor-3" },
  63         { "Constructor with invalid name",
  64           "invalid constructor" }
  65     };
  66     private static String[][] mbeanclasses = {
  67         { "MBean with valid class name",
  68           "ValidMBeanClass1" },
  69         { "MBean with valid class name",
  70           "valid.mbean.Class2" },
  71         { "MBean with invalid class name",
  72           "invalid.MBeanClass3[]"},
  73         { "MBean with invalid class name",
  74           "invalid-mbean-class-4" },
  75         { "MBean with invalid class name",
  76           "invalid mbean class 5" }
  77     };
  78     private static String[][] notificationclasses = {
  79         { "Notification with valid class name",
  80           "ValidNotificationClass1" },
  81         { "Notification with valid class name",
  82           "valid.notification.Class2" },
  83         { "Notification with invalid class name",
  84           "invalid.NotificationClass3[]"},
  85         { "Notification with invalid class name",
  86           "invalid-notification-class-4" },
  87         { "Notification with invalid class name",
  88           "invalid notification class 5" }
  89     };
  90     private static String[][] operations = {
  91         { "Operation with valid identifiers",
  92           "validType1","validNameOp1" },
  93         { "Operation with invalid type",
  94           "invalid-type",   "validNameOp2" },
  95         { "Operation with invalid name", "valid.type2",
  96           "invalid-name-op3" },
  97         { "Operation with invalid name and type",
  98           "invalidtype[]","invalid.name.op4" }
  99     };
 100     private static String[][] parameters = {
 101         { "Parameter with valid identifiers",
 102           "validType1","validNamePar1" },
 103         { "Parameter with invalid type",
 104           "invalid-type",   "validNamePar2" },
 105         { "Parameter with invalid name", "valid.type2",
 106           "invalid-name-par3" },
 107         { "Parameter with invalid name and type",
 108           "invalidtype[]","invalid.name.par4" }
 109     };
 110 
 111     static private MBeanAttributeInfo[] makeAttInfos(String[][] spec) {
 112         final MBeanAttributeInfo[] result =
 113             new MBeanAttributeInfo[spec.length];
 114         for (int i=0;i<result.length;i++) {
 115             System.out.println("\tCreate an MBeanAttributeInfo: " +
 116                                spec[i][0]);
 117             final MBeanAttributeInfo item =
 118                 new MBeanAttributeInfo(spec[i][2],spec[i][1],spec[i][0],
 119                                        true,true,false);
 120             result[i]=item;
 121         }
 122         return result;
 123     }
 124 
 125     static private MBeanParameterInfo[] makeParInfos(String[][] spec) {
 126         final MBeanParameterInfo[] result =
 127             new MBeanParameterInfo[spec.length];
 128         for (int i=0;i<result.length;i++) {
 129             System.out.println("\tCreate an MBeanParameterInfo: " +
 130                                spec[i][0]);
 131             final MBeanParameterInfo item =
 132                 new MBeanParameterInfo(spec[i][2],spec[i][1],spec[i][0]);
 133             result[i]=item;
 134         }
 135         return result;
 136     }
 137 
 138     static private MBeanOperationInfo[] makeOpInfos(String[][] spec) {
 139         final MBeanOperationInfo[] result =
 140             new MBeanOperationInfo[spec.length];
 141         final MBeanParameterInfo[] pars = makeParInfos(parameters);
 142         for (int i=0;i<result.length;i++) {
 143             System.out.println("\tCreate an MBeanOperationInfo: " +
 144                                spec[i][0]);
 145             final MBeanOperationInfo item =
 146                 new MBeanOperationInfo(spec[i][2],spec[i][0],pars,spec[i][1],
 147                                        MBeanOperationInfo.ACTION_INFO);
 148             result[i]=item;
 149         }
 150         return result;
 151     }
 152 
 153     static private MBeanConstructorInfo[] makeCtorInfos(String[][] spec) {
 154         final MBeanConstructorInfo[] result =
 155             new MBeanConstructorInfo[spec.length];
 156         final MBeanParameterInfo[] pars = makeParInfos(parameters);
 157         for (int i=0;i<result.length;i++) {
 158             System.out.println("\tCreate an MBeanConstructorInfo: " +
 159                                spec[i][0]);
 160             final MBeanConstructorInfo item =
 161                 new MBeanConstructorInfo(spec[i][1],spec[i][0],pars);
 162             result[i]=item;
 163         }
 164         return result;
 165     }
 166 
 167     static private MBeanNotificationInfo[] makeNotifInfos(String[][] spec) {
 168         final MBeanNotificationInfo[] result =
 169             new MBeanNotificationInfo[spec.length];
 170         final String[] types = {"valid.type","invalid-type"};
 171         for (int i=0;i<result.length;i++) {
 172             System.out.println("\tCreate an MBeanNotificationInfo: " +
 173                                spec[i][0]);
 174             final MBeanNotificationInfo item =
 175                 new MBeanNotificationInfo(types,spec[i][1],spec[i][0]);
 176             result[i]=item;
 177         }
 178         return result;
 179     }
 180 
 181     public static void main(String[] args) throws Exception {
 182         // Instantiate the MBean server
 183         //
 184         final MBeanAttributeInfo[]    atts   = makeAttInfos(attributes);
 185         final MBeanConstructorInfo[]  ctors  = makeCtorInfos(constructors);
 186         final MBeanOperationInfo[]    ops    = makeOpInfos(operations);
 187         final MBeanNotificationInfo[] notifs =
 188             makeNotifInfos(notificationclasses);
 189 
 190         for (int i=0; i<mbeanclasses.length;i++) {
 191             System.out.println("Create an MBeanInfo: " + mbeanclasses[i][0]);
 192             final MBeanInfo mbi =
 193                 new MBeanInfo(mbeanclasses[i][1],mbeanclasses[i][0],
 194                               atts, ctors, ops, notifs);
 195         }
 196 
 197         // Test OK!
 198         //
 199         System.out.println("All MBeanInfo successfuly created!");
 200         System.out.println("Bye! Bye!");
 201     }
 202 }