0N/A/*
2362N/A * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6204469
0N/A * @summary Check that descriptors have not broken serial interop.
0N/A * @author Eamonn McManus
0N/A * @run clean MBeanInfoInteropTest SerializedInfo
0N/A * @run build MBeanInfoInteropTest SerializedInfo
0N/A * @run main MBeanInfoInteropTest SerializedInfo
0N/A */
0N/A
0N/A/*
0N/A * When run with just a classname argument ("SerializedInfo" above),
0N/A * this reads the serialized objects from that class.
0N/A * When run with the argument "generate" and a classname, say "SerializedInfo",
0N/A * this creates the class SerializedInfo.java. The idea is to do that on JDK 5.0
0N/A * then run this test on the latest JDK, or vice versa. The
0N/A * copy included in this source directory was generated on JDK 5.0
0N/A * so that we continue to check forward interop (serialize on JDK 5,
0N/A * deserialize on JDK 6). There is no easy way to automate backward
0N/A * interop checking.
0N/A */
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.io.Serializable;
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.lang.reflect.Modifier;
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.MBeanAttributeInfo;
0N/Aimport javax.management.MBeanConstructorInfo;
0N/Aimport javax.management.MBeanInfo;
0N/Aimport javax.management.MBeanNotificationInfo;
0N/Aimport static javax.management.MBeanOperationInfo.*;
0N/Aimport static javax.management.openmbean.SimpleType.INTEGER;
0N/Aimport javax.management.MBeanOperationInfo;
0N/Aimport javax.management.MBeanParameterInfo;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanAttributeInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanConstructorInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfoSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanNotificationInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanOperationInfo;
0N/Aimport javax.management.openmbean.OpenMBeanAttributeInfo;
0N/Aimport javax.management.openmbean.OpenMBeanAttributeInfoSupport;
0N/Aimport javax.management.openmbean.OpenMBeanConstructorInfo;
0N/Aimport javax.management.openmbean.OpenMBeanConstructorInfoSupport;
0N/Aimport javax.management.openmbean.OpenMBeanInfoSupport;
0N/Aimport javax.management.openmbean.OpenMBeanOperationInfo;
0N/Aimport javax.management.openmbean.OpenMBeanOperationInfoSupport;
0N/Aimport javax.management.openmbean.OpenMBeanParameterInfo;
0N/Aimport javax.management.openmbean.OpenMBeanParameterInfoSupport;
0N/A
0N/Apublic class MBeanInfoInteropTest {
0N/A public static void main(String[] args) throws Exception {
0N/A if (args.length == 2 && args[0].equals("generate"))
0N/A generate(args[1]);
0N/A else if (args.length == 1)
0N/A test(args[0]);
0N/A else {
0N/A final String usage =
0N/A "Usage: MBeanInfoInteropTest [generate] ClassName";
0N/A throw new Exception(usage);
0N/A }
0N/A }
0N/A
0N/A private static void test(String className) throws Exception {
0N/A Class<?> c = Class.forName(className);
0N/A Field f = c.getField("BYTES");
0N/A byte[] bytes = (byte[]) f.get(null);
0N/A ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
0N/A ObjectInputStream ois = new ObjectInputStream(bis);
0N/A boolean matched = true;
0N/A for (Serializable s : objects) {
0N/A Object o = ois.readObject();
0N/A if (!o.equals(s)) {
0N/A showMismatch(o, s);
0N/A matched = false;
0N/A }
0N/A }
0N/A if (!matched)
0N/A throw new Exception("Read objects did not match");
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A private static void showMismatch(Object read, Serializable expected)
0N/A throws Exception {
0N/A String name = "<unknown>";
0N/A Field[] fs = MBeanInfoInteropTest.class.getDeclaredFields();
0N/A for (Field f : fs) {
0N/A if (!Modifier.isStatic(f.getModifiers()))
0N/A continue;
0N/A Object x = f.get(null);
0N/A if (x == expected) {
0N/A name = f.getName();
0N/A break;
0N/A }
0N/A }
0N/A System.out.println("Read object mismatch for field " + name);
0N/A System.out.println("...read: " + read);
0N/A System.out.println("...expected: " + expected);
0N/A }
0N/A
0N/A private static void generate(String className) throws Exception {
0N/A System.out.println("Generating " + className + ".java");
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oos = new ObjectOutputStream(bos);
0N/A for (Serializable s : objects)
0N/A oos.writeObject(s);
0N/A oos.close();
0N/A byte[] bytes = bos.toByteArray();
0N/A PrintWriter pw = new PrintWriter(className + ".java");
0N/A pw.printf(
0N/A "// Generated by: MBeanInfoInteropTest generate %s\n" +
0N/A "import java.io.*;\n" +
0N/A "public class %s {\n" +
0N/A "public static final byte[] BYTES = {\n", className, className);
0N/A for (int i = 0; i < bytes.length; i++) {
0N/A byte b = bytes[i];
0N/A pw.printf("%d,", b);
0N/A if (i % 16 == 15)
0N/A pw.printf("\n");
0N/A }
0N/A pw.printf("\n");
0N/A pw.printf(
0N/A "};\n" +
0N/A "}\n");
0N/A pw.close();
0N/A System.out.println("...done");
0N/A }
0N/A
0N/A private static MBeanAttributeInfo mbai;
0N/A private static MBeanParameterInfo mbpi;
0N/A private static MBeanConstructorInfo mbci1, mbci2;
0N/A private static MBeanNotificationInfo mbni1, mbni2;
0N/A private static MBeanOperationInfo mboi1, mboi2;
0N/A private static MBeanInfo mbi1, mbi2;
0N/A private static OpenMBeanAttributeInfoSupport ombai1, ombai2, ombai3, ombai4;
0N/A private static OpenMBeanParameterInfoSupport ombpi1, ombpi2, ombpi3, ombpi4;
0N/A private static OpenMBeanConstructorInfoSupport ombci1, ombci2;
0N/A private static OpenMBeanOperationInfoSupport omboi1, omboi2;
0N/A private static OpenMBeanInfoSupport ombi1, ombi2;
0N/A private static ModelMBeanAttributeInfo mmbai1, mmbai2;
0N/A private static ModelMBeanConstructorInfo mmbci1, mmbci2, mmbci3, mmbci4;
0N/A private static ModelMBeanOperationInfo mmboi1, mmboi2, mmboi3, mmboi4;
0N/A private static ModelMBeanNotificationInfo mmbni1, mmbni2, mmbni3, mmbni4;
0N/A private static ModelMBeanInfoSupport mmbi1, mmbi2, mmbi3, mmbi4;
0N/A private static Serializable[] objects;
0N/A static {
0N/A try {
0N/A init();
0N/A } catch (Exception e) {
0N/A throw new IllegalArgumentException("unexpected", e);
0N/A }
0N/A }
0N/A private static void init() throws Exception {
0N/A mbai =
0N/A new MBeanAttributeInfo("name", "type", "descr", true, false, false);
0N/A mbpi =
0N/A new MBeanParameterInfo("name", "type", "descr");
0N/A MBeanParameterInfo[] params = new MBeanParameterInfo[] {mbpi};
0N/A mbci1 =
0N/A new MBeanConstructorInfo("name", "descr", null);
0N/A mbci2 =
0N/A new MBeanConstructorInfo("name", "descr", params);
0N/A mbni1 =
0N/A new MBeanNotificationInfo(null, "name", "descr");
0N/A mbni2 =
0N/A new MBeanNotificationInfo(new String[] {"type"}, "name", "descr");
0N/A mboi1 =
0N/A new MBeanOperationInfo("name", "descr", null, "type", ACTION);
0N/A mboi2 =
0N/A new MBeanOperationInfo("name", "descr", params, "type", INFO);
0N/A mbi1 =
0N/A new MBeanInfo("class", "descr", null, null, null, null);
0N/A mbi2 =
0N/A new MBeanInfo(
0N/A "class", "descr",
0N/A new MBeanAttributeInfo[] {mbai},
0N/A new MBeanConstructorInfo[] {mbci1, mbci2},
0N/A new MBeanOperationInfo[] {mboi1, mboi2},
0N/A new MBeanNotificationInfo[] {mbni1, mbni2});
0N/A
0N/A ombai1 =
0N/A new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,
0N/A true, false, false);
0N/A ombai2 =
0N/A new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,
0N/A true, false, false, 5);
0N/A ombai3 =
0N/A new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,
0N/A true, false, false, 5, 1, 6);
0N/A ombai4 =
0N/A new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,
0N/A true, false, false, 5,
0N/A new Integer[] {2, 3, 5, 7});
0N/A ombpi1 =
0N/A new OpenMBeanParameterInfoSupport("name1", "descr", INTEGER);
0N/A ombpi2 =
0N/A new OpenMBeanParameterInfoSupport("name2", "descr", INTEGER, 5);
0N/A ombpi3 =
0N/A new OpenMBeanParameterInfoSupport("name3", "descr", INTEGER, 5, 1, 6);
0N/A ombpi4 =
0N/A new OpenMBeanParameterInfoSupport("name4", "descr", INTEGER, 5,
0N/A new Integer[] {2, 3, 5, 7});
0N/A OpenMBeanParameterInfo[] oparams = {ombpi1, ombpi2, ombpi3, ombpi4};
0N/A ombci1 =
0N/A new OpenMBeanConstructorInfoSupport("name", "descr", null);
0N/A ombci2 =
0N/A new OpenMBeanConstructorInfoSupport("name", "descr", oparams);
0N/A omboi1 =
0N/A new OpenMBeanOperationInfoSupport("name", "descr", null,
0N/A INTEGER, ACTION);
0N/A omboi2 =
0N/A new OpenMBeanOperationInfoSupport("name", "descr", oparams,
0N/A INTEGER, ACTION);
0N/A ombi1 =
0N/A new OpenMBeanInfoSupport("class", "descr", null, null, null, null);
0N/A ombi2 =
0N/A new OpenMBeanInfoSupport(
0N/A "class", "descr",
0N/A new OpenMBeanAttributeInfo[] {ombai1, ombai2, ombai3, ombai4},
0N/A new OpenMBeanConstructorInfo[] {ombci1, ombci2},
0N/A new OpenMBeanOperationInfo[] {omboi1, omboi2},
0N/A new MBeanNotificationInfo[] {mbni1, mbni2});
0N/A
0N/A Descriptor attrd = new DescriptorSupport(new String[] {
0N/A "name=name", "descriptorType=attribute",
0N/A });
0N/A mmbai1 =
0N/A new ModelMBeanAttributeInfo("name", "type", "descr",
0N/A true, false, false);
0N/A mmbai2 =
0N/A new ModelMBeanAttributeInfo("name", "type", "descr",
0N/A true, false, false, attrd);
0N/A Descriptor constrd = new DescriptorSupport(new String[] {
0N/A "name=name", "descriptorType=operation", "role=constructor",
0N/A });
0N/A mmbci1 =
0N/A new ModelMBeanConstructorInfo("name", "descr", null);
0N/A mmbci2 =
0N/A new ModelMBeanConstructorInfo("name", "descr", null, constrd);
0N/A mmbci3 =
0N/A new ModelMBeanConstructorInfo("name", "descr", params);
0N/A mmbci4 =
0N/A new ModelMBeanConstructorInfo("name", "descr", params, constrd);
0N/A Descriptor operd = new DescriptorSupport(new String[] {
0N/A "name=name", "descriptorType=operation",
0N/A });
0N/A mmboi1 =
0N/A new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION);
0N/A mmboi2 =
0N/A new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION,
0N/A operd);
0N/A mmboi3 =
0N/A new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION);
0N/A mmboi4 =
0N/A new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION,
0N/A operd);
0N/A Descriptor notifd = new DescriptorSupport(new String[] {
0N/A "name=name", "descriptorType=notification",
0N/A });
0N/A mmbni1 =
0N/A new ModelMBeanNotificationInfo(null, "name", "descr");
0N/A mmbni2 =
0N/A new ModelMBeanNotificationInfo(null, "name", "descr", notifd);
0N/A mmbni3 =
0N/A new ModelMBeanNotificationInfo(new String[] {"type"}, "name", "descr");
0N/A mmbni4 =
0N/A new ModelMBeanNotificationInfo(new String[] {"type"}, "name",
0N/A "descr", notifd);
0N/A Descriptor mbeand = new DescriptorSupport(new String[] {
0N/A "name=name", "descriptorType=mbean",
0N/A });
0N/A mmbi1 =
0N/A new ModelMBeanInfoSupport("class", "descr", null, null, null, null);
0N/A mmbi2 =
0N/A new ModelMBeanInfoSupport("class", "descr", null, null, null, null,
0N/A mbeand);
0N/A mmbi3 =
0N/A new ModelMBeanInfoSupport(
0N/A "class", "descr",
0N/A new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},
0N/A new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},
0N/A new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},
0N/A new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4});
0N/A mmbi4 =
0N/A new ModelMBeanInfoSupport(
0N/A "class", "descr",
0N/A new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},
0N/A new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},
0N/A new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},
0N/A new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4},
0N/A mbeand);
0N/A
0N/A objects = new Serializable[] {
0N/A mbai, mbpi, mbci1, mbci2, mbni1, mbni2, mboi1, mboi2, mbi1, mbi2,
0N/A
0N/A ombai1, ombai2, ombai3, ombai4,
0N/A ombpi1, ombpi2, ombpi3, ombpi4,
0N/A ombci1, ombci2,
0N/A omboi1, omboi2,
0N/A ombi1, ombi2,
0N/A
0N/A mmbai1, mmbai2,
0N/A mmbci1, mmbci2, mmbci3, mmbci4,
0N/A mmboi1, mmboi2, mmboi3, mmboi4,
0N/A mmbni1, mmbni2, mmbni3, mmbni4,
0N/A };
0N/A }
0N/A}