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