### Question:
JsonNodeCursor extends AbstractTreeCursor<JsonNode> { @Override protected boolean hasChildren(JsonNode node) { return node.isContainerNode() && node.size() > 0; } JsonNodeCursor(Path p, JsonNode start);   }### Answer:
@Test public void hasChildren_TextNode() throws IOException { String jsonString = "{\"text\":\"value\"}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof TextNode); Assert.assertEquals("text", c.getCurrentPath().toString()); Assert.assertFalse(c.hasChildren(c.getCurrentNode())); }
@Test public void hasChildren_ObjectNode() throws IOException { String jsonString = "{\"x\":{\"text\":\"value\"}}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof ObjectNode); Assert.assertEquals("x", c.getCurrentPath().toString()); Assert.assertTrue(c.hasChildren(c.getCurrentNode())); }
@Test public void hasChildren_ArrayNode() throws IOException { String jsonString = "{\"x\":[1,2,3,4]}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof ArrayNode); Assert.assertEquals("x", c.getCurrentPath().toString()); Assert.assertTrue(c.hasChildren(c.getCurrentNode())); }
@Test public void hasChildren_ArrayNodeEmpty() throws IOException { String jsonString = "{\"x\":[]}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof ArrayNode); Assert.assertEquals("x", c.getCurrentPath().toString()); Assert.assertFalse(c.hasChildren(c.getCurrentNode())); }
@Test public void parent_TextNode() throws IOException { String jsonString = "{\"parent\":{\"text\":\"value\"}}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof TextNode); Assert.assertEquals("parent.text", c.getCurrentPath().toString()); Assert.assertFalse(c.hasChildren(c.getCurrentNode())); Assert.assertTrue(c.parent()); Assert.assertTrue(c.parent()); Assert.assertSame(node, c.getCurrentNode()); }
@Test public void parent_ObjectNode() throws IOException { String jsonString = "{\"parent\":{\"x\":{\"text\":\"value\"}}}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof ObjectNode); Assert.assertEquals("parent.x", c.getCurrentPath().toString()); Assert.assertTrue(c.hasChildren(c.getCurrentNode())); Assert.assertTrue(c.parent()); Assert.assertTrue(c.parent()); Assert.assertSame(node, c.getCurrentNode()); }
@Test public void parent_ArrayNode() throws IOException { String jsonString = "{\"parent\":{\"x\":[1,2,3,4]}}"; JsonNode node = JsonUtils.json(jsonString); JsonNodeCursor c = new JsonNodeCursor(new Path(""), node); Assert.assertTrue(c.firstChild()); Assert.assertTrue(c.firstChild()); Assert.assertNotNull(c.getCurrentNode()); Assert.assertTrue(c.getCurrentNode() instanceof ArrayNode); Assert.assertEquals("parent.x", c.getCurrentPath().toString()); Assert.assertTrue(c.hasChildren(c.getCurrentNode())); Assert.assertTrue(c.parent()); Assert.assertTrue(c.parent()); Assert.assertSame(node, c.getCurrentNode()); }