Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanInfo/NullInfoArraysTest.java
38840 views
/*1* Copyright (c) 2004, 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 505624826* @summary Test that an MBeanInfo works even if it is deserialized from27* an implementation where its array fields can be null.28* @author Eamonn McManus29* @run clean NullInfoArraysTest30* @run build NullInfoArraysTest31* @run main NullInfoArraysTest32*/3334import java.io.*;35import javax.management.*;36import javax.management.modelmbean.*;37import javax.management.openmbean.*;3839public class NullInfoArraysTest {40public static void main(String[] args) throws Exception {41if (args.length > 0 && args[0].equals("write"))42writeSerializedForms();43else44testSerializedForms();45}4647private static void testSerializedForms() throws Exception {48byte[][] serializedMBeanInfos =49SerializedMBeanInfo.serializedMBeanInfos;50for (int i = 0; i < serializedMBeanInfos.length; i++) {51byte[] serializedMBeanInfo = serializedMBeanInfos[i];52ByteArrayInputStream bis =53new ByteArrayInputStream(serializedMBeanInfo);54ObjectInputStream ois = new ObjectInputStream(bis);55MBeanInfo mbi = (MBeanInfo) ois.readObject();5657System.out.println("Testing a " +58mbi.getClass().getName() + "...");5960if (mbi.getAttributes() == null ||61mbi.getOperations() == null ||62mbi.getConstructors() == null ||63mbi.getNotifications() == null)64throw new Exception("At least one getter returned null");6566System.out.println("OK");67}6869System.out.println("Test passed");70}7172/* This method is intended to be invoked when constructing the73test for the first time, with JMX 1.1 RI in the classpath. It74constructs the SerializedMBeanInfo.java source file. There is75of course a chicken-and-egg problem for compiling: the first76time we built this test, we supplied a trivial77SerializedMBeanInfo.java with an empty array in the78serializedMBeanInfos field. */79private static void writeSerializedForms() throws Exception {80OutputStream fos = new FileOutputStream("SerializedMBeanInfo.java");81PrintWriter w = new PrintWriter(fos);82w.println("// Generated by NullInfoArraysTest - do not edit");83w.println();84w.println("public class SerializedMBeanInfo {");85w.println(" public static final byte[][] serializedMBeanInfos = {");86writeSerial(w, new MBeanInfo(null, null, null, null, null, null));87writeSerial(w, new ModelMBeanInfoSupport(null, null, null, null, null,88null, null));89writeSerial(w, new OpenMBeanInfoSupport(null, null, null, null, null,90null));91w.println(" };");92w.println("}");93w.close();94fos.close();95System.out.println("Wrote SerializedMBeanInfo.java");96}9798private static void writeSerial(PrintWriter w, Object o) throws Exception {99ByteArrayOutputStream bos = new ByteArrayOutputStream();100ObjectOutputStream oos = new ObjectOutputStream(bos);101oos.writeObject(o);102oos.close();103byte[] bytes = bos.toByteArray();104w.print(" {");105for (int i = 0; i < bytes.length; i++) {106w.print(bytes[i]);107w.print(", ");108}109w.println("},");110}111}112113114