|
| 1 | +package tools.jackson.databind.exc; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import tools.jackson.core.JacksonException; |
| 9 | +import tools.jackson.databind.*; |
| 10 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.annotation.*; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +// [databind#4603]: Stop unwrapping root cause |
| 17 | +public class UnwrapRootCause4603Test |
| 18 | + extends DatabindTestUtil |
| 19 | +{ |
| 20 | + @SuppressWarnings("serial") |
| 21 | + static class CustomException extends RuntimeException { |
| 22 | + public CustomException(String message) { |
| 23 | + super(message); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static class Feature1347DeserBean |
| 28 | + { |
| 29 | + int value; |
| 30 | + |
| 31 | + public void setValue(int x) { |
| 32 | + throw new CustomException("setValue, fail on purpose"); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static class AnySetterBean |
| 37 | + { |
| 38 | + protected Map<String, Integer> props = new HashMap<String, Integer>(); |
| 39 | + |
| 40 | + @JsonAnySetter |
| 41 | + public void prop(String name, Integer value) { |
| 42 | + throw new CustomException("@JsonAnySetter, fail on purpose"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + static class Baz { |
| 47 | + private String qux; |
| 48 | + |
| 49 | + @JsonCreator |
| 50 | + public Baz(@JsonProperty("qux") String qux) { |
| 51 | + this.qux = qux; |
| 52 | + } |
| 53 | + |
| 54 | + public String getQux() { |
| 55 | + return qux; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static final String JSON = a2q("{'value':3}"); |
| 60 | + |
| 61 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 62 | + |
| 63 | + // Whether disabled or enabled, should get ArithmeticException |
| 64 | + @Test |
| 65 | + public void testExceptionWrappingConfiguration() |
| 66 | + throws Exception |
| 67 | + { |
| 68 | + JacksonException disabledResult = _tryDeserializeWith(MAPPER); |
| 69 | + // !!! TODO: ought to be DatabindException |
| 70 | + assertInstanceOf(JacksonException.class, disabledResult); |
| 71 | + // We are throwing exception inside a setter, but InvocationTargetException |
| 72 | + // will still be unwrapped |
| 73 | + assertInstanceOf(CustomException.class, disabledResult.getCause()); |
| 74 | + } |
| 75 | + |
| 76 | + private JacksonException _tryDeserializeWith(ObjectMapper mapper) { |
| 77 | + return assertThrows(JacksonException.class, |
| 78 | + () -> mapper.readValue(JSON, Feature1347DeserBean.class) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testWithAnySetter() |
| 84 | + throws Exception |
| 85 | + { |
| 86 | + DatabindException result = assertThrows(DatabindException.class, |
| 87 | + () -> MAPPER.readValue(a2q("{'a':3}"), AnySetterBean.class)); |
| 88 | + assertInstanceOf(CustomException.class, result.getCause()); |
| 89 | + } |
| 90 | +} |
0 commit comments