Skip to content

Commit

Permalink
Add an improved test for #735: turns out there is still at least one,…
Browse files Browse the repository at this point in the history
… ordering-dependant issue
  • Loading branch information
cowtowncoder committed Mar 27, 2015
1 parent 503018b commit de5642e
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.fasterxml.jackson.databind.deser;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

@SuppressWarnings("serial")
public class TestCachingOfDeser extends BaseMapTest
{
// For [databind#735]
public static class TestMapNoCustom {

public Map<String, Integer> map;
}

public static class TestMapWithCustom {

@JsonDeserialize(contentUsing = CustomDeserializer735.class)
public Map<String, Integer> map;
}

public static class TestListWithCustom {
@JsonDeserialize(contentUsing = CustomDeserializer735.class)
public List<Integer> list;
}

public static class TestListNoCustom {
public List<Integer> list;
}

public static class CustomDeserializer735 extends StdDeserializer<Integer> {
public CustomDeserializer735() {
super(Integer.class);
}

@Override
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return 100 * p.getValueAsInt();
}
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

final static String MAP_INPUT = "{\"map\":{\"a\":1}}";
final static String LIST_INPUT = "{\"list\":[1]}";


// Ok: first, use custom-annotated instance first, then standard
public void testCustomMapCaching1() throws Exception
{

ObjectMapper mapper = new ObjectMapper();
TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class);
TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class);

assertNotNull(mapC.map);
assertNotNull(mapStd.map);
assertEquals(Integer.valueOf(100), mapC.map.get("a"));
assertEquals(Integer.valueOf(1), mapStd.map.get("a"));
}

// And then standard first, custom next
public void testCustomMapCaching2() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class);
TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class);

assertNotNull(mapStd.map);
assertNotNull(mapC.map);
assertEquals(Integer.valueOf(1), mapStd.map.get("a"));
assertEquals(Integer.valueOf(100), mapC.map.get("a"));
}

// Ok: first, use custom-annotated instance first, then standard
public void testCustomListCaching1() throws Exception {
ObjectMapper mapper = new ObjectMapper();
TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class);
TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class);

assertNotNull(listC.list);
assertNotNull(listStd.list);
assertEquals(Integer.valueOf(100), listC.list.get(0));
assertEquals(Integer.valueOf(1), listStd.list.get(0));
}

// First custom-annotated, then standard
public void testCustomListCaching2() throws Exception {
ObjectMapper mapper = new ObjectMapper();
TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class);
TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class);

assertNotNull(listC.list);
assertNotNull(listStd.list);
assertEquals(Integer.valueOf(100), listC.list.get(0));
assertEquals(Integer.valueOf(1), listStd.list.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,34 +231,6 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanPro
}
}

// For [databind#735]
public static class TestMapBean735 {

@JsonDeserialize(contentUsing = CustomDeserializer735.class)
public Map<String, Integer> map1;

public Map<String, Integer> map2;
}

public static class TestListBean735 {

@JsonDeserialize(contentUsing = CustomDeserializer735.class)
public List<Integer> list1;

public List<Integer> list2;
}

public static class CustomDeserializer735 extends StdDeserializer<Integer> {
public CustomDeserializer735() {
super(Integer.class);
}

@Override
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return 100 * p.getValueAsInt();
}
}

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -360,21 +332,4 @@ public void testContextReadValue() throws Exception
assertNotNull(w.value.inner);
assertEquals(-13, w.value.inner.x);
}

// [databind#735]: erroneous application of custom deserializer
public void testCustomMapValueDeser735() throws Exception {
String json = "{\"map1\":{\"a\":1},\"map2\":{\"a\":1}}";
TestMapBean735 bean = MAPPER.readValue(json, TestMapBean735.class);

assertEquals(100, bean.map1.get("a").intValue());
assertEquals(1, bean.map2.get("a").intValue());
}

public void testCustomListValueDeser735() throws Exception {
String json = "{\"list1\":[1],\"list2\":[1]}";
TestListBean735 bean = MAPPER.readValue(json, TestListBean735.class);

assertEquals(100, bean.list1.get(0).intValue());
assertEquals(1, bean.list2.get(0).intValue());
}
}

0 comments on commit de5642e

Please sign in to comment.