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