-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d96e83
commit f8c1363
Showing
2 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializers4225NullCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test to check that getNullValue for deserializer is not cached, by default. | ||
*/ | ||
public class CustomDeserializers4225NullCacheTest extends DatabindTestUtil | ||
{ | ||
static class CustomListDeserializer extends JsonDeserializer<List<String>> { | ||
|
||
private static int getNullValueInvocationCount = 0; | ||
|
||
@Override | ||
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { | ||
return makeList("regular"); | ||
} | ||
|
||
@Override | ||
public List<String> getNullValue(DeserializationContext ctxt) throws JsonMappingException { | ||
// Increment invocation count | ||
getNullValueInvocationCount++; | ||
return makeList("nullVal_" + getNullValueInvocationCount); | ||
} | ||
|
||
public List<String> makeList(String content) { | ||
List<String> randomList = new ArrayList<>(); | ||
randomList.add(content); | ||
return randomList; | ||
} | ||
} | ||
|
||
static class Bean4225 { | ||
@JsonDeserialize(using = CustomListDeserializer.class) | ||
public List<String> myList; | ||
} | ||
|
||
@Test | ||
public void testGetNullValueIsCached() throws Exception | ||
{ | ||
ObjectMapper mapper = newJsonMapper(); | ||
|
||
// First time deserializing null | ||
verifyGetNullValueInvokedTimes(mapper, 1); | ||
// Second time deserializing null, should be invoked twice | ||
verifyGetNullValueInvokedTimes(mapper, 2); | ||
} | ||
|
||
private void verifyGetNullValueInvokedTimes(ObjectMapper mapper, int times) | ||
throws Exception | ||
{ | ||
Bean4225 someBean = mapper.readValue(a2q("{'myList': null}"), Bean4225.class); | ||
|
||
assertThat(someBean.myList).hasSize(1); | ||
assertThat(someBean.myList.get(0)).isEqualTo("nullVal_" + times); | ||
assertThat(CustomListDeserializer.getNullValueInvocationCount).isEqualTo(times); | ||
} | ||
} |