1 /*
   2  * Copyright (c) 2005, 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 6204469
  27  * @summary Test ImmutableDescriptor serialization.
  28  * @author Eamonn McManus
  29  * @run clean ImmutableDescriptorSerialTest
  30  * @run build ImmutableDescriptorSerialTest
  31  * @run main ImmutableDescriptorSerialTest
  32  */
  33 
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.ObjectInputStream;
  37 import java.io.ObjectOutputStream;
  38 import java.util.Arrays;
  39 import java.util.HashSet;
  40 import java.util.Set;
  41 import javax.management.Descriptor;
  42 import javax.management.ImmutableDescriptor;
  43 
  44 public class ImmutableDescriptorSerialTest {
  45     public static void main(String[] args) throws Exception {
  46         System.out.println("Test that ImmutableDescriptor.EMPTY_DESCRIPTOR " +
  47                 "deserializes identically");
  48         if (serialize(ImmutableDescriptor.EMPTY_DESCRIPTOR) !=
  49                 ImmutableDescriptor.EMPTY_DESCRIPTOR) {
  50             throw new Exception("ImmutableDescriptor.EMPTY_DESCRIPTOR did not " +
  51                     "deserialize identically");
  52         }
  53         System.out.println("...OK");
  54 
  55         System.out.println("Test that serialization preserves case and " +
  56                 "that deserialized object is case-insensitive");
  57         Descriptor d = new ImmutableDescriptor("a=aval", "B=Bval", "cC=cCval");
  58         Descriptor d1 = serialize(d);
  59         Set<String> keys = new HashSet(Arrays.asList(d1.getFieldNames()));
  60         if (keys.size() != 3 ||
  61                 !keys.containsAll(Arrays.asList("a", "B", "cC"))) {
  62             throw new Exception("Keys don't match: " + keys);
  63         }
  64         for (String key : keys) {
  65             String value = (String) d.getFieldValue(key);
  66             for (String t :
  67                     Arrays.asList(key, key.toLowerCase(), key.toUpperCase())) {
  68                 String tvalue = (String) d1.getFieldValue(t);
  69                 if (!tvalue.equals(value)) {
  70                     throw new Exception("Value of " + key + " for " +
  71                             "deserialized object does not match: " +
  72                             tvalue + " should be " + value);
  73                 }
  74             }
  75         }
  76         System.out.println("...OK");
  77     }
  78 
  79     private static <T> T serialize(T x) throws Exception {
  80         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  81         ObjectOutputStream oout = new ObjectOutputStream(bout);
  82         oout.writeObject(x);
  83         oout.close();
  84         byte[] bytes = bout.toByteArray();
  85         ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
  86         ObjectInputStream oin = new ObjectInputStream(bin);
  87         return (T) oin.readObject();
  88     }
  89 }