Path: blob/master/test/jdk/javax/management/descriptor/MBeanInfoInteropTest.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 620446926* @summary Check that descriptors have not broken serial interop.27* @author Eamonn McManus28*29* @run clean MBeanInfoInteropTest SerializedInfo30* @run build MBeanInfoInteropTest SerializedInfo31* @run main MBeanInfoInteropTest SerializedInfo32*/3334/*35* When run with just a classname argument ("SerializedInfo" above),36* this reads the serialized objects from that class.37* When run with the argument "generate" and a classname, say "SerializedInfo",38* this creates the class SerializedInfo.java. The idea is to do that on JDK 5.039* then run this test on the latest JDK, or vice versa. The40* copy included in this source directory was generated on JDK 5.041* so that we continue to check forward interop (serialize on JDK 5,42* deserialize on JDK 6). There is no easy way to automate backward43* interop checking.44*/4546import java.io.ByteArrayInputStream;47import java.io.ByteArrayOutputStream;48import java.io.ObjectInputStream;49import java.io.ObjectOutputStream;50import java.io.PrintWriter;51import java.io.Serializable;52import java.lang.reflect.Field;53import java.lang.reflect.Modifier;54import javax.management.Descriptor;55import javax.management.MBeanAttributeInfo;56import javax.management.MBeanConstructorInfo;57import javax.management.MBeanInfo;58import javax.management.MBeanNotificationInfo;59import static javax.management.MBeanOperationInfo.*;60import static javax.management.openmbean.SimpleType.INTEGER;61import javax.management.MBeanOperationInfo;62import javax.management.MBeanParameterInfo;63import javax.management.modelmbean.DescriptorSupport;64import javax.management.modelmbean.ModelMBeanAttributeInfo;65import javax.management.modelmbean.ModelMBeanConstructorInfo;66import javax.management.modelmbean.ModelMBeanInfoSupport;67import javax.management.modelmbean.ModelMBeanNotificationInfo;68import javax.management.modelmbean.ModelMBeanOperationInfo;69import javax.management.openmbean.OpenMBeanAttributeInfo;70import javax.management.openmbean.OpenMBeanAttributeInfoSupport;71import javax.management.openmbean.OpenMBeanConstructorInfo;72import javax.management.openmbean.OpenMBeanConstructorInfoSupport;73import javax.management.openmbean.OpenMBeanInfoSupport;74import javax.management.openmbean.OpenMBeanOperationInfo;75import javax.management.openmbean.OpenMBeanOperationInfoSupport;76import javax.management.openmbean.OpenMBeanParameterInfo;77import javax.management.openmbean.OpenMBeanParameterInfoSupport;7879public class MBeanInfoInteropTest {80public static void main(String[] args) throws Exception {81if (args.length == 2 && args[0].equals("generate"))82generate(args[1]);83else if (args.length == 1)84test(args[0]);85else {86final String usage =87"Usage: MBeanInfoInteropTest [generate] ClassName";88throw new Exception(usage);89}90}9192private static void test(String className) throws Exception {93Class<?> c = Class.forName(className);94Field f = c.getField("BYTES");95byte[] bytes = (byte[]) f.get(null);96ByteArrayInputStream bis = new ByteArrayInputStream(bytes);97ObjectInputStream ois = new ObjectInputStream(bis);98boolean matched = true;99for (Serializable s : objects) {100Object o = ois.readObject();101if (!o.equals(s)) {102showMismatch(o, s);103matched = false;104}105}106if (!matched)107throw new Exception("Read objects did not match");108System.out.println("Test passed");109}110111private static void showMismatch(Object read, Serializable expected)112throws Exception {113String name = "<unknown>";114Field[] fs = MBeanInfoInteropTest.class.getDeclaredFields();115for (Field f : fs) {116if (!Modifier.isStatic(f.getModifiers()))117continue;118Object x = f.get(null);119if (x == expected) {120name = f.getName();121break;122}123}124System.out.println("Read object mismatch for field " + name);125System.out.println("...read: " + read);126System.out.println("...expected: " + expected);127}128129private static void generate(String className) throws Exception {130System.out.println("Generating " + className + ".java");131ByteArrayOutputStream bos = new ByteArrayOutputStream();132ObjectOutputStream oos = new ObjectOutputStream(bos);133for (Serializable s : objects)134oos.writeObject(s);135oos.close();136byte[] bytes = bos.toByteArray();137PrintWriter pw = new PrintWriter(className + ".java");138pw.printf(139"// Generated by: MBeanInfoInteropTest generate %s\n" +140"import java.io.*;\n" +141"public class %s {\n" +142"public static final byte[] BYTES = {\n", className, className);143for (int i = 0; i < bytes.length; i++) {144byte b = bytes[i];145pw.printf("%d,", b);146if (i % 16 == 15)147pw.printf("\n");148}149pw.printf("\n");150pw.printf(151"};\n" +152"}\n");153pw.close();154System.out.println("...done");155}156157private static MBeanAttributeInfo mbai;158private static MBeanParameterInfo mbpi;159private static MBeanConstructorInfo mbci1, mbci2;160private static MBeanNotificationInfo mbni1, mbni2;161private static MBeanOperationInfo mboi1, mboi2;162private static MBeanInfo mbi1, mbi2;163private static OpenMBeanAttributeInfoSupport ombai1, ombai2, ombai3, ombai4;164private static OpenMBeanParameterInfoSupport ombpi1, ombpi2, ombpi3, ombpi4;165private static OpenMBeanConstructorInfoSupport ombci1, ombci2;166private static OpenMBeanOperationInfoSupport omboi1, omboi2;167private static OpenMBeanInfoSupport ombi1, ombi2;168private static ModelMBeanAttributeInfo mmbai1, mmbai2;169private static ModelMBeanConstructorInfo mmbci1, mmbci2, mmbci3, mmbci4;170private static ModelMBeanOperationInfo mmboi1, mmboi2, mmboi3, mmboi4;171private static ModelMBeanNotificationInfo mmbni1, mmbni2, mmbni3, mmbni4;172private static ModelMBeanInfoSupport mmbi1, mmbi2, mmbi3, mmbi4;173private static Serializable[] objects;174static {175try {176init();177} catch (Exception e) {178throw new IllegalArgumentException("unexpected", e);179}180}181private static void init() throws Exception {182mbai =183new MBeanAttributeInfo("name", "type", "descr", true, false, false);184mbpi =185new MBeanParameterInfo("name", "type", "descr");186MBeanParameterInfo[] params = new MBeanParameterInfo[] {mbpi};187mbci1 =188new MBeanConstructorInfo("name", "descr", null);189mbci2 =190new MBeanConstructorInfo("name", "descr", params);191mbni1 =192new MBeanNotificationInfo(null, "name", "descr");193mbni2 =194new MBeanNotificationInfo(new String[] {"type"}, "name", "descr");195mboi1 =196new MBeanOperationInfo("name", "descr", null, "type", ACTION);197mboi2 =198new MBeanOperationInfo("name", "descr", params, "type", INFO);199mbi1 =200new MBeanInfo("class", "descr", null, null, null, null);201mbi2 =202new MBeanInfo(203"class", "descr",204new MBeanAttributeInfo[] {mbai},205new MBeanConstructorInfo[] {mbci1, mbci2},206new MBeanOperationInfo[] {mboi1, mboi2},207new MBeanNotificationInfo[] {mbni1, mbni2});208209ombai1 =210new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,211true, false, false);212ombai2 =213new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,214true, false, false, 5);215ombai3 =216new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,217true, false, false, 5, 1, 6);218ombai4 =219new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,220true, false, false, 5,221new Integer[] {2, 3, 5, 7});222ombpi1 =223new OpenMBeanParameterInfoSupport("name1", "descr", INTEGER);224ombpi2 =225new OpenMBeanParameterInfoSupport("name2", "descr", INTEGER, 5);226ombpi3 =227new OpenMBeanParameterInfoSupport("name3", "descr", INTEGER, 5, 1, 6);228ombpi4 =229new OpenMBeanParameterInfoSupport("name4", "descr", INTEGER, 5,230new Integer[] {2, 3, 5, 7});231OpenMBeanParameterInfo[] oparams = {ombpi1, ombpi2, ombpi3, ombpi4};232ombci1 =233new OpenMBeanConstructorInfoSupport("name", "descr", null);234ombci2 =235new OpenMBeanConstructorInfoSupport("name", "descr", oparams);236omboi1 =237new OpenMBeanOperationInfoSupport("name", "descr", null,238INTEGER, ACTION);239omboi2 =240new OpenMBeanOperationInfoSupport("name", "descr", oparams,241INTEGER, ACTION);242ombi1 =243new OpenMBeanInfoSupport("class", "descr", null, null, null, null);244ombi2 =245new OpenMBeanInfoSupport(246"class", "descr",247new OpenMBeanAttributeInfo[] {ombai1, ombai2, ombai3, ombai4},248new OpenMBeanConstructorInfo[] {ombci1, ombci2},249new OpenMBeanOperationInfo[] {omboi1, omboi2},250new MBeanNotificationInfo[] {mbni1, mbni2});251252Descriptor attrd = new DescriptorSupport(new String[] {253"name=name", "descriptorType=attribute",254});255mmbai1 =256new ModelMBeanAttributeInfo("name", "type", "descr",257true, false, false);258mmbai2 =259new ModelMBeanAttributeInfo("name", "type", "descr",260true, false, false, attrd);261Descriptor constrd = new DescriptorSupport(new String[] {262"name=name", "descriptorType=operation", "role=constructor",263});264mmbci1 =265new ModelMBeanConstructorInfo("name", "descr", null);266mmbci2 =267new ModelMBeanConstructorInfo("name", "descr", null, constrd);268mmbci3 =269new ModelMBeanConstructorInfo("name", "descr", params);270mmbci4 =271new ModelMBeanConstructorInfo("name", "descr", params, constrd);272Descriptor operd = new DescriptorSupport(new String[] {273"name=name", "descriptorType=operation",274});275mmboi1 =276new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION);277mmboi2 =278new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION,279operd);280mmboi3 =281new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION);282mmboi4 =283new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION,284operd);285Descriptor notifd = new DescriptorSupport(new String[] {286"name=name", "descriptorType=notification",287});288mmbni1 =289new ModelMBeanNotificationInfo(null, "name", "descr");290mmbni2 =291new ModelMBeanNotificationInfo(null, "name", "descr", notifd);292mmbni3 =293new ModelMBeanNotificationInfo(new String[] {"type"}, "name", "descr");294mmbni4 =295new ModelMBeanNotificationInfo(new String[] {"type"}, "name",296"descr", notifd);297Descriptor mbeand = new DescriptorSupport(new String[] {298"name=name", "descriptorType=mbean",299});300mmbi1 =301new ModelMBeanInfoSupport("class", "descr", null, null, null, null);302mmbi2 =303new ModelMBeanInfoSupport("class", "descr", null, null, null, null,304mbeand);305mmbi3 =306new ModelMBeanInfoSupport(307"class", "descr",308new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},309new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},310new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},311new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4});312mmbi4 =313new ModelMBeanInfoSupport(314"class", "descr",315new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},316new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},317new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},318new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4},319mbeand);320321objects = new Serializable[] {322mbai, mbpi, mbci1, mbci2, mbni1, mbni2, mboi1, mboi2, mbi1, mbi2,323324ombai1, ombai2, ombai3, ombai4,325ombpi1, ombpi2, ombpi3, ombpi4,326ombci1, ombci2,327omboi1, omboi2,328ombi1, ombi2,329330mmbai1, mmbai2,331mmbci1, mmbci2, mmbci3, mmbci4,332mmboi1, mmboi2, mmboi3, mmboi4,333mmbni1, mmbni2, mmbni3, mmbni4,334};335}336}337338339