7
7
// for [databuind#1980] implementation
8
8
public class WithPathTest extends BaseMapTest
9
9
{
10
+ private final ObjectMapper MAPPER = sharedMapper ();
11
+
10
12
/*
11
13
/**********************************************************************
12
- /* Test methods
14
+ /* Test methods, withObject()
13
15
/**********************************************************************
14
16
*/
15
17
16
- private final ObjectMapper MAPPER = sharedMapper ();
17
-
18
18
public void testValidWithObjectTrivial () throws Exception
19
19
{
20
20
ObjectNode root = MAPPER .createObjectNode ();
@@ -77,13 +77,7 @@ public void testObjectPathWithReplace() throws Exception
77
77
root .put ("a" , 13 );
78
78
79
79
// 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 );
87
81
88
82
// Except fine via nulls (by default)
89
83
root .putNull ("a" );
@@ -93,13 +87,7 @@ public void testObjectPathWithReplace() throws Exception
93
87
94
88
// but not if prevented
95
89
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 );
103
91
}
104
92
105
93
public void testValidWithObjectWithArray () throws Exception
@@ -115,10 +103,46 @@ public void testValidWithObjectWithArray() throws Exception
115
103
// But also verify we can match
116
104
ObjectNode match2 = root .withObject (JsonPointer .compile ("/arr/2" ));
117
105
assertSame (match , match2 );
118
- match .put ("value2" , true );
106
+ match2 .put ("value2" , true );
119
107
assertEquals (a2q ("{'arr':[null,null,{'value':42,'value2':true}]}" ),
120
108
root .toString ());
121
109
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
+ }
123
140
}
141
+
142
+ /*
143
+ /**********************************************************************
144
+ /* Test methods, withArray()
145
+ /**********************************************************************
146
+ */
147
+
124
148
}
0 commit comments