Skip to content

Commit 1246e12

Browse files
committed
Factor out Writer from Appendable tests.
1 parent ffcfa66 commit 1246e12

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,24 +802,36 @@ public void optQueryWithSyntaxError() {
802802
@Test
803803
public void write() {
804804
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
805-
String expectedStr = str;
806805
JSONArray jsonArray = new JSONArray(str);
806+
String expectedStr = str;
807807
StringWriter stringWriter = new StringWriter();
808808
Writer writer = jsonArray.write(stringWriter);
809809
String actualStr = writer.toString();
810810
assertTrue("write() expected " + expectedStr +
811-
"but found " + actualStr,
811+
" but found " + actualStr,
812812
expectedStr.equals(actualStr));
813+
}
814+
815+
/**
816+
* Exercise the JSONArray write() method using Appendable.
817+
*/
818+
/*
819+
@Test
820+
public void writeAppendable() {
821+
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
822+
JSONArray jsonArray = new JSONArray(str);
823+
String expectedStr = str;
813824
StringBuilder stringBuilder = new StringBuilder();
814825
Appendable appendable = jsonArray.write(stringBuilder);
815-
actualStr = appendable.toString();
826+
String actualStr = appendable.toString();
816827
assertTrue("write() expected " + expectedStr +
817-
"but found " + actualStr,
828+
" but found " + actualStr,
818829
expectedStr.equals(actualStr));
819830
}
831+
*/
820832

821833
/**
822-
* Exercise the JSONArray write(Appendable, int, int) method
834+
* Exercise the JSONArray write(Writer, int, int) method
823835
*/
824836
@Test
825837
public void write3Param() {
@@ -834,23 +846,51 @@ public void write3Param() {
834846
" \"key3\": 3.14\n" +
835847
" }\n" +
836848
" ]";
837-
String expectedStr = str0;
838849
JSONArray jsonArray = new JSONArray(str0);
850+
String expectedStr = str0;
839851
StringWriter stringWriter = new StringWriter();
840852
Writer writer = jsonArray.write(stringWriter, 0, 0);
841853
String actualStr = writer.toString();
842854
assertEquals(expectedStr, actualStr);
843-
expectedStr = str0;
855+
856+
expectedStr = str2;
857+
stringWriter = new StringWriter();
858+
writer = jsonArray.write(stringWriter, 2, 1);
859+
actualStr = writer.toString();
860+
assertEquals(expectedStr, actualStr);
861+
}
862+
863+
/**
864+
* Exercise the JSONArray write(Appendable, int, int) method
865+
*/
866+
/*
867+
@Test
868+
public void write3ParamAppendable() {
869+
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
870+
String str2 =
871+
"[\n" +
872+
" \"value1\",\n" +
873+
" \"value2\",\n" +
874+
" {\n" +
875+
" \"key1\": 1,\n" +
876+
" \"key2\": false,\n" +
877+
" \"key3\": 3.14\n" +
878+
" }\n" +
879+
" ]";
880+
JSONArray jsonArray = new JSONArray(str0);
881+
String expectedStr = str0;
844882
StringBuilder stringBuilder = new StringBuilder();
845883
Appendable appendable = jsonArray.write(stringBuilder, 0, 0);
846-
actualStr = appendable.toString();
884+
String actualStr = appendable.toString();
847885
assertEquals(expectedStr, actualStr);
886+
848887
expectedStr = str2;
849888
stringBuilder = new StringBuilder();
850889
appendable = jsonArray.write(stringBuilder, 2, 1);
851890
actualStr = appendable.toString();
852891
assertEquals(expectedStr, actualStr);
853892
}
893+
*/
854894

855895
/**
856896
* Exercise JSONArray toString() method with various indent levels.

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,18 +1916,30 @@ public void write() {
19161916
Writer writer = jsonObject.write(stringWriter);
19171917
String actualStr = writer.toString();
19181918
assertTrue("write() expected " +expectedStr+
1919-
"but found " +actualStr,
1919+
" but found " +actualStr,
19201920
expectedStr.equals(actualStr));
1921+
}
1922+
1923+
/**
1924+
* Exercise the JSONObject write() method
1925+
*/
1926+
/*
1927+
@Test
1928+
public void writeAppendable() {
1929+
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
1930+
String expectedStr = str;
1931+
JSONObject jsonObject = new JSONObject(str);
19211932
StringBuilder stringBuilder = new StringBuilder();
19221933
Appendable appendable = jsonObject.write(stringBuilder);
1923-
actualStr = appendable.toString();
1934+
String actualStr = appendable.toString();
19241935
assertTrue("write() expected " +expectedStr+
1925-
"but found " +actualStr,
1936+
" but found " +actualStr,
19261937
expectedStr.equals(actualStr));
19271938
}
1939+
*/
19281940

19291941
/**
1930-
* Exercise the JSONObject write(Appendable, int, int) method
1942+
* Exercise the JSONObject write(Writer, int, int) method
19311943
*/
19321944
@Test
19331945
public void write3Param() {
@@ -1941,23 +1953,50 @@ public void write3Param() {
19411953
" 3.14\n" +
19421954
" ]\n" +
19431955
" }";
1944-
String expectedStr = str0;
19451956
JSONObject jsonObject = new JSONObject(str0);
1957+
String expectedStr = str0;
19461958
StringWriter stringWriter = new StringWriter();
19471959
Writer writer = jsonObject.write(stringWriter,0,0);
19481960
String actualStr = writer.toString();
19491961
assertEquals(expectedStr, actualStr);
1950-
expectedStr = str0;
1962+
1963+
expectedStr = str2;
1964+
stringWriter = new StringWriter();
1965+
writer = jsonObject.write(stringWriter,2,1);
1966+
actualStr = writer.toString();
1967+
assertEquals(expectedStr, actualStr);
1968+
}
1969+
1970+
/**
1971+
* Exercise the JSONObject write(Appendable, int, int) method
1972+
*/
1973+
/*
1974+
@Test
1975+
public void write3ParamAppendable() {
1976+
String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}";
1977+
String str2 =
1978+
"{\n" +
1979+
" \"key1\": \"value1\",\n" +
1980+
" \"key2\": [\n" +
1981+
" 1,\n" +
1982+
" false,\n" +
1983+
" 3.14\n" +
1984+
" ]\n" +
1985+
" }";
1986+
JSONObject jsonObject = new JSONObject(str0);
1987+
String expectedStr = str0;
19511988
StringBuilder stringBuilder = new StringBuilder();
19521989
Appendable appendable = jsonObject.write(stringBuilder,0,0);
1953-
actualStr = appendable.toString();
1990+
String actualStr = appendable.toString();
19541991
assertEquals(expectedStr, actualStr);
1992+
19551993
expectedStr = str2;
19561994
stringBuilder = new StringBuilder();
19571995
appendable = jsonObject.write(stringBuilder,2,1);
19581996
actualStr = appendable.toString();
19591997
assertEquals(expectedStr, actualStr);
19601998
}
1999+
*/
19612000

19622001
/**
19632002
* Exercise the JSONObject equals() method

0 commit comments

Comments
 (0)