MapRelatedTypesDeserTest.java
package com.fasterxml.jackson.databind.deser.jdk;
import java.util.*;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import static org.junit.jupiter.api.Assertions.*;
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;
public class MapRelatedTypesDeserTest
{
private final ObjectMapper MAPPER = newJsonMapper();
/*
/**********************************************************
/* Test methods, Map.Entry
/**********************************************************
*/
@Test
public void testMapEntrySimpleTypes() throws Exception
{
List<Map.Entry<String,Long>> stuff = MAPPER.readValue(a2q("[{'a':15},{'b':42}]"),
new TypeReference<List<Map.Entry<String,Long>>>() { });
assertNotNull(stuff);
assertEquals(2, stuff.size());
assertNotNull(stuff.get(1));
assertEquals("b", stuff.get(1).getKey());
assertEquals(Long.valueOf(42), stuff.get(1).getValue());
}
@Test
public void testMapEntryWithStringBean() throws Exception
{
List<Map.Entry<Integer,StringWrapper>> stuff = MAPPER.readValue(a2q("[{'28':'Foo'},{'13':'Bar'}]"),
new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
assertNotNull(stuff);
assertEquals(2, stuff.size());
assertNotNull(stuff.get(1));
assertEquals(Integer.valueOf(13), stuff.get(1).getKey());
StringWrapper sw = stuff.get(1).getValue();
assertEquals("Bar", sw.str);
}
@Test
public void testMapEntryFail() throws Exception
{
try {
/*List<Map.Entry<Integer,StringWrapper>> stuff =*/ MAPPER.readValue(a2q("[{'28':'Foo','13':'Bar'}]"),
new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
fail("Should not have passed");
} catch (Exception e) {
verifyException(e, "more than one entry in JSON");
}
}
/*
/**********************************************************
/* Test methods, other exotic Map types
/**********************************************************
*/
// [databind#810]
@Test
public void testReadProperties() throws Exception
{
Properties props = MAPPER.readValue(a2q("{'a':'foo', 'b':123, 'c':true}"),
Properties.class);
assertEquals(3, props.size());
assertEquals("foo", props.getProperty("a"));
assertEquals("123", props.getProperty("b"));
assertEquals("true", props.getProperty("c"));
}
// JDK singletonMap
@Test
public void testSingletonMapRoundtrip() throws Exception
{
final TypeReference<Map<String,IntWrapper>> type = new TypeReference<Map<String,IntWrapper>>() { };
String json = MAPPER.writeValueAsString(Collections.singletonMap("value", new IntWrapper(5)));
Map<String,IntWrapper> result = MAPPER.readValue(json, type);
assertNotNull(result);
assertEquals(1, result.size());
IntWrapper w = result.get("value");
assertNotNull(w);
assertEquals(5, w.i);
}
}