1 /*
   2  * Copyright (c) 2004, 2015, 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  *
  31  * @run clean NullInfoArraysTest
  32  * @run build NullInfoArraysTest
  33  * @run main NullInfoArraysTest
  34  */
  35 
  36 import java.io.*;
  37 import javax.management.*;
  38 import javax.management.modelmbean.*;
  39 import javax.management.openmbean.*;
  40 
  41 public class NullInfoArraysTest {
  42     public static void main(String[] args) throws Exception {
  43         if (args.length > 0 && args[0].equals("write"))
  44             writeSerializedForms();
  45         else
  46             testSerializedForms();
  47     }
  48 
  49     private static void testSerializedForms() throws Exception {
  50         byte[][] serializedMBeanInfos =
  51             SerializedMBeanInfo.serializedMBeanInfos;
  52         for (int i = 0; i < serializedMBeanInfos.length; i++) {
  53             byte[] serializedMBeanInfo = serializedMBeanInfos[i];
  54             ByteArrayInputStream bis =
  55                 new ByteArrayInputStream(serializedMBeanInfo);
  56             ObjectInputStream ois = new ObjectInputStream(bis);
  57             MBeanInfo mbi = (MBeanInfo) ois.readObject();
  58 
  59             System.out.println("Testing a " +
  60                                mbi.getClass().getName() + "...");
  61 
  62             if (mbi.getAttributes() == null ||
  63                 mbi.getOperations() == null ||
  64                 mbi.getConstructors() == null ||
  65                 mbi.getNotifications() == null)
  66                 throw new Exception("At least one getter returned null");
  67 
  68             System.out.println("OK");
  69         }
  70 
  71         System.out.println("Test passed");
  72     }
  73 
  74     /* This method is intended to be invoked when constructing the
  75        test for the first time, with JMX 1.1 RI in the classpath.  It
  76        constructs the SerializedMBeanInfo.java source file.  There is
  77        of course a chicken-and-egg problem for compiling: the first
  78        time we built this test, we supplied a trivial
  79        SerializedMBeanInfo.java with an empty array in the
  80        serializedMBeanInfos field.  */
  81     private static void writeSerializedForms() throws Exception {
  82         OutputStream fos = new FileOutputStream("SerializedMBeanInfo.java");
  83         PrintWriter w = new PrintWriter(fos);
  84         w.println("// Generated by NullInfoArraysTest - do not edit");
  85         w.println();
  86         w.println("public class SerializedMBeanInfo {");
  87         w.println("    public static final byte[][] serializedMBeanInfos = {");
  88         writeSerial(w, new MBeanInfo(null, null, null, null, null, null));
  89         writeSerial(w, new ModelMBeanInfoSupport(null, null, null, null, null,
  90                                                  null, null));
  91         writeSerial(w, new OpenMBeanInfoSupport(null, null, null, null, null,
  92                                                 null));
  93         w.println("    };");
  94         w.println("}");
  95         w.close();
  96         fos.close();
  97         System.out.println("Wrote SerializedMBeanInfo.java");
  98     }
  99 
 100     private static void writeSerial(PrintWriter w, Object o) throws Exception {
 101         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 102         ObjectOutputStream oos = new ObjectOutputStream(bos);
 103         oos.writeObject(o);
 104         oos.close();
 105         byte[] bytes = bos.toByteArray();
 106         w.print("        {");
 107         for (int i = 0; i < bytes.length; i++) {
 108             w.print(bytes[i]);
 109             w.print(", ");
 110         }
 111         w.println("},");
 112     }
 113 }