1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
package com.esotericsoftware.kryo;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import junit.framework.Assert;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.MapSerializer;
/** @author Nathan Sweet <misc@n4te.com> */
public class MapSerializerTest extends KryoTestCase {
{
supportsCopy = true;
}
public void testMaps () {
kryo.register(HashMap.class);
kryo.register(LinkedHashMap.class);
HashMap map = new HashMap();
map.put("123", "456");
map.put("789", "abc");
roundTrip(18, map);
roundTrip(2, new LinkedHashMap());
roundTrip(18, new LinkedHashMap(map));
MapSerializer serializer = new MapSerializer();
kryo.register(HashMap.class, serializer);
kryo.register(LinkedHashMap.class, serializer);
serializer.setKeyClass(String.class, kryo.getSerializer(String.class));
serializer.setKeysCanBeNull(false);
serializer.setValueClass(String.class, kryo.getSerializer(String.class));
roundTrip(14, map);
serializer.setValuesCanBeNull(false);
roundTrip(14, map);
}
public void testEmptyHashMap () {
execute(new HashMap<Object, Object>(), 0);
}
public void testNotEmptyHashMap () {
execute(new HashMap<Object, Object>(), 1000);
}
public void testEmptyConcurrentHashMap () {
execute(new ConcurrentHashMap<Object, Object>(), 0);
}
public void testNotEmptyConcurrentHashMap () {
execute(new ConcurrentHashMap<Object, Object>(), 1000);
}
public void testGenerics () {
kryo.register(HasGenerics.class);
kryo.register(Integer[].class);
kryo.register(HashMap.class);
HasGenerics test = new HasGenerics();
test.map.put("moo", new Integer[] {1, 2});
output = new Output(4096);
kryo.writeClassAndObject(output, test);
output.flush();
input = new Input(output.toBytes());
HasGenerics test2 = (HasGenerics)kryo.readClassAndObject(input);
assertEquals(test.map.get("moo"), test2.map.get("moo"));
}
private void execute (Map<Object, Object> map, int inserts) {
Random random = new Random();
for (int i = 0; i < inserts; i++)
map.put(random.nextLong(), random.nextBoolean());
Kryo kryo = new Kryo();
kryo.register(HashMap.class, new MapSerializer());
kryo.register(ConcurrentHashMap.class, new MapSerializer());
Output output = new Output(2048, -1);
kryo.writeClassAndObject(output, map);
output.close();
Input input = new Input(output.toBytes());
Object deserialized = kryo.readClassAndObject(input);
input.close();
Assert.assertEquals(map, deserialized);
}
public void testTreeMap () {
kryo.register(TreeMap.class);
TreeMap map = new TreeMap();
map.put("123", "456");
map.put("789", "abc");
roundTrip(19, map);
kryo.register(KeyThatIsntComparable.class);
kryo.register(KeyComparator.class);
map = new TreeMap(new KeyComparator());
KeyThatIsntComparable key = new KeyThatIsntComparable();
key.value = "123";
map.put(key, "456");
roundTrip(11, map);
}
static public class HasGenerics {
public HashMap<String, Integer[]> map = new HashMap();
public HashMap<String, ?> map2 = new HashMap();
}
static public class KeyComparator implements Comparator<KeyThatIsntComparable> {
public int compare (KeyThatIsntComparable o1, KeyThatIsntComparable o2) {
return o1.value.compareTo(o2.value);
}
}
static public class KeyThatIsntComparable {
public String value;
}
}
|