|
34 | 34 | import java.util.Arrays;
|
35 | 35 | import java.util.Collections;
|
36 | 36 | import java.util.List;
|
| 37 | +import java.util.concurrent.atomic.AtomicReference; |
37 | 38 |
|
38 | 39 | import static org.hamcrest.Matchers.containsString;
|
39 | 40 | import static org.hamcrest.Matchers.hasSize;
|
@@ -650,6 +651,49 @@ public void setArray(List<Object> testArray) {
|
650 | 651 | assertThat(ex.getMessage(), containsString("[foo] failed to parse field [int_array]"));
|
651 | 652 | }
|
652 | 653 |
|
| 654 | + public void testNoopDeclareObject() throws IOException { |
| 655 | + ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); |
| 656 | + parser.declareString(AtomicReference::set, new ParseField("body")); |
| 657 | + parser.declareObject((a,b) -> {}, (p, c) -> null, new ParseField("noop")); |
| 658 | + |
| 659 | + assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); |
| 660 | + Exception garbageException = expectThrows(IllegalStateException.class, () -> parser.parse( |
| 661 | + createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), |
| 662 | + null)); |
| 663 | + assertEquals("parser for [noop] did not end on END_OBJECT", garbageException.getMessage()); |
| 664 | + Exception sneakyException = expectThrows(IllegalStateException.class, () -> parser.parse( |
| 665 | + createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), |
| 666 | + null)); |
| 667 | + assertEquals("parser for [noop] did not end on END_OBJECT", sneakyException.getMessage()); |
| 668 | + } |
| 669 | + |
| 670 | + public void testNoopDeclareField() throws IOException { |
| 671 | + ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); |
| 672 | + parser.declareString(AtomicReference::set, new ParseField("body")); |
| 673 | + parser.declareField((a,b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY); |
| 674 | + |
| 675 | + assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); |
| 676 | + Exception e = expectThrows(IllegalStateException.class, () -> parser.parse( |
| 677 | + createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"), |
| 678 | + null)); |
| 679 | + assertEquals("parser for [noop] did not end on END_ARRAY", e.getMessage()); |
| 680 | + } |
| 681 | + |
| 682 | + public void testNoopDeclareObjectArray() throws IOException { |
| 683 | + ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); |
| 684 | + parser.declareString(AtomicReference::set, new ParseField("body")); |
| 685 | + parser.declareObjectArray((a,b) -> {}, (p, c) -> null, new ParseField("noop")); |
| 686 | + |
| 687 | + XContentParseException garbageError = expectThrows(XContentParseException.class, () -> parser.parse( |
| 688 | + createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), |
| 689 | + null)); |
| 690 | + assertEquals("expected value but got [FIELD_NAME]", garbageError.getCause().getMessage()); |
| 691 | + XContentParseException sneakyError = expectThrows(XContentParseException.class, () -> parser.parse( |
| 692 | + createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), |
| 693 | + null)); |
| 694 | + assertEquals("expected value but got [FIELD_NAME]", sneakyError.getCause().getMessage()); |
| 695 | + } |
| 696 | + |
653 | 697 | static class NamedObjectHolder {
|
654 | 698 | public static final ObjectParser<NamedObjectHolder, Void> PARSER = new ObjectParser<>("named_object_holder",
|
655 | 699 | NamedObjectHolder::new);
|
|
0 commit comments