Skip to content

Commit

Permalink
Improve test robustness to help with #3406 (#4903)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 14, 2025
1 parent 49f4dae commit b56139e
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ public void testIncrementalPojoReading() throws IOException
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(Integer.valueOf(1), p.readValueAs(Integer.class));
assertEquals(Boolean.TRUE, p.readValueAs(Boolean.class));
/* note: null can be returned both when there is no more
* data in current scope, AND when Json null literal is
* bound!
*/

// note: null can be returned both when there is no more
// data in current scope, AND when Json null literal is bound!
assertNull(p.readValueAs(Object.class));
// but we can verify that it was Json null by:
assertEquals(JsonToken.VALUE_NULL, p.getLastClearedToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;


/**
* Tests to verify that most core Jackson components can be serialized
* using default JDK serialization: this feature is useful for some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public DataA deserialize(JsonParser p, DeserializationContext ctxt) throws IOExc
}
/*JsonNode node =*/ p.readValueAsTree();

p.skipChildren(); // important, must consume input
DataA da = new DataA();
da.i = 5;
return da;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public BogusBeanDeserializer(String a, String b) {
}

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
public Object deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
p.skipChildren();
return new Bean(a, b);
}
}
Expand Down Expand Up @@ -242,8 +243,10 @@ static class ArrayDeserializerModifier extends BeanDeserializerModifier {
public JsonDeserializer<?> modifyArrayDeserializer(DeserializationConfig config, ArrayType valueType,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return (JsonDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
DeserializationContext ctxt) {
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) throws IOException
{
p.skipChildren();
return new String[] { "foo" };
}
};
Expand All @@ -255,8 +258,10 @@ static class CollectionDeserializerModifier extends BeanDeserializerModifier {
public JsonDeserializer<?> modifyCollectionDeserializer(DeserializationConfig config, CollectionType valueType,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return (JsonDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
DeserializationContext ctxt) {
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) throws IOException
{
p.skipChildren();
ArrayList<String> list = new ArrayList<String>();
list.add("foo");
return list;
Expand All @@ -270,8 +275,10 @@ static class MapDeserializerModifier extends BeanDeserializerModifier {
public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType valueType,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return (JsonDeserializer<?>) new StdDeserializer<Object>(Object.class) {
@Override public Object deserialize(JsonParser jp,
DeserializationContext ctxt) {
@Override public Object deserialize(JsonParser p,
DeserializationContext ctxt) throws IOException
{
p.skipChildren();
HashMap<String,String> map = new HashMap<String,String>();
map.put("a", "foo");
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public Object handleUnexpectedToken(DeserializationContext ctxt,
String failureMsg)
throws IOException
{
p.skipChildren();
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.*;

/**
* Unit tests for verifying handling of simple basic non-structured
* types; primitives (and/or their wrappers), Strings.
*/
public class JDKScalarsDeserTest
extends DatabindTestUtil
{
final static String NAN_STRING = "NaN";

Expand Down Expand Up @@ -434,7 +434,7 @@ public void testBase64Variants() throws Exception

/**
* Then a unit test to verify that we can conveniently bind sequence of
* space-separate simple values
* space-separated simple values
*/
@Test
public void testSequenceOfInts() throws Exception
Expand All @@ -446,12 +446,15 @@ public void testSequenceOfInts() throws Exception
sb.append(" ");
sb.append(i);
}
JsonParser jp = MAPPER.createParser(sb.toString());
for (int i = 0; i < NR_OF_INTS; ++i) {
Integer result = MAPPER.readValue(jp, Integer.class);
assertEquals(Integer.valueOf(i), result);
ObjectMapper mapper = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();
try (JsonParser p = mapper.createParser(sb.toString())) {
for (int i = 0; i < NR_OF_INTS; ++i) {
Integer result = mapper.readValue(p, Integer.class);
assertEquals(Integer.valueOf(i), result);
}
}
jp.close();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -260,7 +261,9 @@ private void assertLocale(Locale expected, Locale actual) {
@Test
public void testLocaleFuzz47034() throws Exception
{
Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json"),
Locale loc = MAPPER.reader()
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json"),
Locale.class);
assertNotNull(loc);
}
Expand All @@ -270,7 +273,9 @@ public void testLocaleFuzz47034() throws Exception
@Test
public void testLocaleFuzz47036() throws Exception
{
Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json"),
Locale loc = MAPPER.reader()
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json"),
Locale.class);
assertNotNull(loc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@

import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.verifyException;

/**
* Test for validating {@link com.fasterxml.jackson.databind.DeserializationFeature#FAIL_ON_TRAILING_TOKENS}.
* Test for validating {@link DeserializationFeature#FAIL_ON_TRAILING_TOKENS}.
*/
public class FullStreamReadTest
public class FullStreamReadTest extends DatabindTestUtil
{
private final static String JSON_OK_ARRAY = " [ 1, 2, 3] ";
private final static String JSON_OK_ARRAY_WITH_COMMENT = JSON_OK_ARRAY + " // stuff ";
Expand All @@ -42,35 +37,40 @@ public class FullStreamReadTest
@Test
public void testMapperAcceptTrailing() throws Exception
{
assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));
ObjectMapper mapper = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();

assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));

// by default, should be ok to read, all
_verifyArray(MAPPER.readTree(JSON_OK_ARRAY));
_verifyArray(MAPPER.readTree(JSON_OK_ARRAY_WITH_COMMENT));
_verifyArray(MAPPER.readTree(JSON_FAIL_ARRAY));
_verifyArray(mapper.readTree(JSON_OK_ARRAY));
_verifyArray(mapper.readTree(JSON_OK_ARRAY_WITH_COMMENT));
_verifyArray(mapper.readTree(JSON_FAIL_ARRAY));

// and also via "untyped"
_verifyCollection(MAPPER.readValue(JSON_OK_ARRAY, List.class));
_verifyCollection(MAPPER.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
_verifyCollection(MAPPER.readValue(JSON_FAIL_ARRAY, List.class));
_verifyCollection(mapper.readValue(JSON_OK_ARRAY, List.class));
_verifyCollection(mapper.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
_verifyCollection(mapper.readValue(JSON_FAIL_ARRAY, List.class));

// ditto for getting `null` and some other token

assertTrue(MAPPER.readTree(JSON_OK_NULL).isNull());
assertTrue(MAPPER.readTree(JSON_OK_NULL_WITH_COMMENT).isNull());
assertTrue(MAPPER.readTree(JSON_FAIL_NULL).isNull());
assertTrue(mapper.readTree(JSON_OK_NULL).isNull());
assertTrue(mapper.readTree(JSON_OK_NULL_WITH_COMMENT).isNull());
assertTrue(mapper.readTree(JSON_FAIL_NULL).isNull());

assertNull(MAPPER.readValue(JSON_OK_NULL, Object.class));
assertNull(MAPPER.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class));
assertNull(MAPPER.readValue(JSON_FAIL_NULL, Object.class));
assertNull(mapper.readValue(JSON_OK_NULL, Object.class));
assertNull(mapper.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class));
assertNull(mapper.readValue(JSON_FAIL_NULL, Object.class));
}

@Test
public void testMapperFailOnTrailing() throws Exception
{
// but things change if we enforce checks
ObjectMapper strict = newJsonMapper()
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
ObjectMapper strict = jsonMapperBuilder()
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();
assertTrue(strict.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));

// some still ok
Expand Down Expand Up @@ -177,8 +177,8 @@ public void testMapperFailOnTrailingWithNull() throws Exception
@Test
public void testReaderAcceptTrailing() throws Exception
{
ObjectReader R = MAPPER.reader();
assertFalse(R.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS));
ObjectReader R = MAPPER.reader()
.without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);

_verifyArray(R.readTree(JSON_OK_ARRAY));
_verifyArray(R.readTree(JSON_OK_ARRAY_WITH_COMMENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void serialize(Dog value, JsonGenerator gen, SerializerProvider serialize

static class ClassDogDeserializer extends JsonDeserializer<Dog> {
@Override
public Dog deserialize(JsonParser p, DeserializationContext ctxt) {
public Dog deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
p.skipChildren();
return new Dog("class-dog");
}
}
Expand All @@ -73,7 +74,8 @@ public Object deserializeKey(String key, DeserializationContext ctxt) {

static class ModuleDogDeserializer extends JsonDeserializer<Dog> {
@Override
public Dog deserialize(JsonParser p, DeserializationContext ctxt) {
public Dog deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
p.skipChildren();
return new Dog("module-dog");
}
}
Expand Down Expand Up @@ -143,6 +145,7 @@ public BuildFailBean build() {
static class BuildSuccessBeanDeserializer extends JsonDeserializer<BuildSuccessBean> {
@Override
public BuildSuccessBean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
p.skipChildren();
return new BuildSuccessBean(7, 8);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ static class Test3787Bean {
static class Deserializer3787A extends JsonDeserializer<Test3787Bean> {
@Override
public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
p.skipChildren(); // important to consume value
Test3787Bean simpleTestBean = new Test3787Bean();
simpleTestBean.value = "I am A";
return simpleTestBean;
Expand All @@ -211,6 +212,7 @@ public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) throw
static class Deserializer3787B extends JsonDeserializer<Test3787Bean> {
@Override
public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
p.skipChildren(); // important to consume value
Test3787Bean simpleTestBean = new Test3787Bean();
simpleTestBean.value = "I am B";
return simpleTestBean;
Expand Down Expand Up @@ -492,7 +494,7 @@ public void testAutoDiscovery() throws Exception
}

@Test
public void testAddSerializerTwiceThenOnlyLatestIsKept() throws JsonProcessingException {
public void testAddSerializerTwiceThenOnlyLatestIsKept() throws Exception {
SimpleModule module = new SimpleModule()
.addSerializer(Test3787Bean.class, new Serializer3787A())
.addSerializer(Test3787Bean.class, new Serializer3787B());
Expand All @@ -503,7 +505,7 @@ public void testAddSerializerTwiceThenOnlyLatestIsKept() throws JsonProcessingEx
}

@Test
public void testAddModuleWithSerializerTwiceThenOnlyLatestIsKept() throws JsonProcessingException {
public void testAddModuleWithSerializerTwiceThenOnlyLatestIsKept() throws Exception {
SimpleModule firstModule = new SimpleModule()
.addSerializer(Test3787Bean.class, new Serializer3787A());
SimpleModule secondModule = new SimpleModule()
Expand All @@ -520,7 +522,7 @@ public void testAddModuleWithSerializerTwiceThenOnlyLatestIsKept() throws JsonPr
}

@Test
public void testAddModuleWithSerializerTwiceThenOnlyLatestIsKept_reverseOrder() throws JsonProcessingException {
public void testAddModuleWithSerializerTwiceThenOnlyLatestIsKept_reverseOrder() throws Exception {
SimpleModule firstModule = new SimpleModule()
.addSerializer(Test3787Bean.class, new Serializer3787A());
SimpleModule secondModule = new SimpleModule()
Expand Down

0 comments on commit b56139e

Please sign in to comment.