Skip to content

Commit

Permalink
Continuing on #2828, reduce use of JsonMappingException in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 22, 2021
1 parent 2666a7f commit 03f92a5
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -128,8 +129,8 @@ public void testParserFeaturesComments() throws Exception
try {
reader.without(JsonReadFeature.ALLOW_JAVA_COMMENTS).readValue(JSON);
fail("Should not have passed");
} catch (JsonMappingException e) {
// mapping exception since it gets wrapped
} catch (DatabindException e) {
// DatabindException since it gets wrapped
verifyException(e, "foo");
}
}
Expand Down Expand Up @@ -480,7 +481,7 @@ public void testMissingType() throws Exception
try {
r.readValue("1");
fail("Should not pass");
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "No value type configured");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.IgnoredPropertyException;

/**
* This unit test suite that tests use of {@link JsonIgnore}
Expand Down Expand Up @@ -68,15 +69,15 @@ public void testFailOnIgnore() throws Exception
try {
result = r.readValue(aposToQuotes("{'x':3, 'y':4}"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (IgnoredPropertyException e) {
verifyException(e, "Ignored field");
}

// or 'z'
try {
result = r.readValue(aposToQuotes("{'z':2 }"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (IgnoredPropertyException e) {
verifyException(e, "Ignored field");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

/**
* Unit tests for verifying that field-backed properties can also be
Expand All @@ -12,12 +13,6 @@
public class TestFieldDeserialization
extends BaseMapTest
{
/*
/**********************************************************
/* Annotated helper classes
/**********************************************************
*/

static class SimpleFieldBean
{
public int x, y;
Expand Down Expand Up @@ -59,7 +54,7 @@ public static class DupFieldBean2
public int _z;

@JsonDeserialize
private int foo;
int foo;
}

