diff options
Diffstat (limited to 'gson/src/test/java/com/google/gson/functional/MapTest.java')
-rw-r--r-- | gson/src/test/java/com/google/gson/functional/MapTest.java | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/gson/src/test/java/com/google/gson/functional/MapTest.java b/gson/src/test/java/com/google/gson/functional/MapTest.java index c5344a76..43725663 100644 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -16,6 +16,12 @@ package com.google.gson.functional; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.InstanceCreator; @@ -41,7 +47,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; -import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; /** * Functional test for Json serialization and deserialization for Maps @@ -49,15 +56,15 @@ import junit.framework.TestCase; * @author Inderjeet Singh * @author Joel Leitch */ -public class MapTest extends TestCase { +public class MapTest { private Gson gson; - @Override - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { gson = new Gson(); } + @Test public void testMapSerialization() { Map<String, Integer> map = new LinkedHashMap<>(); map.put("a", 1); @@ -68,6 +75,7 @@ public class MapTest extends TestCase { assertTrue(json.contains("\"b\":2")); } + @Test public void testMapDeserialization() { String json = "{\"a\":1,\"b\":2}"; Type typeOfMap = new TypeToken<Map<String,Integer>>(){}.getType(); @@ -76,6 +84,7 @@ public class MapTest extends TestCase { assertEquals(2, target.get("b").intValue()); } + @Test public void testObjectMapSerialization() { Map<String, Object> map = new LinkedHashMap<>(); map.put("a", 1); @@ -85,6 +94,7 @@ public class MapTest extends TestCase { assertTrue(json.contains("\"b\":\"string\"")); } + @Test public void testMapSerializationEmpty() { Map<String, Integer> map = new LinkedHashMap<>(); Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType(); @@ -92,12 +102,14 @@ public class MapTest extends TestCase { assertEquals("{}", json); } + @Test public void testMapDeserializationEmpty() { Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType(); Map<String, Integer> map = gson.fromJson("{}", typeOfMap); assertTrue(map.isEmpty()); } + @Test public void testMapSerializationWithNullValue() { Map<String, Integer> map = new LinkedHashMap<>(); map.put("abc", null); @@ -108,6 +120,7 @@ public class MapTest extends TestCase { assertEquals("{}", json); } + @Test public void testMapDeserializationWithNullValue() { Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType(); Map<String, Integer> map = gson.fromJson("{\"abc\":null}", typeOfMap); @@ -115,6 +128,7 @@ public class MapTest extends TestCase { assertNull(map.get("abc")); } + @Test public void testMapSerializationWithNullValueButSerializeNulls() { gson = new GsonBuilder().serializeNulls().create(); Map<String, Integer> map = new LinkedHashMap<>(); @@ -125,6 +139,7 @@ public class MapTest extends TestCase { assertEquals("{\"abc\":null}", json); } + @Test public void testMapSerializationWithNullKey() { Map<String, Integer> map = new LinkedHashMap<>(); map.put(null, 123); @@ -134,6 +149,7 @@ public class MapTest extends TestCase { assertEquals("{\"null\":123}", json); } + @Test public void testMapDeserializationWithNullKey() { Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType(); Map<String, Integer> map = gson.fromJson("{\"null\":123}", typeOfMap); @@ -147,6 +163,7 @@ public class MapTest extends TestCase { assertNull(map.get(null)); } + @Test public void testMapSerializationWithIntegerKeys() { Map<Integer, String> map = new LinkedHashMap<>(); map.put(123, "456"); @@ -156,6 +173,7 @@ public class MapTest extends TestCase { assertEquals("{\"123\":\"456\"}", json); } + @Test public void testMapDeserializationWithIntegerKeys() { Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType(); Map<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -164,6 +182,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(123)); } + @Test public void testMapDeserializationWithUnquotedIntegerKeys() { Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType(); Map<Integer, String> map = gson.fromJson("{123:\"456\"}", typeOfMap); @@ -172,6 +191,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(123)); } + @Test public void testMapDeserializationWithLongKeys() { long longValue = 9876543210L; String json = String.format("{\"%d\":\"456\"}", longValue); @@ -182,6 +202,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(longValue)); } + @Test public void testMapDeserializationWithUnquotedLongKeys() { long longKey = 9876543210L; String json = String.format("{%d:\"456\"}", longKey); @@ -192,6 +213,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(longKey)); } + @Test public void testHashMapDeserialization() throws Exception { Type typeOfMap = new TypeToken<HashMap<Integer, String>>() {}.getType(); HashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -200,6 +222,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(123)); } + @Test public void testSortedMap() throws Exception { Type typeOfMap = new TypeToken<SortedMap<Integer, String>>() {}.getType(); SortedMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -208,6 +231,7 @@ public class MapTest extends TestCase { assertEquals("456", map.get(123)); } + @Test public void testConcurrentMap() throws Exception { Type typeOfMap = new TypeToken<ConcurrentMap<Integer, String>>() {}.getType(); ConcurrentMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -218,6 +242,7 @@ public class MapTest extends TestCase { assertEquals("{\"123\":\"456\"}", json); } + @Test public void testConcurrentHashMap() throws Exception { Type typeOfMap = new TypeToken<ConcurrentHashMap<Integer, String>>() {}.getType(); ConcurrentHashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -228,6 +253,7 @@ public class MapTest extends TestCase { assertEquals("{\"123\":\"456\"}", json); } + @Test public void testConcurrentNavigableMap() throws Exception { Type typeOfMap = new TypeToken<ConcurrentNavigableMap<Integer, String>>() {}.getType(); ConcurrentNavigableMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -238,6 +264,7 @@ public class MapTest extends TestCase { assertEquals("{\"123\":\"456\"}", json); } + @Test public void testConcurrentSkipListMap() throws Exception { Type typeOfMap = new TypeToken<ConcurrentSkipListMap<Integer, String>>() {}.getType(); ConcurrentSkipListMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); @@ -248,6 +275,7 @@ public class MapTest extends TestCase { assertEquals("{\"123\":\"456\"}", json); } + @Test public void testParameterizedMapSubclassSerialization() { MyParameterizedMap<String, String> map = new MyParameterizedMap<>(10); map.put("a", "b"); @@ -264,6 +292,7 @@ public class MapTest extends TestCase { } } + @Test public void testMapSubclassSerialization() { MyMap map = new MyMap(); map.put("a", "b"); @@ -271,6 +300,7 @@ public class MapTest extends TestCase { assertTrue(json.contains("\"a\":\"b\"")); } + @Test public void testMapStandardSubclassDeserialization() { String json = "{a:'1',b:'2'}"; Type type = new TypeToken<LinkedHashMap<String, String>>() {}.getType(); @@ -279,6 +309,7 @@ public class MapTest extends TestCase { assertEquals("2", map.get("b")); } + @Test public void testMapSubclassDeserialization() { Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() { @Override public MyMap createInstance(Type type) { @@ -291,6 +322,7 @@ public class MapTest extends TestCase { assertEquals("2", map.get("b")); } + @Test public void testCustomSerializerForSpecificMapType() { Type type = $Gson$Types.newParameterizedTypeWithOwner( null, Map.class, String.class, Long.class); @@ -324,6 +356,7 @@ public class MapTest extends TestCase { /** * Created in response to http://code.google.com/p/google-gson/issues/detail?id=99 */ + @Test public void testMapSerializationWithNullValues() { ClassWithAMap target = new ClassWithAMap(); target.map.put("name1", null); @@ -336,6 +369,7 @@ public class MapTest extends TestCase { /** * Created in response to http://code.google.com/p/google-gson/issues/detail?id=99 */ + @Test public void testMapSerializationWithNullValuesSerialized() { Gson gson = new GsonBuilder().serializeNulls().create(); ClassWithAMap target = new ClassWithAMap(); @@ -346,6 +380,7 @@ public class MapTest extends TestCase { assertTrue(json.contains("name2")); } + @Test public void testMapSerializationWithWildcardValues() { Map<String, ? extends Collection<? extends Integer>> map = new LinkedHashMap<>(); map.put("test", null); @@ -356,6 +391,7 @@ public class MapTest extends TestCase { assertEquals("{}", json); } + @Test public void testMapDeserializationWithWildcardValues() { Type typeOfMap = new TypeToken<Map<String, ? extends Long>>() {}.getType(); Map<String, ? extends Long> map = gson.fromJson("{\"test\":123}", typeOfMap); @@ -374,6 +410,7 @@ public class MapTest extends TestCase { /** * From bug report http://code.google.com/p/google-gson/issues/detail?id=95 */ + @Test public void testMapOfMapSerialization() { Map<String, Map<String, String>> map = new HashMap<>(); Map<String, String> nestedMap = new HashMap<>(); @@ -389,6 +426,7 @@ public class MapTest extends TestCase { /** * From bug report http://code.google.com/p/google-gson/issues/detail?id=95 */ + @Test public void testMapOfMapDeserialization() { String json = "{nestedMap:{'2':'2','1':'1'}}"; Type type = new TypeToken<Map<String, Map<String, String>>>(){}.getType(); @@ -401,6 +439,7 @@ public class MapTest extends TestCase { /** * From bug report http://code.google.com/p/google-gson/issues/detail?id=178 */ + @Test public void testMapWithQuotes() { Map<String, String> map = new HashMap<>(); map.put("a\"b", "c\"d"); @@ -411,6 +450,7 @@ public class MapTest extends TestCase { /** * From issue 227. */ + @Test public void testWriteMapsWithEmptyStringKey() { Map<String, Boolean> map = new HashMap<>(); map.put("", true); @@ -418,6 +458,7 @@ public class MapTest extends TestCase { } + @Test public void testReadMapsWithEmptyStringKey() { Map<String, Boolean> map = gson.fromJson("{\"\":true}", new TypeToken<Map<String, Boolean>>() {}.getType()); assertEquals(Boolean.TRUE, map.get("")); @@ -426,6 +467,7 @@ public class MapTest extends TestCase { /** * From bug report http://code.google.com/p/google-gson/issues/detail?id=204 */ + @Test public void testSerializeMaps() { Map<String, Object> map = new LinkedHashMap<>(); map.put("a", 12); @@ -454,6 +496,7 @@ public class MapTest extends TestCase { new Gson().toJson(map)); } + @Test public final void testInterfaceTypeMap() { MapClass element = new MapClass(); TestTypes.Sub subType = new TestTypes.Sub(); @@ -475,6 +518,7 @@ public class MapTest extends TestCase { assertEquals(expected, json); } + @Test public final void testInterfaceTypeMapWithSerializer() { MapClass element = new MapClass(); TestTypes.Sub subType = new TestTypes.Sub(); @@ -509,6 +553,7 @@ public class MapTest extends TestCase { assertEquals(expected, json); } + @Test public void testGeneralMapField() throws Exception { MapWithGeneralMapParameters map = new MapWithGeneralMapParameters(); map.map.put("string", "testString"); @@ -525,6 +570,7 @@ public class MapTest extends TestCase { assertEquals(expected, gson.toJson(map)); } + @Test public void testComplexKeysSerialization() { Map<Point, String> map = new LinkedHashMap<>(); map.put(new Point(2, 3), "a"); @@ -534,6 +580,7 @@ public class MapTest extends TestCase { assertEquals(json, gson.toJson(map, Map.class)); } + @Test public void testComplexKeysDeserialization() { String json = "{'2,3':'a','5,7':'b'}"; try { @@ -543,6 +590,7 @@ public class MapTest extends TestCase { } } + @Test public void testStringKeyDeserialization() { String json = "{'2,3':'a','5,7':'b'}"; Map<String, String> map = new LinkedHashMap<>(); @@ -551,6 +599,7 @@ public class MapTest extends TestCase { assertEquals(map, gson.fromJson(json, new TypeToken<Map<String, String>>() {}.getType())); } + @Test public void testNumberKeyDeserialization() { String json = "{'2.3':'a','5.7':'b'}"; Map<Double, String> map = new LinkedHashMap<>(); @@ -559,6 +608,7 @@ public class MapTest extends TestCase { assertEquals(map, gson.fromJson(json, new TypeToken<Map<Double, String>>() {}.getType())); } + @Test public void testBooleanKeyDeserialization() { String json = "{'true':'a','false':'b'}"; Map<Boolean, String> map = new LinkedHashMap<>(); @@ -567,6 +617,7 @@ public class MapTest extends TestCase { assertEquals(map, gson.fromJson(json, new TypeToken<Map<Boolean, String>>() {}.getType())); } + @Test public void testMapDeserializationWithDuplicateKeys() { try { gson.fromJson("{'a':1,'a':2}", new TypeToken<Map<String, Integer>>() {}.getType()); @@ -575,6 +626,7 @@ public class MapTest extends TestCase { } } + @Test public void testSerializeMapOfMaps() { Type type = new TypeToken<Map<String, Map<String, String>>>() {}.getType(); Map<String, Map<String, String>> map = newMap( @@ -584,6 +636,7 @@ public class MapTest extends TestCase { gson.toJson(map, type).replace('"', '\'')); } + @Test public void testDeerializeMapOfMaps() { Type type = new TypeToken<Map<String, Map<String, String>>>() {}.getType(); Map<String, Map<String, String>> map = newMap( @@ -600,6 +653,7 @@ public class MapTest extends TestCase { return result; } + @Test public void testMapNamePromotionWithJsonElementReader() { String json = "{'2.3':'a'}"; Map<Double, String> map = new LinkedHashMap<>(); |