Skip to content

Commit 03f92a5

Browse files
committed
Continuing on #2828, reduce use of JsonMappingException in tests
1 parent 2666a7f commit 03f92a5

14 files changed

+58
-63
lines changed

Diff for: src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
1515
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
16+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
1617
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1718
import com.fasterxml.jackson.databind.json.JsonMapper;
1819
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -128,8 +129,8 @@ public void testParserFeaturesComments() throws Exception
128129
try {
129130
reader.without(JsonReadFeature.ALLOW_JAVA_COMMENTS).readValue(JSON);
130131
fail("Should not have passed");
131-
} catch (JsonMappingException e) {
132-
// mapping exception since it gets wrapped
132+
} catch (DatabindException e) {
133+
// DatabindException since it gets wrapped
133134
verifyException(e, "foo");
134135
}
135136
}
@@ -480,7 +481,7 @@ public void testMissingType() throws Exception
480481
try {
481482
r.readValue("1");
482483
fail("Should not pass");
483-
} catch (JsonMappingException e) {
484+
} catch (InvalidDefinitionException e) {
484485
verifyException(e, "No value type configured");
485486
}
486487
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/IgnoreWithDeserTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.*;
44

55
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.databind.exc.IgnoredPropertyException;
67

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

7576
// or 'z'
7677
try {
7778
result = r.readValue(aposToQuotes("{'z':2 }"));
7879
fail("Should fail");
79-
} catch (JsonMappingException e) {
80+
} catch (IgnoredPropertyException e) {
8081
verifyException(e, "Ignored field");
8182
}
8283
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/TestFieldDeserialization.java

+22-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
55
import com.fasterxml.jackson.databind.*;
66
import com.fasterxml.jackson.databind.annotation.*;
7+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
78

89
/**
910
* Unit tests for verifying that field-backed properties can also be
@@ -12,12 +13,6 @@
1213
public class TestFieldDeserialization
1314
extends BaseMapTest
1415
{
15-
/*
16-
/**********************************************************
17-
/* Annotated helper classes
18-
/**********************************************************
19-
*/
20-
2116
static class SimpleFieldBean
2217
{
2318
public int x, y;
@@ -59,7 +54,7 @@ public static class DupFieldBean2
5954
public int _z;
6055

6156
@JsonDeserialize
62-
private int foo;
57+
int foo;
6358
}
6459

