@@ -196,6 +196,47 @@ private void _verifyObjectReplaceFail(JsonNode doc, JsonPointer ptr, OverwriteMo
196
196
}
197
197
}
198
198
199
+ /*
200
+ /**********************************************************************
201
+ /* Test methods, withObjectProperty()
202
+ /**********************************************************************
203
+ */
204
+
205
+ public void testWithObjectProperty () throws Exception
206
+ {
207
+ ObjectNode root = MAPPER .createObjectNode ();
208
+
209
+ // First: create new property value
210
+ ObjectNode match = root .withObjectProperty ("a" );
211
+ assertTrue (match .isObject ());
212
+ assertEquals (a2q ("{}" ), match .toString ());
213
+ match .put ("value" , 42 );
214
+ assertEquals (a2q ("{'a':{'value':42}}" ), root .toString ());
215
+
216
+ // Second: match existing Object property
217
+ ObjectNode match2 = root .withObjectProperty ("a" );
218
+ assertSame (match , match2 );
219
+ match .put ("value2" , true );
220
+
221
+ assertEquals (a2q ("{'a':{'value':42,'value2':true}}" ),
222
+ root .toString ());
223
+
224
+ // Third: match and overwrite existing null node
225
+ JsonNode root2 = MAPPER .readTree ("{\" b\" : null}" );
226
+ ObjectNode match3 = root2 .withObjectProperty ("b" );
227
+ assertNotSame (match , match3 );
228
+ assertEquals ("{\" b\" :{}}" , root2 .toString ());
229
+
230
+ // and then failing case
231
+ JsonNode root3 = MAPPER .readTree ("{\" c\" : 123}" );
232
+ try {
233
+ root3 .withObjectProperty ("c" );
234
+ fail ("Should not pass" );
235
+ } catch (UnsupportedOperationException e ) {
236
+ verifyException (e , "Cannot replace `JsonNode` of type " );
237
+ }
238
+ }
239
+
199
240
/*
200
241
/**********************************************************************
201
242
/* Test methods, withArray()
@@ -315,4 +356,44 @@ public void testWithArray3882() throws Exception
315
356
assertEquals (a2q ("{'key1':{'array1':[{'element1':['v1']}]}}" ),
316
357
root .toString ());
317
358
}
359
+
360
+ /*
361
+ /**********************************************************************
362
+ /* Test methods, withArrayProperty()
363
+ /**********************************************************************
364
+ */
365
+
366
+ public void testWithArrayProperty () throws Exception
367
+ {
368
+ ObjectNode root = MAPPER .createObjectNode ();
369
+
370
+ // First: create new property value
371
+ ArrayNode match = root .withArrayProperty ("a" );
372
+ assertTrue (match .isArray ());
373
+ assertEquals (a2q ("[]" ), match .toString ());
374
+ match .add (42 );
375
+ assertEquals (a2q ("{'a':[42]}" ), root .toString ());
376
+
377
+ // Second: match existing Object property
378
+ ArrayNode match2 = root .withArrayProperty ("a" );
379
+ assertSame (match , match2 );
380
+ match .add (true );
381
+
382
+ assertEquals (a2q ("{'a':[42,true]}" ), root .toString ());
383
+
384
+ // Third: match and overwrite existing null node
385
+ JsonNode root2 = MAPPER .readTree ("{\" b\" : null}" );
386
+ ArrayNode match3 = root2 .withArrayProperty ("b" );
387
+ assertNotSame (match , match3 );
388
+ assertEquals ("{\" b\" :[]}" , root2 .toString ());
389
+
390
+ // and then failing case
391
+ JsonNode root3 = MAPPER .readTree ("{\" c\" : 123}" );
392
+ try {
393
+ root3 .withArrayProperty ("c" );
394
+ fail ("Should not pass" );
395
+ } catch (UnsupportedOperationException e ) {
396
+ verifyException (e , "Cannot replace `JsonNode` of type " );
397
+ }
398
+ }
318
399
}
0 commit comments