Skip to content

Commit 2d8ad7d

Browse files
committed
Add a test (albeit commented out, until I figure out how I really feel about it) for #1868
1 parent 6a1152c commit 2d8ad7d

File tree

5 files changed

+94
-37
lines changed

5 files changed

+94
-37
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
public class ClassNameIdResolver
1717
extends TypeIdResolverBase
1818
{
19+
private final static String JAVA_UTIL_PKG = "java.util.";
20+
1921
public ClassNameIdResolver(JavaType baseType, TypeFactory typeFactory) {
2022
super(baseType, typeFactory);
2123
}
@@ -70,7 +72,7 @@ protected String _idFrom(Object value, Class<?> cls, TypeFactory typeFactory)
7072
}
7173
}
7274
String str = cls.getName();
73-
if (str.startsWith("java.util.")) {
75+
if (str.startsWith(JAVA_UTIL_PKG)) {
7476
// 25-Jan-2009, tatu: There are some internal classes that we cannot access as is.
7577
// We need better mechanism; for now this has to do...
7678

@@ -90,12 +92,12 @@ protected String _idFrom(Object value, Class<?> cls, TypeFactory typeFactory)
9092
// 17-Feb-2010, tatus: Another such case: result of Arrays.asList() is
9193
// named like so in Sun JDK... Let's just plain old ArrayList in its place.
9294
// ... also, other similar cases exist...
93-
String suffix = str.substring(10);
95+
String suffix = str.substring(JAVA_UTIL_PKG.length());
9496
if (isJavaUtilCollectionClass(suffix, "List")) {
9597
str = ArrayList.class.getName();
96-
} else if(isJavaUtilCollectionClass(suffix, "Map")){
98+
} else if (isJavaUtilCollectionClass(suffix, "Map")){
9799
str = HashMap.class.getName();
98-
} else if(isJavaUtilCollectionClass(suffix, "Set")){
100+
} else if (isJavaUtilCollectionClass(suffix, "Set")){
99101
str = HashSet.class.getName();
100102
}
101103
}
@@ -109,10 +111,8 @@ protected String _idFrom(Object value, Class<?> cls, TypeFactory typeFactory)
109111
*/
110112
Class<?> outer = ClassUtil.getOuterClass(cls);
111113
if (outer != null) {
112-
/* one more check: let's actually not worry if the declared
113-
* static type is non-static as well; if so, deserializer does
114-
* have a chance at figuring it all out.
115-
*/
114+
// one more check: let's actually not worry if the declared static type is
115+
// non-static as well; if so, deserializer does have a chance at figuring it all out.
116116
Class<?> staticType = _baseType.getRawClass();
117117
if (ClassUtil.getOuterClass(staticType) == null) {
118118
// Is this always correct? Seems like it should be...
@@ -129,8 +129,16 @@ public String getDescForKnownTypeIds() {
129129
return "class name used as type id";
130130
}
131131

132-
private static boolean isJavaUtilCollectionClass(String clz, String type){
133-
return (clz.startsWith("Collections$") || clz.startsWith("Arrays$"))
134-
&& clz.indexOf(type) > 0;
132+
private static boolean isJavaUtilCollectionClass(String clz, String type)
133+
{
134+
if (clz.startsWith("Collections$")) {
135+
// 02-Jan-2017, tatu: As per [databind#1868], need to leave Unmodifiable variants as is
136+
return ((clz.indexOf(type) > 0)
137+
&& !clz.contains("Unmodifiable"));
138+
}
139+
if (clz.startsWith("Arrays$")) {
140+
return (clz.indexOf(type) > 0);
141+
}
142+
return false;
135143
}
136144
}

src/test/java/com/fasterxml/jackson/databind/deser/TestCollectionDeserialization.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/jdk/CollectionDeserTest.java

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.databind.deser;
1+
package com.fasterxml.jackson.databind.deser.jdk;
22

33
import java.io.IOException;
44
import java.util.*;
@@ -11,7 +11,7 @@
1111
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1212

1313
@SuppressWarnings("serial")
14-
public class TestCollectionDeserialization
14+
public class CollectionDeserTest
1515
extends BaseMapTest
1616
{
1717
enum Key {
@@ -42,7 +42,7 @@ public XBean() { }
4242
public XBean(int x) { this.x = x; }
4343
}
4444

45-
// [Issue#199]
45+
// [databind#199]
4646
static class ListAsIterable {
4747
public Iterable<String> values;
4848
}
@@ -284,22 +284,4 @@ public void testWrapExceptions() throws Exception
284284
assertEquals("I want to catch this exception", exc.getMessage());
285285
}
286286
}
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-
}
305287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fasterxml.jackson.databind.deser.jdk;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
7+
import com.fasterxml.jackson.core.type.TypeReference;
8+
9+
import com.fasterxml.jackson.databind.*;
10+
11+
/**
12+
* Tests for special collection/map types via `java.util.Collections`
13+
*/
14+
public class JDKCollectionsDeserTest extends BaseMapTest
15+
{
16+
static class XBean {
17+
public int x;
18+
19+
public XBean() { }
20+
public XBean(int x) { this.x = x; }
21+
}
22+
23+
/*
24+
/**********************************************************************
25+
/* Test methods
26+
/**********************************************************************
27+
*/
28+
29+
private final static ObjectMapper MAPPER = new ObjectMapper();
30+
31+
// And then a round-trip test for singleton collections
32+
public void testSingletonCollections() throws Exception
33+
{
34+
final TypeReference<?> xbeanListType = new TypeReference<List<XBean>>() { };
35+
36+
String json = MAPPER.writeValueAsString(Collections.singleton(new XBean(3)));
37+
Collection<XBean> result = MAPPER.readValue(json, xbeanListType);
38+
assertNotNull(result);
39+
assertEquals(1, result.size());
40+
assertEquals(3, result.iterator().next().x);
41+
42+
json = MAPPER.writeValueAsString(Collections.singletonList(new XBean(28)));
43+
result = MAPPER.readValue(json, xbeanListType);
44+
assertNotNull(result);
45+
assertEquals(1, result.size());
46+
assertEquals(28, result.iterator().next().x);
47+
}
48+
49+
// [databind#1868]: Not sure if this should or should not pass...
50+
/*
51+
public void testUnmodifiableSet() throws Exception
52+
{
53+
ObjectMapper mapper = new ObjectMapper();
54+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
55+
56+
Set<String> theSet = Collections.unmodifiableSet(Collections.singleton("a"));
57+
String json = mapper.writeValueAsString(theSet);
58+
59+
assertEquals("[\"java.util.Collections$UnmodifiableSet\",[\"a\"]]", json);
60+
61+
// and back...
62+
Set<String> result = mapper.readValue(json, Set.class);
63+
assertNotNull(result);
64+
assertEquals(1, result.size());
65+
}
66+
*/
67+
}

src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void testIntBooleanMap() throws Exception
223223
{
224224
// to get typing, must use type reference
225225
String JSON = "{ \"1\" : true, \"-1\" : false }";
226-
Map<Integer,Object> result = MAPPER.readValue
226+
Map<?,Object> result = MAPPER.readValue
227227
(JSON, new TypeReference<HashMap<Integer,Object>>() { });
228228

229229
assertNotNull(result);

src/test/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolverTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void setup(){
3636
}
3737

3838
@Test
39-
public void testIdFromValue_shouldUseJavaUtilHashMapForSingletonMap(){
39+
public void testShouldUseJavaUtilHashMapForSingletonMap(){
4040
Map<String, String> singletonMap = Collections.singletonMap("ANY_KEY", "ANY_VALUE");
4141

4242
String clazz = classNameIdResolver.idFromValue( singletonMap );
@@ -45,7 +45,7 @@ public void testIdFromValue_shouldUseJavaUtilHashMapForSingletonMap(){
4545
}
4646

4747
@Test
48-
public void testIdFromValue_shouldUseJavaUtilHashSetForSingletonSet(){
48+
public void testShouldUseJavaUtilHashSetForSingletonSet(){
4949
Set<String> singletonSet = Collections.singleton("ANY_VALUE");
5050

5151
String clazz = classNameIdResolver.idFromValue( singletonSet );
@@ -54,7 +54,7 @@ public void testIdFromValue_shouldUseJavaUtilHashSetForSingletonSet(){
5454
}
5555

5656
@Test
57-
public void testIdFromValue_shouldUseJavaUtilArrayListForSingletonList(){
57+
public void testShouldUseJavaUtilArrayListForSingletonList(){
5858
List<String> singletonList = Collections.singletonList("ANY_VALUE");
5959

6060
String clazz = classNameIdResolver.idFromValue( singletonList );
@@ -63,7 +63,7 @@ public void testIdFromValue_shouldUseJavaUtilArrayListForSingletonList(){
6363
}
6464

6565
@Test
66-
public void testIdFromValue_shouldUseJavaUtilArrayListForArrays$List(){
66+
public void testShouldUseJavaUtilArrayListForArraysAsList(){
6767
List<String> utilList = Arrays.asList("ANY_VALUE");
6868

6969
String clazz = classNameIdResolver.idFromValue( utilList );

0 commit comments

Comments
 (0)