1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5056248 27 * @summary Test that an MBeanInfo works even if it is deserialized from 28 * an implementation where its array fields can be null. 29 * @author Eamonn McManus 30 * @run clean NullInfoArraysTest 31 * @run build NullInfoArraysTest 32 * @run main NullInfoArraysTest 33 */ 34 35 import java.io.*; 36 import javax.management.*; 37 import javax.management.modelmbean.*; 38 import javax.management.openmbean.*; 39 40 public class NullInfoArraysTest { 41 public static void main(String[] args) throws Exception { 42 if (args.length > 0 && args[0].equals("write")) 43 writeSerializedForms(); 44 else 45 testSerializedForms(); 46 } 47 48 private static void testSerializedForms() throws Exception { 49 byte[][] serializedMBeanInfos = 50 SerializedMBeanInfo.serializedMBeanInfos; 51 for (int i = 0; i < serializedMBeanInfos.length; i++) { 52 byte[] serializedMBeanInfo = serializedMBeanInfos[i]; 53 ByteArrayInputStream bis = 54 new ByteArrayInputStream(serializedMBeanInfo); 55 ObjectInputStream ois = new ObjectInputStream(bis); 56 MBeanInfo mbi = (MBeanInfo) ois.readObject(); 57 58 System.out.println("Testing a " + 59 mbi.getClass().getName() + "..."); 60 61 if (mbi.getAttributes() == null || 62 mbi.getOperations() == null || 63 mbi.getConstructors() == null || 64 mbi.getNotifications() == null) 65 throw new Exception("At least one getter returned null"); 66 67 System.out.println("OK"); 68 } 69 70 System.out.println("Test passed"); 71 } 72 73 /* This method is intended to be invoked when constructing the 74 test for the first time, with JMX 1.1 RI in the classpath. It 75 constructs the SerializedMBeanInfo.java source file. There is 76 of course a chicken-and-egg problem for compiling: the first 77 time we built this test, we supplied a trivial 78 SerializedMBeanInfo.java with an empty array in the 79 serializedMBeanInfos field. */ 80 private static void writeSerializedForms() throws Exception { 81 OutputStream fos = new FileOutputStream("SerializedMBeanInfo.java"); 82 PrintWriter w = new PrintWriter(fos); 83 w.println("// Generated by NullInfoArraysTest - do not edit"); 84 w.println(); 85 w.println("public class SerializedMBeanInfo {"); 86 w.println(" public static final byte[][] serializedMBeanInfos = {"); 87 writeSerial(w, new MBeanInfo(null, null, null, null, null, null)); 88 writeSerial(w, new ModelMBeanInfoSupport(null, null, null, null, null, 89 null, null)); 90 writeSerial(w, new OpenMBeanInfoSupport(null, null, null, null, null, 91 null)); 92 w.println(" };"); 93 w.println("}"); 94 w.close(); 95 fos.close(); 96 System.out.println("Wrote SerializedMBeanInfo.java"); 97 } 98 99 private static void writeSerial(PrintWriter w, Object o) throws Exception { 100 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 101 ObjectOutputStream oos = new ObjectOutputStream(bos); 102 oos.writeObject(o); 103 oos.close(); 104 byte[] bytes = bos.toByteArray(); 105 w.print(" {"); 106 for (int i = 0; i < bytes.length; i++) { 107 w.print(bytes[i]); 108 w.print(", "); 109 } 110 w.println("},"); 111 } 112 }