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 6254721
  27  * @bug 6360539
  28  * @summary Test that Open*MBeanInfo classes include "openType" in descriptor
  29  *          and also test serial compatibility with Java 5.
  30  * @author Eamonn McManus
  31  * @modules java.management
  32  * @run clean OpenTypeDescriptorTest
  33  * @run build OpenTypeDescriptorTest
  34  * @run main OpenTypeDescriptorTest
  35  */
  36 
  37 import java.util.Date;
  38 import javax.management.*;
  39 import javax.management.openmbean.*;
  40 import static javax.management.MBeanOperationInfo.ACTION;
  41 
  42 public class OpenTypeDescriptorTest {
  43     public static void main(String[] args) throws Exception {
  44         Descriptor constraints =
  45                 new ImmutableDescriptor("defaultValue=25",
  46                                         "minValue=3");
  47         Date now = new Date();
  48         Date then = new Date(System.currentTimeMillis() - 86400000);
  49         final DescriptorRead[] testObjects = {
  50             new OpenMBeanAttributeInfoSupport("name", "descr",
  51                                               SimpleType.STRING,
  52                                               true, false, false),
  53             new OpenMBeanAttributeInfoSupport("name", "descr",
  54                                               SimpleType.INTEGER,
  55                                               false, true, false, constraints),
  56             new OpenMBeanAttributeInfoSupport("name", "descr",
  57                                               SimpleType.BOOLEAN,
  58                                               true, false, false, true),
  59             new OpenMBeanAttributeInfoSupport("name", "descr",
  60                                               SimpleType.FLOAT,
  61                                               true, true, false,
  62                                               1.0f, 0.0f, 2.0f),
  63             new OpenMBeanAttributeInfoSupport("name", "descr",
  64                                               SimpleType.DATE,
  65                                               true, false, false,
  66                                               now, new Date[] {now, then}),
  67             new OpenMBeanParameterInfoSupport("name", "descr",
  68                                               SimpleType.STRING),
  69             new OpenMBeanParameterInfoSupport("name", "descr",
  70                                               SimpleType.INTEGER, constraints),
  71             new OpenMBeanParameterInfoSupport("name", "descr",
  72                                               SimpleType.BOOLEAN, true),
  73             new OpenMBeanParameterInfoSupport("name", "descr",
  74                                               SimpleType.FLOAT,
  75                                               1.0f, 0.0f, 2.0f),
  76             new OpenMBeanParameterInfoSupport("name", "descr",
  77                                               SimpleType.DATE,
  78                                               now, new Date[] {now, then}),
  79             new OpenMBeanOperationInfoSupport("name", "descr", null,
  80                                               ArrayType.getPrimitiveArrayType(
  81                                                       int[][].class),
  82                                               ACTION),
  83             new OpenMBeanOperationInfoSupport("name", "descr", null,
  84                                               ArrayType.getArrayType(
  85                                                       SimpleType.INTEGER),
  86                                               ACTION, constraints),
  87         };
  88 
  89         for (DescriptorRead x : testObjects) {
  90             OpenType descriptorType = (OpenType)
  91                     x.getDescriptor().getFieldValue("openType");
  92             OpenType openType;
  93             if (x instanceof OpenMBeanParameterInfo)
  94                 openType = ((OpenMBeanParameterInfo) x).getOpenType();
  95             else if (x instanceof OpenMBeanOperationInfo)
  96                 openType = ((OpenMBeanOperationInfo) x).getReturnOpenType();
  97             else
  98                 throw new Exception("Can't get OpenType for " + x.getClass());
  99             if (openType.equals(descriptorType))
 100                 System.out.println("OK: " + x);
 101             else {
 102                 failure("OpenType is " + openType + ", descriptor says " +
 103                         descriptorType + " for " + x);
 104             }
 105         }
 106 
 107         // Serial compatibility test:
 108         //
 109         System.out.println("Testing serial compatibility with Java "+
 110                 MBeanFeatureInfoSerialStore.SERIALIZER_VM_VERSION+
 111                 " "+
 112                 MBeanFeatureInfoSerialStore.SERIALIZER_VM_VENDOR);
 113 
 114         for (String key : MBeanFeatureInfoSerialStore.keySet() ) {
 115             MBeanFeatureInfo f = MBeanFeatureInfoSerialStore.get(key);
 116             DescriptorRead x = (DescriptorRead)f;
 117             OpenType descriptorType = (OpenType)
 118                     x.getDescriptor().getFieldValue("openType");
 119             OpenType openType;
 120             if (x instanceof OpenMBeanParameterInfo)
 121                 openType = ((OpenMBeanParameterInfo) x).getOpenType();
 122             else if (x instanceof OpenMBeanOperationInfo)
 123                 openType = ((OpenMBeanOperationInfo) x).getReturnOpenType();
 124             else
 125                 throw new Exception("Can't get OpenType for " + key +": "+
 126                         x.getClass());
 127             if (openType.equals(descriptorType))
 128                 System.out.println("OK "+key+": " + x);
 129             else {
 130                 failure("OpenType for "+key+" is " + openType +
 131                         ", descriptor says " +
 132                         descriptorType + " for " + x);
 133             }
 134 
 135         }
 136 
 137         // Test that we get an exception if "openType" in Descriptor
 138         // doesn't agree with OpenType parameter
 139         Descriptor d =
 140                 new ImmutableDescriptor(new String[] {"openType"},
 141                                         new Object[] {SimpleType.STRING});
 142         for (Type t : Type.values()) {
 143             try {
 144                 switch (t) {
 145                     case ATTR:
 146                         new OpenMBeanAttributeInfoSupport("name", "descr",
 147                                                           SimpleType.INTEGER,
 148                                                           true, true, false, d);
 149                         break;
 150                     case PARAM:
 151                         new OpenMBeanParameterInfoSupport("name", "descr",
 152                                                           SimpleType.INTEGER, d);
 153                         break;
 154                     case OPER:
 155                         new OpenMBeanOperationInfoSupport("name", "descr", null,
 156                                                           SimpleType.INTEGER,
 157                                                           ACTION, d);
 158                         break;
 159                 }
 160                 failure("did not get expected exception for " + t);
 161             } catch (IllegalArgumentException e) {
 162                 System.out.println("OK: got expected exception for " + t);
 163             } catch (Exception e) {
 164                 failure("wrong exception for " + t + ": " + e);
 165             }
 166         }
 167 
 168         if (failed != null)
 169             throw new Exception(failed);
 170         System.out.println("Test passed");
 171     }
 172 
 173     private static enum Type {ATTR, PARAM, OPER}
 174 
 175     private static void failure(String what) {
 176         System.out.println("FAILED: what");
 177         failed = what;
 178     }
 179 
 180     private static String failed;
 181 }