|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertNull; |
4 | 4 | import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.Assert.assertEquals; |
5 | 6 |
|
| 7 | +import java.io.StringWriter; |
| 8 | +import java.io.Writer; |
6 | 9 | import java.math.BigDecimal; |
7 | 10 | import java.math.BigInteger; |
8 | 11 | import java.util.ArrayList; |
@@ -639,6 +642,71 @@ public void notSimilar() { |
639 | 642 | !jsonArray.similar(otherJsonArray)); |
640 | 643 | } |
641 | 644 |
|
| 645 | + /** |
| 646 | + * Exercise JSONArray toString() method with various indent levels. |
| 647 | + */ |
| 648 | + @Test |
| 649 | + public void jsonArrayToStringIndent() { |
| 650 | + String jsonArray0Str = |
| 651 | + "[" + |
| 652 | + "[1,2," + |
| 653 | + "{\"key3\":true}" + |
| 654 | + "]," + |
| 655 | + "{\"key1\":\"val1\",\"key2\":" + |
| 656 | + "{\"key2\":\"val2\"}" + |
| 657 | + "}," + |
| 658 | + "[" + |
| 659 | + "[1,2.1]" + |
| 660 | + "," + |
| 661 | + "[null]" + |
| 662 | + "]" + |
| 663 | + "]"; |
| 664 | + |
| 665 | + String jsonArray1Str = |
| 666 | + "[\n" + |
| 667 | + " [\n" + |
| 668 | + " 1,\n" + |
| 669 | + " 2,\n" + |
| 670 | + " {\"key3\": true}\n" + |
| 671 | + " ],\n" + |
| 672 | + " {\n" + |
| 673 | + " \"key1\": \"val1\",\n" + |
| 674 | + " \"key2\": {\"key2\": \"val2\"}\n" + |
| 675 | + " },\n" + |
| 676 | + " [\n" + |
| 677 | + " [\n" + |
| 678 | + " 1,\n" + |
| 679 | + " 2.1\n" + |
| 680 | + " ],\n" + |
| 681 | + " [null]\n" + |
| 682 | + " ]\n" + |
| 683 | + "]"; |
| 684 | + String jsonArray4Str = |
| 685 | + "[\n" + |
| 686 | + " [\n" + |
| 687 | + " 1,\n" + |
| 688 | + " 2,\n" + |
| 689 | + " {\"key3\": true}\n" + |
| 690 | + " ],\n" + |
| 691 | + " {\n" + |
| 692 | + " \"key1\": \"val1\",\n" + |
| 693 | + " \"key2\": {\"key2\": \"val2\"}\n" + |
| 694 | + " },\n" + |
| 695 | + " [\n" + |
| 696 | + " [\n" + |
| 697 | + " 1,\n" + |
| 698 | + " 2.1\n" + |
| 699 | + " ],\n" + |
| 700 | + " [null]\n" + |
| 701 | + " ]\n" + |
| 702 | + "]"; |
| 703 | + JSONArray jsonArray = new JSONArray(jsonArray0Str); |
| 704 | + assertEquals(jsonArray0Str, jsonArray.toString()); |
| 705 | + assertEquals(jsonArray0Str, jsonArray.toString(0)); |
| 706 | + assertEquals(jsonArray1Str, jsonArray.toString(1)); |
| 707 | + assertEquals(jsonArray4Str, jsonArray.toString(4)); |
| 708 | + } |
| 709 | + |
642 | 710 | /** |
643 | 711 | * Convert an empty JSONArray to JSONObject |
644 | 712 | */ |
@@ -726,4 +794,131 @@ public void optQueryWithNoResult() { |
726 | 794 | public void optQueryWithSyntaxError() { |
727 | 795 | new JSONArray().optQuery("invalid"); |
728 | 796 | } |
| 797 | + |
| 798 | + |
| 799 | + /** |
| 800 | + * Exercise the JSONArray write() method |
| 801 | + */ |
| 802 | + @Test |
| 803 | + public void write() { |
| 804 | + String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; |
| 805 | + String expectedStr = str; |
| 806 | + JSONArray jsonArray = new JSONArray(str); |
| 807 | + StringWriter stringWriter = new StringWriter(); |
| 808 | + Writer writer = jsonArray.write(stringWriter); |
| 809 | + String actualStr = writer.toString(); |
| 810 | + assertTrue("write() expected " + expectedStr + |
| 811 | + "but found " + actualStr, |
| 812 | + expectedStr.equals(actualStr)); |
| 813 | + StringBuilder stringBuilder = new StringBuilder(); |
| 814 | + Appendable appendable = jsonArray.write(stringBuilder); |
| 815 | + actualStr = appendable.toString(); |
| 816 | + assertTrue("write() expected " + expectedStr + |
| 817 | + "but found " + actualStr, |
| 818 | + expectedStr.equals(actualStr)); |
| 819 | + } |
| 820 | + |
| 821 | + /** |
| 822 | + * Exercise the JSONArray write(Appendable, int, int) method |
| 823 | + */ |
| 824 | + @Test |
| 825 | + public void write3Param() { |
| 826 | + String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; |
| 827 | + String str2 = |
| 828 | + "[\n" + |
| 829 | + " \"value1\",\n" + |
| 830 | + " \"value2\",\n" + |
| 831 | + " {\n" + |
| 832 | + " \"key1\": 1,\n" + |
| 833 | + " \"key2\": false,\n" + |
| 834 | + " \"key3\": 3.14\n" + |
| 835 | + " }\n" + |
| 836 | + " ]"; |
| 837 | + String expectedStr = str0; |
| 838 | + JSONArray jsonArray = new JSONArray(str0); |
| 839 | + StringWriter stringWriter = new StringWriter(); |
| 840 | + Writer writer = jsonArray.write(stringWriter, 0, 0); |
| 841 | + String actualStr = writer.toString(); |
| 842 | + assertEquals(expectedStr, actualStr); |
| 843 | + expectedStr = str0; |
| 844 | + StringBuilder stringBuilder = new StringBuilder(); |
| 845 | + Appendable appendable = jsonArray.write(stringBuilder, 0, 0); |
| 846 | + actualStr = appendable.toString(); |
| 847 | + assertEquals(expectedStr, actualStr); |
| 848 | + expectedStr = str2; |
| 849 | + stringBuilder = new StringBuilder(); |
| 850 | + appendable = jsonArray.write(stringBuilder, 2, 1); |
| 851 | + actualStr = appendable.toString(); |
| 852 | + assertEquals(expectedStr, actualStr); |
| 853 | + } |
| 854 | + |
| 855 | + /** |
| 856 | + * Exercise JSONArray toString() method with various indent levels. |
| 857 | + */ |
| 858 | + @Test |
| 859 | + public void toList() { |
| 860 | + String jsonArrayStr = |
| 861 | + "[" + |
| 862 | + "[1,2," + |
| 863 | + "{\"key3\":true}" + |
| 864 | + "]," + |
| 865 | + "{\"key1\":\"val1\",\"key2\":" + |
| 866 | + "{\"key2\":null}," + |
| 867 | + "\"key3\":42,\"key4\":[]" + |
| 868 | + "}," + |
| 869 | + "[" + |
| 870 | + "[\"value1\",2.1]" + |
| 871 | + "," + |
| 872 | + "[null]" + |
| 873 | + "]" + |
| 874 | + "]"; |
| 875 | + |
| 876 | + JSONArray jsonArray = new JSONArray(jsonArrayStr); |
| 877 | + List list = jsonArray.toList(); |
| 878 | + |
| 879 | + assertTrue("List should not be null", list != null); |
| 880 | + assertTrue("List should have 3 elements", list.size() == 3); |
| 881 | + |
| 882 | + List val1List = (List) list.get(0); |
| 883 | + assertTrue("val1 should not be null", val1List != null); |
| 884 | + assertTrue("val1 should have 3 elements", val1List.size() == 3); |
| 885 | + |
| 886 | + assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1))); |
| 887 | + assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2))); |
| 888 | + |
| 889 | + Map key1Value3Map = (Map)val1List.get(2); |
| 890 | + assertTrue("Map should not be null", key1Value3Map != null); |
| 891 | + assertTrue("Map should have 1 element", key1Value3Map.size() == 1); |
| 892 | + assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); |
| 893 | + |
| 894 | + Map val2Map = (Map) list.get(1); |
| 895 | + assertTrue("val2 should not be null", val2Map != null); |
| 896 | + assertTrue("val2 should have 4 elements", val2Map.size() == 4); |
| 897 | + assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1")); |
| 898 | + assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42))); |
| 899 | + |
| 900 | + Map val2Key2Map = (Map)val2Map.get("key2"); |
| 901 | + assertTrue("val2 map key 2 should not be null", val2Key2Map != null); |
| 902 | + assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2")); |
| 903 | + assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null); |
| 904 | + |
| 905 | + List val2Key4List = (List)val2Map.get("key4"); |
| 906 | + assertTrue("val2 map key 4 should not be null", val2Key4List != null); |
| 907 | + assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty()); |
| 908 | + |
| 909 | + List val3List = (List) list.get(2); |
| 910 | + assertTrue("val3 should not be null", val3List != null); |
| 911 | + assertTrue("val3 should have 2 elements", val3List.size() == 2); |
| 912 | + |
| 913 | + List val3Val1List = (List)val3List.get(0); |
| 914 | + assertTrue("val3 list val 1 should not be null", val3Val1List != null); |
| 915 | + assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2); |
| 916 | + assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1")); |
| 917 | + assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1"))); |
| 918 | + |
| 919 | + List val3Val2List = (List)val3List.get(1); |
| 920 | + assertTrue("val3 list val 2 should not be null", val3Val2List != null); |
| 921 | + assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1); |
| 922 | + assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null); |
| 923 | + } |
729 | 924 | } |
0 commit comments