Path: blob/master/test/jdk/javax/management/MBeanInfo/NullInfoArraysTest.java
41152 views
/*1* Copyright (c) 2004, 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 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*30* @run clean NullInfoArraysTest31* @run build NullInfoArraysTest32* @run main NullInfoArraysTest33*/3435import java.io.*;36import javax.management.*;37import javax.management.modelmbean.*;38import javax.management.openmbean.*;3940public class NullInfoArraysTest {41public static void main(String[] args) throws Exception {42if (args.length > 0 && args[0].equals("write"))43writeSerializedForms();44else45testSerializedForms();46}4748private static void testSerializedForms() throws Exception {49byte[][] serializedMBeanInfos =50SerializedMBeanInfo.serializedMBeanInfos;51for (int i = 0; i < serializedMBeanInfos.length; i++) {52byte[] serializedMBeanInfo = serializedMBeanInfos[i];53ByteArrayInputStream bis =54new ByteArrayInputStream(serializedMBeanInfo);55ObjectInputStream ois = new ObjectInputStream(bis);56MBeanInfo mbi = (MBeanInfo) ois.readObject();5758System.out.println("Testing a " +59mbi.getClass().getName() + "...");6061if (mbi.getAttributes() == null ||62mbi.getOperations() == null ||63mbi.getConstructors() == null ||64mbi.getNotifications() == null)65throw new Exception("At least one getter returned null");6667System.out.println("OK");68}6970System.out.println("Test passed");71}7273/* This method is intended to be invoked when constructing the74test for the first time, with JMX 1.1 RI in the classpath. It75constructs the SerializedMBeanInfo.java source file. There is76of course a chicken-and-egg problem for compiling: the first77time we built this test, we supplied a trivial78SerializedMBeanInfo.java with an empty array in the79serializedMBeanInfos field. */80private static void writeSerializedForms() throws Exception {81OutputStream fos = new FileOutputStream("SerializedMBeanInfo.java");82PrintWriter w = new PrintWriter(fos);83w.println("// Generated by NullInfoArraysTest - do not edit");84w.println();85w.println("public class SerializedMBeanInfo {");86w.println(" public static final byte[][] serializedMBeanInfos = {");87writeSerial(w, new MBeanInfo(null, null, null, null, null, null));88writeSerial(w, new ModelMBeanInfoSupport(null, null, null, null, null,89null, null));90writeSerial(w, new OpenMBeanInfoSupport(null, null, null, null, null,91null));92w.println(" };");93w.println("}");94w.close();95fos.close();96System.out.println("Wrote SerializedMBeanInfo.java");97}9899private static void writeSerial(PrintWriter w, Object o) throws Exception {100ByteArrayOutputStream bos = new ByteArrayOutputStream();101ObjectOutputStream oos = new ObjectOutputStream(bos);102oos.writeObject(o);103oos.close();104byte[] bytes = bos.toByteArray();105w.print(" {");106for (int i = 0; i < bytes.length; i++) {107w.print(bytes[i]);108w.print(", ");109}110w.println("},");111}112}113114115