-
-
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.
Update CustomDeserializers4225NullCacheTest.java Clean up note
- Loading branch information
1 parent
8d96e83
commit ebe3bcf
Showing
2 changed files
with
84 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test to check that getNullValue for deserializer is not cached, by default. | ||
*/ | ||
@SuppressWarnings("serial") | ||
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; | ||
} | ||
} | ||
|
||
// [databind#2467]: Allow missing "content" for as-array deserialization | ||
static class Bean4225 { | ||
@JsonDeserialize(using = CustomListDeserializer.class) | ||
public List<String> myList; | ||
} | ||
|
||
@Test | ||
public void testGetNullValueIsCached() throws Exception | ||
{ | ||
ObjectMapper mapper = objectMapper(); | ||
|
||
// 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); | ||
} | ||
} |