Skip to content

Commit

Permalink
Completed eradication of JsonMappingException from tests (which can…
Browse files Browse the repository at this point in the history
… be done for 2.x); now moving to 3.0 side
  • Loading branch information
cowtowncoder committed Jan 22, 2021
1 parent db3aefb commit 516c91f
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,15 @@ public void testReconfiguringOfWrapping() throws Exception
result = mapper.readerFor(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
.readValue(jsonUnwrapped);
fail("Should have failed");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Root name ('a')");
}
// except wrapping may be expected:
result = mapper.readerFor(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
.readValue(jsonWrapped);
assertNotNull(result);
}

// [JACKSON-764]

public void testRootUsingExplicitConfig() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
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 @@ -102,7 +103,7 @@ public void testIgnoredCycle() throws Exception
try {
MAPPER.writeValueAsString(self1);
fail("Should fail with direct self-ref");
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "Direct self-reference");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.annotation.*;

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

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -138,15 +133,15 @@ public void testIncludeIgnoredAndUnrecognizedField() throws Exception
try {
r.readValue(aposToQuotes("{'x':3, 'y': 4, 'z': 5}"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (IgnoredPropertyException e) {
verifyException(e, "Ignored field");
}

// or fail on unrecognized properties
try {
r.readValue(aposToQuotes("{'y': 3, 'z':2 }"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized field");
}

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.MismatchedInputException;

public class ReadOnlyDeserFailOnUnknown2719Test extends BaseMapTest
{
Expand Down Expand Up @@ -36,7 +37,7 @@ public void testFailOnIgnore() throws Exception
try {
r.readValue(aposToQuotes("{'login':'foo', 'password':'bar'}"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Ignored field");
}

Expand All @@ -45,7 +46,7 @@ public void testFailOnIgnore() throws Exception
try {
r.readValue(aposToQuotes("{'login':'foo', 'username':'bar'}"));
fail("Should fail");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Ignored field");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,16 @@ static class ArrayListBean extends BaseListBean
public void setList(ArrayList<String> l) { super.setList(l); }
}

// 27-Feb-2010, tatus: Won't fix immediately, need to comment out
/*
static class OverloadBean
{
String a;
public OverloadBean() { }
public void setA(int value) { a = String.valueOf(value); }
public void setA(String value) { a = value; }
}
*/

static class NumberBean {
protected Object value;
public void setValue(Number n) { value = n; }
protected Object value;

public void setValue(Number n) { value = n; }
}

static class WasNumberBean extends NumberBean {
public void setValue(String str) { value = str; }
public void setValue(String str) { value = str; }
}

// [JACKSON-739]
static class Overloaded739
{
protected Object _value;
Expand All @@ -69,35 +55,17 @@ static class Overloaded739
* And then a Bean that is conflicting and should not work
*/
static class ConflictBean {
public void setA(ArrayList<Object> a) { }
public void setA(LinkedList<Object> a) { }
public void setA(ArrayList<Object> a) { }
public void setA(LinkedList<Object> a) { }
}

/*
/************************************************************
/* Unit tests, valid
/************************************************************
*/

private final ObjectMapper MAPPER = new ObjectMapper();

/**
* Unit test related to [JACKSON-189]
*/
// 27-Feb-2010, tatus: Won't fix immediately, need to comment out
/*
public void testSimpleOverload() throws Exception
{
OverloadBean bean;
try {
bean = new ObjectMapper().readValue("{ \"a\" : 13 }", OverloadBean.class);
} catch (JsonMappingException e) {
fail("Did not expect an exception, got: "+e.getMessage());
return;
}
assertEquals("13", bean.a);
}
*/
private final ObjectMapper MAPPER = newJsonMapper();

/**
* It should be ok to overload with specialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.*;

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

/**
* Unit tests for verifying that feature requested
Expand Down Expand Up @@ -78,10 +79,9 @@ public void testSimpleSetterlessCollectionFailure()
m.readValue
("{\"values\":[ \"abc\", \"def\" ]}", CollectionBean.class);
fail("Expected an exception");
} catch (JsonMappingException e) {
/* Not a good exception, ideally could suggest a need for
* a setter...?
*/
} catch (MismatchedInputException e) {
// Not a good exception, ideally could suggest a need for
// a setter...?
verifyException(e, "Unrecognized field");
}
}
Expand All @@ -108,7 +108,7 @@ public void testSimpleSetterlessMapFailure()
m.readValue
("{\"values\":{ \"a\":3 }}", MapBean.class);
fail("Expected an exception");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Unrecognized field");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.*;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
Expand Down Expand Up @@ -285,7 +286,7 @@ public void testOverrideClassInvalid() throws Exception
BrokenCollectionHolder result = MAPPER.readValue
("{ \"strings\" : [ ] }", BrokenCollectionHolder.class);
fail("Expected a failure, but got results: "+result);
} catch (JsonMappingException jme) {
} catch (DatabindException jme) {
verifyException(jme, "not subtype of");
}
}
Expand Down Expand Up @@ -355,7 +356,7 @@ public void testOverrideKeyClassInvalid() throws Exception
BrokenMapKeyHolder result = MAPPER.readValue
("{ \"123\" : \"xxx\" }", BrokenMapKeyHolder.class);
fail("Expected a failure, but got results: "+result);
} catch (JsonMappingException jme) {
} catch (DatabindException jme) {
verifyException(jme, "not subtype of");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

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

/**
* Tests to ensure one can disable {@link JsonCreator} annotations.
Expand Down Expand Up @@ -36,7 +37,7 @@ public NonConflictingCreators(String foo, int value) { }

public void testDisabling() throws Exception
{
final ObjectMapper mapper = objectMapper();
final ObjectMapper mapper = newJsonMapper();

// first, non-problematic case
NonConflictingCreators value = mapper.readValue(quote("abc"), NonConflictingCreators.class);
Expand All @@ -47,7 +48,7 @@ public void testDisabling() throws Exception
try {
/*ConflictingCreators value =*/ mapper.readValue(quote("abc"), ConflictingCreators.class);
fail("Should have failed with JsonCreator conflict");
} catch (JsonMappingException e) {
} catch (InvalidDefinitionException e) {
verifyException(e, "Conflicting property-based creators");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

/**
* Tests to ensure that deserialization fails when a bean property has a null value
Expand Down Expand Up @@ -43,15 +44,15 @@ public void testRequiredNonNullParam() throws Exception
try {
r.readValue(aposToQuotes("{}"));
fail("Should not pass third test");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Null value for creator property 'name'");
}

// Fourth: throws exception if property is set to null explicitly
try {
r.readValue(aposToQuotes("{'age': 5, 'name': null}"));
fail("Should not pass fourth test");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Null value for creator property 'name'");
}
}
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.MismatchedInputException;

public class RequiredCreatorTest extends BaseMapTest
{
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testRequiredAnnotatedParam() throws Exception
try {
POINT_READER.readValue(aposToQuotes("{'y':3}"));
fail("Should not pass");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Missing required creator property 'x' (index 0)");
}
}
Expand All @@ -97,7 +98,7 @@ public void testRequiredGloballyParam() throws Exception
try {
r.readValue(aposToQuotes("{'x':6}"));
fail("Should not pass");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Missing creator property 'y' (index 1)");
}
}
Expand All @@ -109,7 +110,7 @@ public void testRequiredViaParameter2591() throws Exception
try {
/*LoginUserResponse resp =*/ MAPPER.readValue(input, LoginUserResponse.class);
fail("Shoud not pass");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Missing required creator property 'otp'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

import java.io.IOException;

public class TestCreatorNullPrimitives extends BaseMapTest {

public class TestCreatorNullPrimitives extends BaseMapTest
{
// [databind#2101]
static class JsonEntity {
protected final int x;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void testCreatorNullPrimitive() throws IOException {
try {
r.readValue(json);
fail("Should not have succeeded");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type `int`");
assertEquals(1, e.getPath().size());
assertEquals("y", e.getPath().get(0).getFieldName());
Expand All @@ -64,7 +64,7 @@ public void testCreatorNullPrimitiveInNestedObject() throws IOException {
try {
r.readValue(json);
fail("Should not have succeeded");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type `int`");
assertEquals(2, e.getPath().size());
assertEquals("y", e.getPath().get(1).getFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void testJackson438() throws Exception
try {
MAPPER.readValue("{ \"name\":\"foobar\" }", BeanFor438.class);
fail("Should have failed");
} catch (JsonMappingException e0) {
} catch (Exception e0) {
e = e0;
}
if (!(e instanceof ValueInstantiationException)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

Expand Down Expand Up @@ -180,7 +180,7 @@ public void testWithCtorAndDelegate() throws Exception
CtorBean711 bean = null;
try {
bean = mapper.readValue("38", CtorBean711.class);
} catch (JsonMappingException e) {
} catch (JacksonException e) {
fail("Did not expect problems, got: "+e.getMessage());
}
assertEquals(38, bean.age);
Expand All @@ -196,7 +196,7 @@ public void testWithFactoryAndDelegate() throws Exception
FactoryBean711 bean = null;
try {
bean = mapper.readValue("38", FactoryBean711.class);
} catch (JsonMappingException e) {
} catch (JacksonException e) {
fail("Did not expect problems, got: "+e.getMessage());
}
assertEquals(38, bean.age);
Expand Down
Loading

0 comments on commit 516c91f

Please sign in to comment.