-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Pointer based add and remove methods for #392 #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c75cd5
Pointer based add and remove methods for #392
jgustie 027f265
Improvements to add(ptr,val) and remove(ptr)
jgustie 71013d3
Merge remote-tracking branch 'upstream/master'
jgustie e5b37bd
Use ObjectMapper from base test class for tree construction
jgustie 0d04d0b
add(ptr,val) and remove(ptr) throw IllegalArgumentException for missi…
jgustie d44cf4d
Return value of add(ptr,val) should always be the current node
jgustie 9fb35d1
Make MissingNode._add(ptr) throw IllegalArgumentException
jgustie 77be9f1
Merge remote-tracking branch 'upstream/master'
jgustie 3b3dff3
Merge remote-tracking branch 'upstream/master'
jgustie 802e99a
Update comments
jgustie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/com/fasterxml/jackson/databind/TestAddByPointer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import com.fasterxml.jackson.core.JsonPointer; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.IntNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
/** | ||
* Basic tests to ensure we can add into the tree using JSON pointers. | ||
*/ | ||
public class TestAddByPointer extends BaseMapTest { | ||
|
||
private final JsonNode ONE = new IntNode(1); | ||
|
||
/* | ||
/********************************************************** | ||
/* JsonNode | ||
/********************************************************** | ||
*/ | ||
|
||
// TODO It would be nice to have a "TestNode" to isolate implementations in the base class | ||
|
||
public void testAddEmpty() { | ||
try { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
n.add(JsonPointer.compile(""), ONE); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
} | ||
} | ||
|
||
public void testAddRootPath() { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
assertEquals(ONE, n.add(JsonPointer.compile("/"), ONE)); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ArrayNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testAddArrayDepth1() { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
|
||
n.add(JsonPointer.compile("/0"), new IntNode(1)); | ||
assertEquals(1, n.get(0).asInt()); | ||
|
||
n.add(JsonPointer.compile("/0"), new IntNode(2)); | ||
assertEquals(2, n.get(0).asInt()); | ||
assertEquals(1, n.get(1).asInt()); | ||
|
||
n.add(JsonPointer.compile("/-"), new IntNode(3)); // special case: append | ||
assertEquals(2, n.get(0).asInt()); | ||
assertEquals(1, n.get(1).asInt()); | ||
assertEquals(3, n.get(2).asInt()); | ||
} | ||
|
||
public void testAddArrayDepth2() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
((ObjectNode) n).set("a", new ArrayNode(JsonNodeFactory.instance)); | ||
JsonPointer A_APPEND = JsonPointer.compile("/a/-"); | ||
n.add(A_APPEND, new IntNode(1)); | ||
n.add(A_APPEND, new IntNode(2)); | ||
n.add(A_APPEND, new IntNode(3)); | ||
assertEquals(1, n.at("/a/0").asInt()); | ||
assertEquals(2, n.at("/a/1").asInt()); | ||
assertEquals(3, n.at("/a/2").asInt()); | ||
} | ||
|
||
/** | ||
* RFC 6902 isn't clear about what this behavior should be: error, silent | ||
* ignore or perform insert. We error out to allow higher level | ||
* implementations the opportunity to handle the problem as they see fit. | ||
*/ | ||
public void testAddInvalidArrayElementPointer() { | ||
try { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
n.add(JsonPointer.compile("/a"), ONE); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ObjectNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testAddObjectDepth1() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
n.add(JsonPointer.compile("/a"), ONE); | ||
assertEquals(1, n.get("a").asInt()); | ||
} | ||
|
||
public void testAddObjectDepth2() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
((ObjectNode) n).set("o", new ObjectNode(JsonNodeFactory.instance)); | ||
n.add(JsonPointer.compile("/o/i"), ONE); | ||
assertEquals(1, n.at("/o/i").asInt()); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ValueNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testAddValue() { | ||
try { | ||
ONE.add(JsonPointer.compile("/0"), ONE); | ||
fail(); | ||
} catch (UnsupportedOperationException expected) { | ||
} | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/test/java/com/fasterxml/jackson/databind/TestRemoveByPointer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import com.fasterxml.jackson.core.JsonPointer; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.IntNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
/** | ||
* Basic tests to ensure we can remove from the tree using JSON pointers. | ||
*/ | ||
public class TestRemoveByPointer extends BaseMapTest { | ||
|
||
/* | ||
/********************************************************** | ||
/* JsonNode | ||
/********************************************************** | ||
*/ | ||
|
||
// TODO It would be nice to have a "TestNode" to isolate implementations in the base class | ||
|
||
public void testRemoveEmpty() { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
assertEquals(n, n.remove(JsonPointer.compile(""))); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ArrayNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testRemoveArrayRootPath() { | ||
try { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance); | ||
assertEquals(n, n.remove(JsonPointer.compile("/"))); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
} | ||
} | ||
|
||
public void testRemoveArrayDepth1() { | ||
JsonNode n = new ArrayNode(JsonNodeFactory.instance).add(1).add(2).add(3); | ||
|
||
n.remove(JsonPointer.compile("/1")); | ||
assertEquals(1, n.get(0).asInt()); | ||
assertEquals(3, n.get(1).asInt()); | ||
} | ||
|
||
public void testRemoveArrayDepth2() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
((ObjectNode) n).set("a", new ArrayNode(JsonNodeFactory.instance).add(1).add(2).add(3)); | ||
|
||
n.remove(JsonPointer.compile("/a/1")); | ||
assertEquals(1, n.at("/a/0").asInt()); | ||
assertEquals(3, n.at("/a/1").asInt()); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ObjectNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testObjectRemoveRootPath() { | ||
try { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
assertEquals(n, n.remove(JsonPointer.compile("/"))); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
} | ||
} | ||
|
||
public void testRemoveObjectDepth1() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance).set("i", new IntNode(1)); | ||
assertEquals(1, n.remove(JsonPointer.compile("/i")).asInt()); | ||
} | ||
|
||
public void testRemoveObjectDepth2() { | ||
JsonNode n = new ObjectNode(JsonNodeFactory.instance); | ||
((ObjectNode) n).set("o", new ObjectNode(JsonNodeFactory.instance).set("i", new IntNode(1))); | ||
assertEquals(1, n.remove(JsonPointer.compile("/o/i")).asInt()); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* ValueNode | ||
/********************************************************** | ||
*/ | ||
|
||
public void testValueRemoveRootPath() { | ||
try { | ||
JsonNode n = new IntNode(1); | ||
assertEquals(n, n.remove(JsonPointer.compile("/"))); | ||
fail(); | ||
} catch (UnsupportedOperationException expected) { | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion here; take JsonNodeFactory.instance, use its factory methods for creating ObjectNodes (etc) -- same for other usage too of course. Or, if tests have
ObjectMapper
, use array/object node factory methods.