|
10 | 10 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
11 | 11 | import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
12 | 12 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
| 13 | +import com.fasterxml.jackson.databind.exc.ValueInstantiationException; |
13 | 14 | import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
|
14 | 15 |
|
15 | 16 | public class BuilderSimpleTest extends BaseMapTest
|
@@ -295,6 +296,54 @@ private Value2354 build() {
|
295 | 296 | }
|
296 | 297 | }
|
297 | 298 |
|
| 299 | + @JsonDeserialize(builder = ValidatingValue.Builder.class) |
| 300 | + static class ValidatingValue |
| 301 | + { |
| 302 | + private final String first; |
| 303 | + private final String second; |
| 304 | + |
| 305 | + ValidatingValue(String first, String second) { |
| 306 | + this.first = first; |
| 307 | + this.second = second; |
| 308 | + } |
| 309 | + |
| 310 | + static class ValidationException extends RuntimeException |
| 311 | + { |
| 312 | + ValidationException(String message) { |
| 313 | + super(message); |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + static class Builder |
| 318 | + { |
| 319 | + |
| 320 | + private String first; |
| 321 | + private String second; |
| 322 | + |
| 323 | + @JsonSetter("a") |
| 324 | + Builder first(String value) { |
| 325 | + this.first = value; |
| 326 | + return this; |
| 327 | + } |
| 328 | + |
| 329 | + @JsonSetter("b") |
| 330 | + Builder second(String value) { |
| 331 | + this.second = value; |
| 332 | + return this; |
| 333 | + } |
| 334 | + |
| 335 | + ValidatingValue build() { |
| 336 | + if (first == null) { |
| 337 | + throw new ValidationException("Missing first"); |
| 338 | + } |
| 339 | + if (second == null) { |
| 340 | + throw new ValidationException("Missing second"); |
| 341 | + } |
| 342 | + return new ValidatingValue(first, second); |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + |
298 | 347 | /*
|
299 | 348 | /**********************************************************
|
300 | 349 | /* Test methods
|
@@ -430,4 +479,66 @@ public void testPrivateInnerBuilder() throws Exception
|
430 | 479 | Value2354 result = MAPPER.readValue(json, Value2354.class);
|
431 | 480 | assertEquals(13, result.value());
|
432 | 481 | }
|
| 482 | + |
| 483 | + public void testSuccessfulValidatingBuilder() throws Exception |
| 484 | + { |
| 485 | + ValidatingValue result = MAPPER.readValue(aposToQuotes("{'a':'1','b':'2'}"), ValidatingValue.class); |
| 486 | + assertEquals("1", result.first); |
| 487 | + assertEquals("2", result.second); |
| 488 | + } |
| 489 | + |
| 490 | + |
| 491 | + public void testFailingValidatingBuilderWithExceptionWrapping() throws Exception |
| 492 | + { |
| 493 | + ObjectMapper withWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS) |
| 494 | + ? MAPPER : MAPPER.copy().enable(DeserializationFeature.WRAP_EXCEPTIONS); |
| 495 | + try { |
| 496 | + withWrapping |
| 497 | + .readValue(aposToQuotes("{'a':'1'}"), ValidatingValue.class); |
| 498 | + fail("Expected an exception"); |
| 499 | + } catch (JsonMappingException e) { |
| 500 | + assertTrue(e.getMessage().contains("Missing second")); |
| 501 | + assertTrue(e.getCause() instanceof ValidatingValue.ValidationException); |
| 502 | + } |
| 503 | + } |
| 504 | + |
| 505 | + public void testFailingValidatingBuilderWithExceptionWrappingFromTree() throws Exception |
| 506 | + { |
| 507 | + ObjectMapper withWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS) |
| 508 | + ? MAPPER : MAPPER.copy().enable(DeserializationFeature.WRAP_EXCEPTIONS); |
| 509 | + try { |
| 510 | + JsonNode tree = withWrapping.readTree(aposToQuotes("{'a':'1'}")); |
| 511 | + withWrapping.treeToValue(tree, ValidatingValue.class); |
| 512 | + fail("Expected an exception"); |
| 513 | + } catch (ValueInstantiationException e) { |
| 514 | + assertTrue(e.getMessage().contains("Missing second")); |
| 515 | + assertTrue(e.getCause() instanceof ValidatingValue.ValidationException); |
| 516 | + } |
| 517 | + } |
| 518 | + |
| 519 | + public void testFailingValidatingBuilderWithoutExceptionWrapping() throws Exception |
| 520 | + { |
| 521 | + ObjectMapper withoutWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS) |
| 522 | + ? MAPPER.copy().disable(DeserializationFeature.WRAP_EXCEPTIONS) : MAPPER; |
| 523 | + try { |
| 524 | + withoutWrapping |
| 525 | + .readValue(aposToQuotes("{'a':'1'}"), ValidatingValue.class); |
| 526 | + fail("Expected an exception"); |
| 527 | + } catch (ValidatingValue.ValidationException e) { |
| 528 | + assertEquals("Missing second", e.getMessage()); |
| 529 | + } |
| 530 | + } |
| 531 | + |
| 532 | + public void testFailingValidatingBuilderWithoutExceptionWrappingFromTree() throws Exception |
| 533 | + { |
| 534 | + ObjectMapper withoutWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS) |
| 535 | + ? MAPPER.copy().disable(DeserializationFeature.WRAP_EXCEPTIONS) : MAPPER; |
| 536 | + try { |
| 537 | + JsonNode tree = withoutWrapping.readTree(aposToQuotes("{'a':'1'}")); |
| 538 | + withoutWrapping.treeToValue(tree, ValidatingValue.class); |
| 539 | + fail("Expected an exception"); |
| 540 | + } catch (ValidatingValue.ValidationException e) { |
| 541 | + assertEquals("Missing second", e.getMessage()); |
| 542 | + } |
| 543 | + } |
433 | 544 | }
|
0 commit comments