|
1 | 1 | package org.json.junit;
|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals;
|
| 4 | +import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertNotEquals;
|
| 6 | +import static org.junit.Assert.assertNotNull; |
5 | 7 | import static org.junit.Assert.assertNull;
|
6 | 8 | import static org.junit.Assert.assertTrue;
|
7 | 9 | import static org.junit.Assert.fail;
|
@@ -613,6 +615,67 @@ public void updateListener2Test() {
|
613 | 615 | assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
|
614 | 616 | }
|
615 | 617 |
|
| 618 | + @Test |
| 619 | + public void updateChildTest() { |
| 620 | + final JSONObject jsonParent = new JSONObject(); |
| 621 | + final JSONObject jsonChild = new JSONObject(); |
| 622 | + |
| 623 | + jsonParent.onUpdateGlobal(evt -> { |
| 624 | + final Object oldValue = evt.getOldValue(); |
| 625 | + final Object newValue = evt.getNewValue(); |
| 626 | + |
| 627 | + assertNotEquals(oldValue.toString(), newValue.toString()); |
| 628 | + |
| 629 | + assertTrue(oldValue instanceof JSONObject); |
| 630 | + assertTrue(newValue instanceof JSONObject); |
| 631 | + |
| 632 | + final JSONObject oldValueJson = (JSONObject) oldValue; |
| 633 | + final JSONObject newValueJson = (JSONObject) newValue; |
| 634 | + |
| 635 | + if (oldValueJson.has("jsonChild")) { |
| 636 | + var oldValueJsonChild = oldValueJson.optJSONObject("jsonChild"); |
| 637 | + var newValueJsonChild = newValueJson.optJSONObject("jsonChild"); |
| 638 | + |
| 639 | + assertNotEquals(oldValueJsonChild.toString(), newValueJsonChild.toString()); |
| 640 | + assertNotEquals(oldValueJson.toString(), newValueJson.toString()); |
| 641 | + |
| 642 | + assertFalse(newValueJsonChild.has("test4")); |
| 643 | + assertTrue(newValueJsonChild.isNull("test4")); |
| 644 | + assertFalse(newValueJsonChild.has("test5")); |
| 645 | + assertTrue(newValueJsonChild.isNull("test5")); |
| 646 | + |
| 647 | + if (newValueJsonChild.has("test3")) { |
| 648 | + final JSONObject test3 = newValueJsonChild.getJSONObject("test3"); |
| 649 | + if (test3.has("test4")) { |
| 650 | + assertEquals(test3.optString("test4"), "value4"); |
| 651 | + } |
| 652 | + |
| 653 | + if (test3.has("test5")) { |
| 654 | + assertEquals(test3.optString("test5"), "value5"); |
| 655 | + } |
| 656 | + } else if (newValueJsonChild.has("test2")) { |
| 657 | + assertNull(oldValueJsonChild.optJSONObject("test2")); |
| 658 | + assertNotNull(newValueJsonChild.optJSONObject("test2")); |
| 659 | + } else { |
| 660 | + assertTrue(oldValueJsonChild.isNull("test1")); |
| 661 | + assertFalse(oldValueJsonChild.optBoolean("test1")); |
| 662 | + |
| 663 | + assertFalse(newValueJsonChild.isNull("test1")); |
| 664 | + assertTrue(newValueJsonChild.optBoolean("test1")); |
| 665 | + } |
| 666 | + } |
| 667 | + }); |
| 668 | + |
| 669 | + jsonParent.update("jsonChild", jsonChild); |
| 670 | + |
| 671 | + jsonChild.update("test1", true); |
| 672 | + jsonChild.update("test2", new JSONObject()); |
| 673 | + final JSONObject test3 = new JSONObject(); |
| 674 | + test3.update("test4", "value4"); |
| 675 | + jsonChild.update("test3", test3); |
| 676 | + test3.update("test5", "value5"); |
| 677 | + } |
| 678 | + |
616 | 679 | @Test
|
617 | 680 | public void updateOrRemoveSrcEmptyTest() {
|
618 | 681 | try {
|
|
0 commit comments