6560
public static class OkDupFieldBean
@@ -91,19 +86,19 @@ static class AbstractWrapper {
9186
/**********************************************************
9287
*/
9388

89+
private final ObjectMapper MAPPER = newJsonMapper();
90+
9491
public void testSimpleAutoDetect() throws Exception
9592
{
96-
ObjectMapper m = new ObjectMapper();
97-
SimpleFieldBean result = m.readValue("{ \"x\" : -13 }",
98-
SimpleFieldBean.class);
93+
SimpleFieldBean result = MAPPER.readValue("{ \"x\" : -13 }",
94+
SimpleFieldBean.class);
9995
assertEquals(-13, result.x);
10096
assertEquals(0, result.y);
10197
}
10298

10399
public void testSimpleAnnotation() throws Exception
104100
{
105-
ObjectMapper m = new ObjectMapper();
106-
SimpleFieldBean2 bean = m.readValue("{ \"values\" : [ \"x\", \"y\" ] }",
101+
SimpleFieldBean2 bean = MAPPER.readValue("{ \"values\" : [ \"x\", \"y\" ] }",
107102
SimpleFieldBean2.class);
108103
String[] values = bean.values;
109104
assertNotNull(values);
@@ -114,46 +109,42 @@ public void testSimpleAnnotation() throws Exception
114109

115110
public void testNoAutoDetect() throws Exception
116111
{
117-
ObjectMapper m = new ObjectMapper();
118-
NoAutoDetectBean bean = m.readValue("{ \"z\" : 7 }",
119-
NoAutoDetectBean.class);
112+
NoAutoDetectBean bean = MAPPER.readValue("{ \"z\" : 7 }",
113+
NoAutoDetectBean.class);
120114
assertEquals(7, bean._z);
121115
}
122116

123117
public void testTypeAnnotation() throws Exception
124118
{
125-
ObjectMapper m = new ObjectMapper();
126-
AbstractWrapper w = m.readValue("{ \"value\" : \"abc\" }",
127-
AbstractWrapper.class);
119+
AbstractWrapper w = MAPPER.readValue("{ \"value\" : \"abc\" }",
120+
AbstractWrapper.class);
128121
Abstract bean = w.value;
129122
assertNotNull(bean);
130123
assertEquals(Concrete.class, bean.getClass());
131124
assertEquals("abc", ((Concrete)bean).value);
132125
}
133126

134-
public void testFailureDueToDups() throws Exception
127+
public void testResolvedDups1() throws Exception
135128
{
136-
try {
137-
writeAndMap(new ObjectMapper(), new DupFieldBean());
138-
} catch (JsonMappingException e) {
139-
verifyException(e, "Multiple fields representing property");
140-
}
129+
DupFieldBean result = MAPPER.readValue(a2q("{'z':3}"), DupFieldBean.class);
130+
assertEquals(3, result._z);
131+
assertEquals(0, result.z);
141132
}
142133

143-
public void testFailureDueToDups2() throws Exception
134+
public void testFailingDups2() throws Exception
144135
{
136+
// Fails because both fields have explicit annotation
145137
try {
146-
writeAndMap(new ObjectMapper(), new DupFieldBean2());
147-
} catch (JsonMappingException e) {
148-
verifyException(e, "Multiple fields representing property");
138+
DupFieldBean2 result = MAPPER.readValue(a2q("{'foo':28}"), DupFieldBean2.class);
139+
fail("Should not pass but got: "+result);
140+
} catch (InvalidDefinitionException e) {
141+
verifyException(e, "Multiple fields representing property \"foo\"");
149142
}
150143
}
151144

152-
// For [JACKSON-226], acceptable field overrides
153145
public void testOkFieldOverride() throws Exception
154146
{
155-
ObjectMapper m = new ObjectMapper();
156-
OkDupFieldBean result = m.readValue("{ \"x\" : 1, \"y\" : 2 }",
147+
OkDupFieldBean result = MAPPER.readValue("{ \"x\" : 1, \"y\" : 2 }",
157148
OkDupFieldBean.class);
158149
assertEquals(1, result.myX);
159150
assertEquals(2, result.y);

Diff for: src/test/java/com/fasterxml/jackson/databind/interop/ExceptionJDKSerializable1195Test.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
78

89
public class ExceptionJDKSerializable1195Test extends BaseMapTest
910
{
@@ -26,14 +27,14 @@ public void testExceptionSerializabilitySimple() throws Exception
2627
try {
2728
MAPPER.readValue("{\"x\": \"B\"}", ClassToRead.class);
2829
fail("Should not have passed");
29-
} catch (JsonMappingException e) {
30+
} catch (MismatchedInputException e) {
3031
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
3132
_testSerializability(e);
3233
}
3334
try {
3435
MAPPER.readValue("{\"classToRead\": {\"x\": \"B\"}}", ContainerClassToRead.class);
3536
fail("Should not have passed");
36-
} catch (JsonMappingException e) {
37+
} catch (MismatchedInputException e) {
3738
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
3839
_testSerializability(e);
3940
}
@@ -45,7 +46,7 @@ public void testExceptionSerializabilityStructured() throws Exception
4546
MAPPER.readValue("{\"classesToRead\": [{\"x\": 1}, {\"x\": \"B\"}]}",
4647
ContainerClassesToRead.class);
4748
fail("Should not have passed");
48-
} catch (JsonMappingException e) {
49+
} catch (MismatchedInputException e) {
4950
verifyException(e, "Cannot deserialize value of type `int` from String \"B\": not a valid `int` value");
5051
_testSerializability(e);
5152
}

Diff for: src/test/java/com/fasterxml/jackson/databind/jsontype/TestBaseTypeAsDefault.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.JsonMappingException;
77
import com.fasterxml.jackson.databind.MapperFeature;
88
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
910

1011
import java.io.IOException;
1112

@@ -59,16 +60,16 @@ public void testNegativeForParent() throws IOException {
5960
try {
6061
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Parent.class).readValue("{}");
6162
fail("Should not pass");
62-
} catch (JsonMappingException ex) {
63-
assertTrue(ex.getMessage().contains("missing type id property '@class'"));
63+
} catch (InvalidTypeIdException e) {
64+
assertTrue(e.getMessage().contains("missing type id property '@class'"));
6465
}
6566
}
6667

6768
public void testNegativeForChild() throws IOException {
6869
try {
6970
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Child.class).readValue("{}");
7071
fail("Should not pass");
71-
} catch (JsonMappingException ex) {
72+
} catch (InvalidTypeIdException ex) {
7273
assertTrue(ex.getMessage().contains("missing type id property '@class'"));
7374
}
7475
}

Diff for: src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ public void testAbstractBean() throws Exception
174174
try {
175175
m.readValue(serial, AbstractBean[].class);
176176
fail("Should have failed");
177-
} catch (JsonMappingException e) {
178-
// let's use whatever is currently thrown exception... may change tho
179-
verifyException(e, "cannot construct");
177+
} catch (InvalidDefinitionException e) {
178+
verifyException(e, "cannot construct instance of");
180179
}
181180
// and then that we will succeed with default type info
182181
m = JsonMapper.builder()

Diff for: src/test/java/com/fasterxml/jackson/databind/misc/CaseInsensitiveDeserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void testCaseInsensitiveDeserialization() throws Exception
133133
mapper.readValue(JSON, Issue476Bean.class);
134134

135135
fail("Should not accept improper case properties by default");
136-
} catch (JsonMappingException e) {
136+
} catch (UnrecognizedPropertyException e) {
137137
verifyException(e, "Unrecognized field");
138138
assertValidLocation(e.getLocation());
139139
}

Diff for: src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public void testFailOnDupKeys() throws Exception
389389
try {
390390
MAPPER.reader(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY).readTree(DUP_JSON);
391391
fail("Should have thrown exception!");
392-
} catch (JsonMappingException e) {
392+
} catch (MismatchedInputException e) {
393393
verifyException(e, "duplicate field 'a'");
394394
}
395395
}
@@ -404,7 +404,7 @@ public void testFailOnDupNestedKeys() throws Exception
404404
.with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
405405
.readValue(DOC);
406406
fail("Should have thrown exception!");
407-
} catch (JsonMappingException e) {
407+
} catch (MismatchedInputException e) {
408408
verifyException(e, "duplicate field 'foo'");
409409
}
410410
}

