|
| 1 | +package com.fasterxml.jackson.databind.node; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonPointer; |
| 4 | +import com.fasterxml.jackson.databind.*; |
| 5 | + |
| 6 | +// for [databuind#1980] implementation |
| 7 | +public class WithPathTest extends BaseMapTest |
| 8 | +{ |
| 9 | + /* |
| 10 | + /********************************************************************** |
| 11 | + /* Test methods |
| 12 | + /********************************************************************** |
| 13 | + */ |
| 14 | + |
| 15 | + private final ObjectMapper MAPPER = sharedMapper(); |
| 16 | + |
| 17 | + public void testValidWithObjectTrivial() throws Exception |
| 18 | + { |
| 19 | + ObjectNode root = MAPPER.createObjectNode(); |
| 20 | + ObjectNode match = root.withObject(JsonPointer.empty()); |
| 21 | + assertSame(root, match); |
| 22 | + } |
| 23 | + |
| 24 | + public void testValidWithObjectSimple() throws Exception |
| 25 | + { |
| 26 | + ObjectNode root = MAPPER.createObjectNode(); |
| 27 | + ObjectNode match = root.withObject(JsonPointer.compile("/a/b")); |
| 28 | + assertTrue(match.isObject()); |
| 29 | + match.put("value", 42); |
| 30 | + |
| 31 | + assertEquals(a2q("{'a':{'b':{'value':42}}}"), |
| 32 | + root.toString()); |
| 33 | + |
| 34 | + // and with that |
| 35 | + ObjectNode match2 = root.withObject(JsonPointer.compile("/a/b")); |
| 36 | + assertSame(match, match2); |
| 37 | + match.put("value2", true); |
| 38 | + |
| 39 | + assertEquals(a2q("{'a':{'b':{'value':42,'value2':true}}}"), |
| 40 | + root.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + public void testValidWithObjectWithArray() throws Exception |
| 44 | + { |
| 45 | + ObjectNode root = MAPPER.createObjectNode(); |
| 46 | + root.putArray("arr"); |
| 47 | + ObjectNode match = root.withObject(JsonPointer.compile("/arr/2")); |
| 48 | + assertTrue(match.isObject()); |
| 49 | + match.put("value", 42); |
| 50 | + assertEquals(a2q("{'arr':[null,null,{'value':42}]}"), |
| 51 | + root.toString()); |
| 52 | + |
| 53 | + // But also verify we can match |
| 54 | + ObjectNode match2 = root.withObject(JsonPointer.compile("/arr/2")); |
| 55 | + assertSame(match, match2); |
| 56 | + match.put("value2", true); |
| 57 | + assertEquals(a2q("{'arr':[null,null,{'value':42,'value2':true}]}"), |
| 58 | + root.toString()); |
| 59 | + } |
| 60 | +} |
0 commit comments