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