Path: blob/master/test/jdk/java/util/Map/BasicSerialization.java
41149 views
/*1* Copyright (c) 2012, 2013, 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 801120026* @run testng BasicSerialization27* @summary Ensure Maps can be serialized and deserialized.28* @author Mike Duigou29*/30import java.io.ByteArrayOutputStream;31import java.io.InputStream;32import java.io.IOException;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.io.ByteArrayInputStream;36import java.lang.reflect.Constructor;37import java.lang.reflect.Method;38import java.util.*;39import java.util.concurrent.ConcurrentHashMap;40import java.util.concurrent.ConcurrentSkipListMap;4142import org.testng.annotations.Test;43import org.testng.annotations.DataProvider;44import static org.testng.Assert.fail;45import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertTrue;47import static org.testng.Assert.assertFalse;48import static org.testng.Assert.assertSame;4950public class BasicSerialization {5152enum IntegerEnum {5354e0, e1, e2, e3, e4, e5, e6, e7, e8, e9,55e10, e11, e12, e13, e14, e15, e16, e17, e18, e19,56e20, e21, e22, e23, e24, e25, e26, e27, e28, e29,57e30, e31, e32, e33, e34, e35, e36, e37, e38, e39,58e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,59e50, e51, e52, e53, e54, e55, e56, e57, e58, e59,60e60, e61, e62, e63, e64, e65, e66, e67, e68, e69,61e70, e71, e72, e73, e74, e75, e76, e77, e78, e79,62e80, e81, e82, e83, e84, e85, e86, e87, e88, e89,63e90, e91, e92, e93, e94, e95, e96, e97, e98, e99,64EXTRA_KEY;65public static final int SIZE = values().length;66};67private static final int TEST_SIZE = IntegerEnum.SIZE - 1;68/**69* Realized keys ensure that there is always a hard ref to all test objects.70*/71private static final IntegerEnum[] KEYS = new IntegerEnum[TEST_SIZE];72/**73* Realized values ensure that there is always a hard ref to all test74* objects.75*/76private static final String[] VALUES = new String[TEST_SIZE];7778static {79IntegerEnum[] keys = IntegerEnum.values();80for (int each = 0; each < TEST_SIZE; each++) {81KEYS[each] = keys[each];82VALUES[each] = keys[each].name();83}84}85private static final IntegerEnum EXTRA_KEY = IntegerEnum.EXTRA_KEY;86private static final String EXTRA_VALUE = IntegerEnum.EXTRA_KEY.name();8788public static <K, V> Map<K, V> mapClone(Map<K, V> map) {89Method cloneMethod;9091try {92cloneMethod = map.getClass().getMethod("clone", new Class[]{});93} catch (NoSuchMethodException | SecurityException all) {94cloneMethod = null;95}9697if (null != cloneMethod) {98try {99Map<K, V> result = (Map<K, V>)cloneMethod.invoke(map, new Object[]{});100return result;101} catch (Exception all) {102fail("clone() failed " + map.getClass().getSimpleName(), all);103return null;104}105} else {106Constructor<? extends Map> copyConstructor;107try {108copyConstructor = (Constructor<? extends Map>)map.getClass().getConstructor(new Class[]{Map.class});109110Map<K, V> result = (Map<K, V>)copyConstructor.newInstance(new Object[]{map});111112return result;113} catch (Exception all) {114return serialClone(map);115}116}117}118119@Test(dataProvider = "Map<IntegerEnum,String>")120public void testSerialization(String description, Map<IntegerEnum, String> map) {121Object foo = new Object();122123Map<IntegerEnum, String> clone = mapClone(map);124Map<IntegerEnum, String> serialClone = serialClone(map);125126assertEquals(map, map, description + ":should equal self");127assertEquals(clone, map, description + ":should equal clone");128assertEquals(map, clone, description + ": should equal orginal map");129assertEquals(serialClone, map, description + ": should equal deserialized clone");130assertEquals(map, serialClone, description + ": should equal original map");131assertEquals(serialClone, clone, description + ": deserialized clone should equal clone");132assertEquals(clone, serialClone, description + ": clone should equal deserialized clone");133134assertFalse(map.containsKey(EXTRA_KEY), description + ":unexpected key");135assertFalse(clone.containsKey(EXTRA_KEY), description + ":unexpected key");136assertFalse(serialClone.containsKey(EXTRA_KEY), description + ":unexpected key");137map.put(EXTRA_KEY, EXTRA_VALUE);138clone.put(EXTRA_KEY, EXTRA_VALUE);139serialClone.put(EXTRA_KEY, EXTRA_VALUE);140assertTrue(map.containsKey(EXTRA_KEY), description + ":missing key");141assertTrue(clone.containsKey(EXTRA_KEY), description + ":missing key");142assertTrue(serialClone.containsKey(EXTRA_KEY), description + ":missing key");143assertSame(map.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");144assertSame(clone.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");145assertSame(serialClone.get(EXTRA_KEY), EXTRA_VALUE, description + ":wrong value");146147assertEquals(map, map, description + ":should equal self");148assertEquals(clone, map, description + ":should equal clone");149assertEquals(map, clone, description + ": should equal orginal map");150assertEquals(serialClone, map, description + ": should equal deserialized clone");151assertEquals(map, serialClone, description + ": should equal original map");152assertEquals(serialClone, clone, description + ": deserialized clone should equal clone");153assertEquals(clone, serialClone, description + ": clone should equal deserialized clone");154}155156static byte[] serializedForm(Object obj) {157try {158ByteArrayOutputStream baos = new ByteArrayOutputStream();159new ObjectOutputStream(baos).writeObject(obj);160return baos.toByteArray();161} catch (IOException e) {162fail("Unexpected Exception", e);163return null;164}165}166167static Object readObject(byte[] bytes) throws IOException, ClassNotFoundException {168InputStream is = new ByteArrayInputStream(bytes);169return new ObjectInputStream(is).readObject();170}171172@SuppressWarnings("unchecked")173static <T> T serialClone(T obj) {174try {175return (T)readObject(serializedForm(obj));176} catch (IOException | ClassNotFoundException e) {177fail("Unexpected Exception", e);178return null;179}180}181182@DataProvider(name = "Map<IntegerEnum,String>", parallel = true)183private static Iterator<Object[]> makeMaps() {184return Arrays.asList(185// empty186new Object[]{"HashMap", new HashMap()},187new Object[]{"LinkedHashMap", new LinkedHashMap()},188new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(new HashMap(), IntegerEnum.class, String.class)},189new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(new HashMap())},190// null hostile191new Object[]{"EnumMap", new EnumMap(IntegerEnum.class)},192new Object[]{"Hashtable", new Hashtable()},193new Object[]{"TreeMap", new TreeMap()},194new Object[]{"ConcurrentHashMap", new ConcurrentHashMap()},195new Object[]{"ConcurrentSkipListMap", new ConcurrentSkipListMap()},196new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(new ConcurrentHashMap(), IntegerEnum.class, String.class)},197new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(new EnumMap(IntegerEnum.class))},198// filled199new Object[]{"HashMap", fillMap(new HashMap())},200new Object[]{"LinkedHashMap", fillMap(new LinkedHashMap())},201new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(fillMap(new HashMap()), IntegerEnum.class, String.class)},202new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(fillMap(new HashMap()))},203// null hostile204new Object[]{"EnumMap", fillMap(new EnumMap(IntegerEnum.class))},205new Object[]{"Hashtable", fillMap(new Hashtable())},206new Object[]{"TreeMap", fillMap(new TreeMap())},207new Object[]{"ConcurrentHashMap", fillMap(new ConcurrentHashMap())},208new Object[]{"ConcurrentSkipListMap", fillMap(new ConcurrentSkipListMap())},209new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(fillMap(new ConcurrentHashMap()), IntegerEnum.class, String.class)},210new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(fillMap(new EnumMap(IntegerEnum.class)))}).iterator();211}212213private static Map<IntegerEnum, String> fillMap(Map<IntegerEnum, String> result) {214for (int each = 0; each < TEST_SIZE; each++) {215result.put(KEYS[each], VALUES[each]);216}217218return result;219}220}221222223