public static class OkDupFieldBean
Expand Down Expand Up @@ -91,19 +86,19 @@ static class AbstractWrapper {
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

public void testSimpleAutoDetect() throws Exception
{
ObjectMapper m = new ObjectMapper();
SimpleFieldBean result = m.readValue("{ \"x\" : -13 }",
SimpleFieldBean.class);
SimpleFieldBean result = MAPPER.readValue("{ \"x\" : -13 }",
SimpleFieldBean.class);
assertEquals(-13, result.x);
assertEquals(0, result.y);
}

public void testSimpleAnnotation() throws Exception
{
ObjectMapper m = new ObjectMapper();
SimpleFieldBean2 bean = m.readValue("{ \"values\" : [ \"x\", \"y\" ] }",
SimpleFieldBean2 bean = MAPPER.readValue("{ \"values\" : [ \"x\", \"y\" ] }",
SimpleFieldBean2.class);
String[] values = bean.values;
assertNotNull(values);
Expand All @@ -114,46 +109,42 @@ public void testSimpleAnnotation() throws Exception

public void testNoAutoDetect() throws Exception
{
ObjectMapper m = new ObjectMapper();
NoAutoDetectBean bean = m.readValue("{ \"z\" : 7 }",
NoAutoDetectBean.class);
NoAutoDetectBean bean = MAPPER.readValue("{ \"z\" : 7 }",
NoAutoDetectBean.class);
assertEquals(7, bean._z);
}

public void testTypeAnnotation() throws Exception
{
ObjectMapper m = new ObjectMapper();
AbstractWrapper w = m.readValue("{ \"value\" : \"abc\" }",
AbstractWrapper.class);
AbstractWrapper w = MAPPER.readValue("{ \"value\" : \"abc\" }",
AbstractWrapper.class);
Abstract bean = w.value;
assertNotNull(bean);
assertEquals(Concrete.class, bean.getClass());
assertEquals("abc", ((Concrete)bean).value);
}

public void testFailureDueToDups() throws Exception
public void testResolvedDups1() throws Exception
{
try {
writeAndMap(new ObjectMapper(), new DupFieldBean());
} catch (JsonMappingException e) {
verifyException(e, "Multiple fields representing property");
}
DupFieldBean result = MAPPER.readValue(a2q("{'z':3}"), DupFieldBean.class);
assertEquals(3, result._z);
assertEquals(0, result.z);
}

public void testFailureDueToDups2() throws Exception
public void testFailingDups2() throws Exception
{
// Fails because both fields have explicit annotation
try {
writeAndMap(new ObjectMapper(), new DupFieldBean2());
} catch (JsonMappingException e) {
verifyException(e, "Multiple fields representing property");
DupFieldBean2 result = MAPPER.readValue(a2q("{'foo':28}"), DupFieldBean2.class);
fail("Should not pass but got: "+result);
} catch (InvalidDefinitionException e) {
verifyException(e, "Multiple fields representing property \"foo\"");
}
}

// For [JACKSON-226], acceptable field overrides
public void testOkFieldOverride() throws Exception
{
ObjectMapper m = new ObjectMapper();
OkDupFieldBean result = m.readValue("{ \"x\" : 1, \"y\" : 2 }",
OkDupFieldBean result = MAPPER.readValue("{ \"x\" : 1, \"y\" : 2 }",
OkDupFieldBean.class);
assertEquals(1, result.myX);
assertEquals(2, result.y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

public class ExceptionJDKSerializable1195Test extends BaseMapTest
{
Expand All @@ -26,14 +27,14 @@ public void testExceptionSerializabilitySimple() throws Exception
try {
MAPPER.readValue("{\"x\": \"B\"}", ClassToRead.class);
fail("Should not have passed");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
_testSerializability(e);
}
try {
MAPPER.readValue("{\"classToRead\": {\"x\": \"B\"}}", ContainerClassToRead.class);
fail("Should not have passed");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
_testSerializability(e);
}
Expand All @@ -45,7 +46,7 @@ public void testExceptionSerializabilityStructured() throws Exception
MAPPER.readValue("{\"classesToRead\": [{\"x\": 1}, {\"x\": \"B\"}]}",
ContainerClassesToRead.class);
fail("Should not have passed");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
_testSerializability(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;

import java.io.IOException;

Expand Down Expand Up @@ -59,16 +60,16 @@ public void testNegativeForParent() throws IOException {
try {
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Parent.class).readValue("{}");
fail("Should not pass");
} catch (JsonMappingException ex) {
assertTrue(ex.getMessage().contains("missing type id property '@class'"));
} catch (InvalidTypeIdException e) {
assertTrue(e.getMessage().contains("missing type id property '@class'"));
}
}

public void testNegativeForChild() throws IOException {
try {
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Child.class).readValue("{}");
fail("Should not pass");
} catch (JsonMappingException ex) {
} catch (InvalidTypeIdException ex) {
assertTrue(ex.getMessage().contains("missing type id property '@class'"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ public void testAbstractBean() throws Exception
try {
m.readValue(serial, AbstractBean[].class);
fail("Should have failed");
} catch (JsonMappingException e) {
// let's use whatever is currently thrown exception... may change tho
verifyException(e, "cannot construct");
} catch (InvalidDefinitionException e) {
verifyException(e, "cannot construct instance of");
}
// and then that we will succeed with default type info
m = JsonMapper.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testCaseInsensitiveDeserialization() throws Exception
mapper.readValue(JSON, Issue476Bean.class);

fail("Should not accept improper case properties by default");
} catch (JsonMappingException e) {
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized field");
assertValidLocation(e.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public void testFailOnDupKeys() throws Exception
try {
MAPPER.reader(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY).readTree(DUP_JSON);
fail("Should have thrown exception!");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "duplicate field 'a'");
}
}
Expand All @@ -404,7 +404,7 @@ public void testFailOnDupNestedKeys() throws Exception
.with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.readValue(DOC);
fail("Should have thrown exception!");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "duplicate field 'foo'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testObjectId2759() throws Exception
mapper.readerFor(JsonNode.class)
.with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.readValue(json);
} catch (JsonMappingException e) {
} catch (DatabindException e) {
fail("Should not have duplicates, but JSON content has: "+json);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.json.JsonMapper;

/**
Expand Down Expand Up @@ -334,7 +335,7 @@ public void testInvalidProp() throws Exception
try {
MAPPER.writeValueAsString(new Broken());
fail("Should have thrown an exception");
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "cannot find property with name 'id'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

/**
* Simple unit tests to verify that it is possible to handle
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testSimpleDirectSelfReference() throws Exception
Bean[] wrapper = new Bean[] { first };
try {
writeAndMap(MAPPER, wrapper);
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "Direct self-reference leading to cycle");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

public class TestEmptyClass
extends BaseMapTest
Expand All @@ -15,8 +16,6 @@ static class Empty { }
@JsonSerialize
static class EmptyWithAnno { }

// for [JACKSON-695]:

@JsonSerialize(using=NonZeroSerializer.class)
static class NonZero {
public int nr;
Expand Down Expand Up @@ -65,7 +64,7 @@ public void testEmptyWithAnnotations() throws Exception
// First: without annotations, should complain
try {
serializeAsString(mapper, new Empty());
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "No serializer found for class");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.*;
Expand Down Expand Up @@ -110,7 +111,7 @@ static class FilteredProps
/**********************************************************
*/

private final ObjectMapper MAPPER = new ObjectMapper();
private final ObjectMapper MAPPER = newJsonMapper();

public void testSimpleInclusionFilter() throws Exception
{
Expand Down Expand Up @@ -145,7 +146,7 @@ public void testMissingFilter() throws Exception
try {
MAPPER.writeValueAsString(new Bean());
fail("Should have failed without configured filter");
} catch (JsonMappingException e) { // should be resolved to a MappingException (internally may be something else)
} catch (InvalidDefinitionException e) { // should be resolved to this (internally may be something else)
verifyException(e, "Cannot resolve PropertyFilter with id 'RootFilter'");
}

Expand All @@ -164,24 +165,23 @@ public void testDefaultFilter() throws Exception
assertEquals("{\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}

// [Issue#89] combining @JsonIgnore, @JsonProperty
// [databind#89] combining @JsonIgnore, @JsonProperty
public void testIssue89() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Pod pod = new Pod();
pod.username = "Bob";
pod.userPassword = "s3cr3t!";

String json = mapper.writeValueAsString(pod);
String json = MAPPER.writeValueAsString(pod);

assertEquals("{\"username\":\"Bob\"}", json);

Pod pod2 = mapper.readValue("{\"username\":\"Bill\",\"user_password\":\"foo!\"}", Pod.class);
Pod pod2 = MAPPER.readValue("{\"username\":\"Bill\",\"user_password\":\"foo!\"}", Pod.class);
assertEquals("Bill", pod2.username);
assertEquals("foo!", pod2.userPassword);
}

// Wrt [Issue#306]
// Wrt [databind#306]
public void testFilterOnProperty() throws Exception
{
FilterProvider prov = new SimpleFilterProvider()
Expand Down
Loading

0 comments on commit 03f92a5

Please sign in to comment.