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