Diff for: src/test/java/com/fasterxml/jackson/databind/objectid/ObjectId2759Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testObjectId2759() throws Exception
6666
mapper.readerFor(JsonNode.class)
6767
.with(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
6868
.readValue(json);
69-
} catch (JsonMappingException e) {
69+
} catch (DatabindException e) {
7070
fail("Should not have duplicates, but JSON content has: "+json);
7171
}
7272
}

Diff for: src/test/java/com/fasterxml/jackson/databind/objectid/TestObjectIdSerialization.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
66
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
89
import com.fasterxml.jackson.databind.json.JsonMapper;
910

1011
/**
@@ -334,7 +335,7 @@ public void testInvalidProp() throws Exception
334335
try {
335336
MAPPER.writeValueAsString(new Broken());
336337
fail("Should have thrown an exception");
337-
} catch (JsonMappingException e) {
338+
} catch (InvalidDefinitionException e) {
338339
verifyException(e, "cannot find property with name 'id'");
339340
}
340341
}

Diff for: src/test/java/com/fasterxml/jackson/databind/ser/CyclicTypeSerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
77

88
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
910

1011
/**
1112
* Simple unit tests to verify that it is possible to handle
@@ -79,7 +80,7 @@ public void testSimpleDirectSelfReference() throws Exception
7980
Bean[] wrapper = new Bean[] { first };
8081
try {
8182
writeAndMap(MAPPER, wrapper);
82-
} catch (JsonMappingException e) {
83+
} catch (InvalidDefinitionException e) {
8384
verifyException(e, "Direct self-reference leading to cycle");
8485
}
8586
}

Diff for: src/test/java/com/fasterxml/jackson/databind/ser/TestEmptyClass.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.JsonGenerator;
77
import com.fasterxml.jackson.databind.*;
88
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
910

1011
public class TestEmptyClass
1112
extends BaseMapTest
@@ -15,8 +16,6 @@ static class Empty { }
1516
@JsonSerialize
1617
static class EmptyWithAnno { }
1718

18-
// for [JACKSON-695]:
19-
2019
@JsonSerialize(using=NonZeroSerializer.class)
2120
static class NonZero {
2221
public int nr;
@@ -65,7 +64,7 @@ public void testEmptyWithAnnotations() throws Exception
6564
// First: without annotations, should complain
6665
try {
6766
serializeAsString(mapper, new Empty());
68-
} catch (JsonMappingException e) {
67+
} catch (InvalidDefinitionException e) {
6968
verifyException(e, "No serializer found for class");
7069
}
7170

Diff for: src/test/java/com/fasterxml/jackson/databind/ser/filter/TestJsonFilter.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.JsonGenerator;
77
import com.fasterxml.jackson.core.JsonStreamContext;
88
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
910
import com.fasterxml.jackson.databind.ser.FilterProvider;
1011
import com.fasterxml.jackson.databind.ser.PropertyWriter;
1112
import com.fasterxml.jackson.databind.ser.impl.*;
@@ -110,7 +111,7 @@ static class FilteredProps
110111
/**********************************************************
111112
*/
112113

113-
private final ObjectMapper MAPPER = new ObjectMapper();
114+
private final ObjectMapper MAPPER = newJsonMapper();
114115

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

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

167-
// [Issue#89] combining @JsonIgnore, @JsonProperty
168+
// [databind#89] combining @JsonIgnore, @JsonProperty
168169
public void testIssue89() throws Exception
169170
{
170-
ObjectMapper mapper = new ObjectMapper();
171171
Pod pod = new Pod();
172172
pod.username = "Bob";
173173
pod.userPassword = "s3cr3t!";
174174

175-
String json = mapper.writeValueAsString(pod);
175+
String json = MAPPER.writeValueAsString(pod);
176176

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

179-
Pod pod2 = mapper.readValue("{\"username\":\"Bill\",\"user_password\":\"foo!\"}", Pod.class);
179+
Pod pod2 = MAPPER.readValue("{\"username\":\"Bill\",\"user_password\":\"foo!\"}", Pod.class);
180180
assertEquals("Bill", pod2.username);
181181
assertEquals("foo!", pod2.userPassword);
182182
}
183183

184-
// Wrt [Issue#306]
184+
// Wrt [databind#306]
185185
public void testFilterOnProperty() throws Exception
186186
{
187187
FilterProvider prov = new SimpleFilterProvider()

0 commit comments

Comments
 (0)