Skip to content

Commit 0241fc4

Browse files
committed
Minor refactoring, test improvements
1 parent 7a48952 commit 0241fc4

File tree

4 files changed

+58
-29
lines changed

4 files changed

+58
-29
lines changed

src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ protected ObjectNode _withObject(JsonPointer origPtr,
8484
}
8585
// Ok no; must replace if allowed to
8686
if (!_withObjectMayReplace(n, overwriteMode)) {
87-
return _reportWrongNodeType(
88-
"Cannot replace `JsonNode` of type `%s` for property \"%s\" in JSON Pointer \"%s\" (mode %s)",
89-
n.getClass().getName(), currentPtr.getMatchingProperty(),
90-
origPtr, overwriteMode);
87+
_withObjectVerifyReplace(origPtr, currentPtr, overwriteMode, preferIndex, n);
9188
}
9289
}
9390
// Either way; must replace or add a new property

src/main/java/com/fasterxml/jackson/databind/node/BaseJsonNode.java

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ protected ObjectNode _withObject(JsonPointer origPtr,
148148
return null;
149149
}
150150

151+
protected void _withObjectVerifyReplace(JsonPointer origPtr,
152+
JsonPointer currentPtr,
153+
OverwriteMode overwriteMode, boolean preferIndex,
154+
JsonNode toReplace)
155+
{
156+
if (!_withObjectMayReplace(toReplace, overwriteMode)) {
157+
_reportWrongNodeType(
158+
"Cannot replace `JsonNode` of type `%s` for property \"%s\" in JSON Pointer \"%s\" (mode `OverwriteMode.%s`)",
159+
toReplace.getClass().getName(), currentPtr.getMatchingProperty(),
160+
origPtr, overwriteMode);
161+
}
162+
}
163+
151164
protected boolean _withObjectMayReplace(JsonNode node, OverwriteMode overwriteMode) {
152165
switch (overwriteMode) {
153166
case NONE:

src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ protected ObjectNode _withObject(JsonPointer origPtr,
8080
return found;
8181
}
8282
// Ok no; must replace if allowed to
83-
if (!_withObjectMayReplace(n, overwriteMode)) {
84-
return _reportWrongNodeType(
85-
"Cannot replace `JsonNode` of type `%s` for property \"%s\" in JSON Pointer \"%s\" (mode `OverwriteMode.%s`)",
86-
n.getClass().getName(), currentPtr.getMatchingProperty(),
87-
origPtr, overwriteMode);
88-
}
83+
_withObjectVerifyReplace(origPtr, currentPtr, overwriteMode, preferIndex, n);
8984
}
9085
// Either way; must replace or add a new property
9186
return _withObjectAddTailProperty(currentPtr, preferIndex);

src/test/java/com/fasterxml/jackson/databind/node/WithPathTest.java

+43-19
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
// for [databuind#1980] implementation
88
public class WithPathTest extends BaseMapTest
99
{
10+
private final ObjectMapper MAPPER = sharedMapper();
11+
1012
/*
1113
/**********************************************************************
12-
/* Test methods
14+
/* Test methods, withObject()
1315
/**********************************************************************
1416
*/
1517

16-
private final ObjectMapper MAPPER = sharedMapper();
17-
1818
public void testValidWithObjectTrivial() throws Exception
1919
{
2020
ObjectNode root = MAPPER.createObjectNode();
@@ -77,13 +77,7 @@ public void testObjectPathWithReplace() throws Exception
7777
root.put("a", 13);
7878

7979
// First, without replacement (default) get exception
80-
try {
81-
root.withObject(abPath);
82-
fail("Should not pass");
83-
} catch (UnsupportedOperationException e) {
84-
verifyException(e, "Cannot replace `JsonNode` of type ");
85-
verifyException(e, "(mode `OverwriteMode.NULLS`)");
86-
}
80+
_verifyReplaceFail(root, abPath, null);
8781

8882
// Except fine via nulls (by default)
8983
root.putNull("a");
@@ -93,13 +87,7 @@ public void testObjectPathWithReplace() throws Exception
9387

9488
// but not if prevented
9589
root = (ObjectNode) MAPPER.readTree(a2q("{'a':null}"));
96-
try {
97-
root.withObject(abPath, OverwriteMode.NONE, true);
98-
fail("Should not pass");
99-
} catch (UnsupportedOperationException e) {
100-
verifyException(e, "Cannot replace `JsonNode` of type ");
101-
verifyException(e, "(mode `OverwriteMode.NONE`)");
102-
}
90+
_verifyReplaceFail(root, abPath, OverwriteMode.NONE);
10391
}
10492

10593
public void testValidWithObjectWithArray() throws Exception
@@ -115,10 +103,46 @@ public void testValidWithObjectWithArray() throws Exception
115103
// But also verify we can match
116104
ObjectNode match2 = root.withObject(JsonPointer.compile("/arr/2"));
117105
assertSame(match, match2);
118-
match.put("value2", true);
106+
match2.put("value2", true);
119107
assertEquals(a2q("{'arr':[null,null,{'value':42,'value2':true}]}"),
120108
root.toString());
121109

122-
// And even more! `null`s can be replaced
110+
// And even more! `null`s can be replaced by default
111+
ObjectNode match3 = root.withObject(JsonPointer.compile("/arr/0"));
112+
assertEquals("{}", match3.toString());
113+
match3.put("value", "bar");
114+
assertEquals(a2q("{'arr':[{'value':'bar'},null,{'value':42,'value2':true}]}"),
115+
root.toString());
116+
117+
// But not if prevented
118+
_verifyReplaceFail(root, "/arr/1", OverwriteMode.NONE);
119+
120+
}
121+
122+
private void _verifyReplaceFail(JsonNode doc, String ptrExpr, OverwriteMode mode) {
123+
_verifyReplaceFail(doc, JsonPointer.compile(ptrExpr), mode);
124+
}
125+
126+
private void _verifyReplaceFail(JsonNode doc, JsonPointer ptr, OverwriteMode mode) {
127+
try {
128+
if (mode == null) {
129+
// default is "NULLS":
130+
mode = OverwriteMode.NULLS;
131+
doc.withObject(ptr);
132+
} else {
133+
doc.withObject(ptr, mode, true);
134+
}
135+
fail("Should not pass");
136+
} catch (UnsupportedOperationException e) {
137+
verifyException(e, "Cannot replace `JsonNode` of type ");
138+
verifyException(e, "(mode `OverwriteMode."+mode.name()+"`)");
139+
}
123140
}
141+
142+
/*
143+
/**********************************************************************
144+
/* Test methods, withArray()
145+
/**********************************************************************
146+
*/
147+
124148
}

0 commit comments

Comments
 (0)