Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/descriptor/MBeanInfoInteropTest.java
38839 views
/*1* Copyright (c) 2005, 2007, 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* @run clean MBeanInfoInteropTest SerializedInfo29* @run build MBeanInfoInteropTest SerializedInfo30* @run main MBeanInfoInteropTest SerializedInfo31*/3233/*34* When run with just a classname argument ("SerializedInfo" above),35* this reads the serialized objects from that class.36* When run with the argument "generate" and a classname, say "SerializedInfo",37* this creates the class SerializedInfo.java. The idea is to do that on JDK 5.038* then run this test on the latest JDK, or vice versa. The39* copy included in this source directory was generated on JDK 5.040* so that we continue to check forward interop (serialize on JDK 5,41* deserialize on JDK 6). There is no easy way to automate backward42* interop checking.43*/4445import java.io.ByteArrayInputStream;46import java.io.ByteArrayOutputStream;47import java.io.ObjectInputStream;48import java.io.ObjectOutputStream;49import java.io.PrintWriter;50import java.io.Serializable;51import java.lang.reflect.Field;52import java.lang.reflect.Modifier;53import javax.management.Descriptor;54import javax.management.MBeanAttributeInfo;55import javax.management.MBeanConstructorInfo;56import javax.management.MBeanInfo;57import javax.management.MBeanNotificationInfo;58import static javax.management.MBeanOperationInfo.*;59import static javax.management.openmbean.SimpleType.INTEGER;60import javax.management.MBeanOperationInfo;61import javax.management.MBeanParameterInfo;62import javax.management.modelmbean.DescriptorSupport;63import javax.management.modelmbean.ModelMBeanAttributeInfo;64import javax.management.modelmbean.ModelMBeanConstructorInfo;65import javax.management.modelmbean.ModelMBeanInfoSupport;66import javax.management.modelmbean.ModelMBeanNotificationInfo;67import javax.management.modelmbean.ModelMBeanOperationInfo;68import javax.management.openmbean.OpenMBeanAttributeInfo;69import javax.management.openmbean.OpenMBeanAttributeInfoSupport;70import javax.management.openmbean.OpenMBeanConstructorInfo;71import javax.management.openmbean.OpenMBeanConstructorInfoSupport;72import javax.management.openmbean.OpenMBeanInfoSupport;73import javax.management.openmbean.OpenMBeanOperationInfo;74import javax.management.openmbean.OpenMBeanOperationInfoSupport;75import javax.management.openmbean.OpenMBeanParameterInfo;76import javax.management.openmbean.OpenMBeanParameterInfoSupport;7778public class MBeanInfoInteropTest {79public static void main(String[] args) throws Exception {80if (args.length == 2 && args[0].equals("generate"))81generate(args[1]);82else if (args.length == 1)83test(args[0]);84else {85final String usage =86"Usage: MBeanInfoInteropTest [generate] ClassName";87throw new Exception(usage);88}89}9091private static void test(String className) throws Exception {92Class<?> c = Class.forName(className);93Field f = c.getField("BYTES");94byte[] bytes = (byte[]) f.get(null);95ByteArrayInputStream bis = new ByteArrayInputStream(bytes);96ObjectInputStream ois = new ObjectInputStream(bis);97boolean matched = true;98for (Serializable s : objects) {99Object o = ois.readObject();100if (!o.equals(s)) {101showMismatch(o, s);102matched = false;103}104}105if (!matched)106throw new Exception("Read objects did not match");107System.out.println("Test passed");108}109110private static void showMismatch(Object read, Serializable expected)111throws Exception {112String name = "<unknown>";113Field[] fs = MBeanInfoInteropTest.class.getDeclaredFields();114for (Field f : fs) {115if (!Modifier.isStatic(f.getModifiers()))116continue;117Object x = f.get(null);118if (x == expected) {119name = f.getName();120break;121}122}123System.out.println("Read object mismatch for field " + name);124System.out.println("...read: " + read);125System.out.println("...expected: " + expected);126}127128private static void generate(String className) throws Exception {129System.out.println("Generating " + className + ".java");130ByteArrayOutputStream bos = new ByteArrayOutputStream();131ObjectOutputStream oos = new ObjectOutputStream(bos);132for (Serializable s : objects)133oos.writeObject(s);134oos.close();135byte[] bytes = bos.toByteArray();136PrintWriter pw = new PrintWriter(className + ".java");137pw.printf(138"// Generated by: MBeanInfoInteropTest generate %s\n" +139"import java.io.*;\n" +140"public class %s {\n" +141"public static final byte[] BYTES = {\n", className, className);142for (int i = 0; i < bytes.length; i++) {143byte b = bytes[i];144pw.printf("%d,", b);145if (i % 16 == 15)146pw.printf("\n");147}148pw.printf("\n");149pw.printf(150"};\n" +151"}\n");152pw.close();153System.out.println("...done");154}155156private static MBeanAttributeInfo mbai;157private static MBeanParameterInfo mbpi;158private static MBeanConstructorInfo mbci1, mbci2;159private static MBeanNotificationInfo mbni1, mbni2;160private static MBeanOperationInfo mboi1, mboi2;161private static MBeanInfo mbi1, mbi2;162private static OpenMBeanAttributeInfoSupport ombai1, ombai2, ombai3, ombai4;163private static OpenMBeanParameterInfoSupport ombpi1, ombpi2, ombpi3, ombpi4;164private static OpenMBeanConstructorInfoSupport ombci1, ombci2;165private static OpenMBeanOperationInfoSupport omboi1, omboi2;166private static OpenMBeanInfoSupport ombi1, ombi2;167private static ModelMBeanAttributeInfo mmbai1, mmbai2;168private static ModelMBeanConstructorInfo mmbci1, mmbci2, mmbci3, mmbci4;169private static ModelMBeanOperationInfo mmboi1, mmboi2, mmboi3, mmboi4;170private static ModelMBeanNotificationInfo mmbni1, mmbni2, mmbni3, mmbni4;171private static ModelMBeanInfoSupport mmbi1, mmbi2, mmbi3, mmbi4;172private static Serializable[] objects;173static {174try {175init();176} catch (Exception e) {177throw new IllegalArgumentException("unexpected", e);178}179}180private static void init() throws Exception {181mbai =182new MBeanAttributeInfo("name", "type", "descr", true, false, false);183mbpi =184new MBeanParameterInfo("name", "type", "descr");185MBeanParameterInfo[] params = new MBeanParameterInfo[] {mbpi};186mbci1 =187new MBeanConstructorInfo("name", "descr", null);188mbci2 =189new MBeanConstructorInfo("name", "descr", params);190mbni1 =191new MBeanNotificationInfo(null, "name", "descr");192mbni2 =193new MBeanNotificationInfo(new String[] {"type"}, "name", "descr");194mboi1 =195new MBeanOperationInfo("name", "descr", null, "type", ACTION);196mboi2 =197new MBeanOperationInfo("name", "descr", params, "type", INFO);198mbi1 =199new MBeanInfo("class", "descr", null, null, null, null);200mbi2 =201new MBeanInfo(202"class", "descr",203new MBeanAttributeInfo[] {mbai},204new MBeanConstructorInfo[] {mbci1, mbci2},205new MBeanOperationInfo[] {mboi1, mboi2},206new MBeanNotificationInfo[] {mbni1, mbni2});207208ombai1 =209new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,210true, false, false);211ombai2 =212new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,213true, false, false, 5);214ombai3 =215new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,216true, false, false, 5, 1, 6);217ombai4 =218new OpenMBeanAttributeInfoSupport("name", "descr", INTEGER,219true, false, false, 5,220new Integer[] {2, 3, 5, 7});221ombpi1 =222new OpenMBeanParameterInfoSupport("name1", "descr", INTEGER);223ombpi2 =224new OpenMBeanParameterInfoSupport("name2", "descr", INTEGER, 5);225ombpi3 =226new OpenMBeanParameterInfoSupport("name3", "descr", INTEGER, 5, 1, 6);227ombpi4 =228new OpenMBeanParameterInfoSupport("name4", "descr", INTEGER, 5,229new Integer[] {2, 3, 5, 7});230OpenMBeanParameterInfo[] oparams = {ombpi1, ombpi2, ombpi3, ombpi4};231ombci1 =232new OpenMBeanConstructorInfoSupport("name", "descr", null);233ombci2 =234new OpenMBeanConstructorInfoSupport("name", "descr", oparams);235omboi1 =236new OpenMBeanOperationInfoSupport("name", "descr", null,237INTEGER, ACTION);238omboi2 =239new OpenMBeanOperationInfoSupport("name", "descr", oparams,240INTEGER, ACTION);241ombi1 =242new OpenMBeanInfoSupport("class", "descr", null, null, null, null);243ombi2 =244new OpenMBeanInfoSupport(245"class", "descr",246new OpenMBeanAttributeInfo[] {ombai1, ombai2, ombai3, ombai4},247new OpenMBeanConstructorInfo[] {ombci1, ombci2},248new OpenMBeanOperationInfo[] {omboi1, omboi2},249new MBeanNotificationInfo[] {mbni1, mbni2});250251Descriptor attrd = new DescriptorSupport(new String[] {252"name=name", "descriptorType=attribute",253});254mmbai1 =255new ModelMBeanAttributeInfo("name", "type", "descr",256true, false, false);257mmbai2 =258new ModelMBeanAttributeInfo("name", "type", "descr",259true, false, false, attrd);260Descriptor constrd = new DescriptorSupport(new String[] {261"name=name", "descriptorType=operation", "role=constructor",262});263mmbci1 =264new ModelMBeanConstructorInfo("name", "descr", null);265mmbci2 =266new ModelMBeanConstructorInfo("name", "descr", null, constrd);267mmbci3 =268new ModelMBeanConstructorInfo("name", "descr", params);269mmbci4 =270new ModelMBeanConstructorInfo("name", "descr", params, constrd);271Descriptor operd = new DescriptorSupport(new String[] {272"name=name", "descriptorType=operation",273});274mmboi1 =275new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION);276mmboi2 =277new ModelMBeanOperationInfo("name", "descr", null, "type", ACTION,278operd);279mmboi3 =280new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION);281mmboi4 =282new ModelMBeanOperationInfo("name", "descr", params, "type", ACTION,283operd);284Descriptor notifd = new DescriptorSupport(new String[] {285"name=name", "descriptorType=notification",286});287mmbni1 =288new ModelMBeanNotificationInfo(null, "name", "descr");289mmbni2 =290new ModelMBeanNotificationInfo(null, "name", "descr", notifd);291mmbni3 =292new ModelMBeanNotificationInfo(new String[] {"type"}, "name", "descr");293mmbni4 =294new ModelMBeanNotificationInfo(new String[] {"type"}, "name",295"descr", notifd);296Descriptor mbeand = new DescriptorSupport(new String[] {297"name=name", "descriptorType=mbean",298});299mmbi1 =300new ModelMBeanInfoSupport("class", "descr", null, null, null, null);301mmbi2 =302new ModelMBeanInfoSupport("class", "descr", null, null, null, null,303mbeand);304mmbi3 =305new ModelMBeanInfoSupport(306"class", "descr",307new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},308new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},309new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},310new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4});311mmbi4 =312new ModelMBeanInfoSupport(313"class", "descr",314new ModelMBeanAttributeInfo[] {mmbai1, mmbai2},315new ModelMBeanConstructorInfo[] {mmbci1, mmbci2, mmbci3, mmbci4},316new ModelMBeanOperationInfo[] {mmboi1, mmboi2, mmboi3, mmboi4},317new ModelMBeanNotificationInfo[] {mmbni1, mmbni2, mmbni3, mmbni4},318mbeand);319320objects = new Serializable[] {321mbai, mbpi, mbci1, mbci2, mbni1, mbni2, mboi1, mboi2, mbi1, mbi2,322323ombai1, ombai2, ombai3, ombai4,324ombpi1, ombpi2, ombpi3, ombpi4,325ombci1, ombci2,326omboi1, omboi2,327ombi1, ombi2,328329mmbai1, mmbai2,330mmbci1, mmbci2, mmbci3, mmbci4,331mmboi1, mmboi2, mmboi3, mmboi4,332mmbni1, mmbni2, mmbni3, mmbni4,333};334}335}336337338