Skip to content

Commit 8574ed4

Browse files
committed
Minor test coverage improvement; ensure round-trippability of JDK singleton collections/maps
1 parent 7e6cf92 commit 8574ed4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/TestCollectionDeserialization.java

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public CustomList deserialize(JsonParser jp, DeserializationContext ctxt)
3737

3838
static class XBean {
3939
public int x;
40+
41+
public XBean() { }
42+
public XBean(int x) { this.x = x; }
4043
}
4144

4245
// [Issue#199]
@@ -281,4 +284,22 @@ public void testWrapExceptions() throws Exception
281284
assertEquals("I want to catch this exception", exc.getMessage());
282285
}
283286
}
287+
288+
// And then a round-trip test for singleton collections
289+
public void testSingletonCollections() throws Exception
290+
{
291+
final TypeReference<?> xbeanListType = new TypeReference<List<XBean>>() { };
292+
293+
String json = MAPPER.writeValueAsString(Collections.singleton(new XBean(3)));
294+
Collection<XBean> result = MAPPER.readValue(json, xbeanListType);
295+
assertNotNull(result);
296+
assertEquals(1, result.size());
297+
assertEquals(3, result.iterator().next().x);
298+
299+
json = MAPPER.writeValueAsString(Collections.singletonList(new XBean(28)));
300+
result = MAPPER.readValue(json, xbeanListType);
301+
assertNotNull(result);
302+
assertEquals(1, result.size());
303+
assertEquals(28, result.iterator().next().x);
304+
}
284305
}

src/test/java/com/fasterxml/jackson/databind/deser/TestMapDeserialization.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.core.type.TypeReference;
1212
import com.fasterxml.jackson.databind.*;
1313
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
14+
import com.fasterxml.jackson.databind.deser.TestCollectionDeserialization.XBean;
1415
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1516

1617
@SuppressWarnings("serial")
@@ -576,7 +577,21 @@ public void testReadProperties() throws Exception
576577
assertEquals("123", props.getProperty("b"));
577578
assertEquals("true", props.getProperty("c"));
578579
}
579-
580+
581+
// JDK singletonMap
582+
public void testSingletonMapRoundtrip() throws Exception
583+
{
584+
final TypeReference<?> type = new TypeReference<Map<String,IntWrapper>>() { };
585+
586+
String json = MAPPER.writeValueAsString(Collections.singletonMap("value", new IntWrapper(5)));
587+
Map<String,IntWrapper> result = MAPPER.readValue(json, type);
588+
assertNotNull(result);
589+
assertEquals(1, result.size());
590+
IntWrapper w = result.get("value");
591+
assertNotNull(w);
592+
assertEquals(5, w.i);
593+
}
594+
580595
/*
581596
/**********************************************************
582597
/* Error tests

0 commit comments

Comments
 (0)