1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Factor out Writer from Appendable tests.

This commit is contained in:
Nicholas Cull 2016-07-24 19:39:52 +10:00
parent ffcfa66d77
commit 1246e12827
2 changed files with 102 additions and 23 deletions

View file

@ -802,24 +802,36 @@ public class JSONArrayTest {
@Test @Test
public void write() { public void write() {
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
String expectedStr = str;
JSONArray jsonArray = new JSONArray(str); JSONArray jsonArray = new JSONArray(str);
String expectedStr = str;
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Writer writer = jsonArray.write(stringWriter); Writer writer = jsonArray.write(stringWriter);
String actualStr = writer.toString(); String actualStr = writer.toString();
assertTrue("write() expected " + expectedStr + assertTrue("write() expected " + expectedStr +
"but found " + actualStr, " but found " + actualStr,
expectedStr.equals(actualStr));
StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonArray.write(stringBuilder);
actualStr = appendable.toString();
assertTrue("write() expected " + expectedStr +
"but found " + actualStr,
expectedStr.equals(actualStr)); expectedStr.equals(actualStr));
} }
/** /**
* Exercise the JSONArray write(Appendable, int, int) method * Exercise the JSONArray write() method using Appendable.
*/
/*
@Test
public void writeAppendable() {
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
JSONArray jsonArray = new JSONArray(str);
String expectedStr = str;
StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonArray.write(stringBuilder);
String actualStr = appendable.toString();
assertTrue("write() expected " + expectedStr +
" but found " + actualStr,
expectedStr.equals(actualStr));
}
*/
/**
* Exercise the JSONArray write(Writer, int, int) method
*/ */
@Test @Test
public void write3Param() { public void write3Param() {
@ -834,23 +846,51 @@ public class JSONArrayTest {
" \"key3\": 3.14\n" + " \"key3\": 3.14\n" +
" }\n" + " }\n" +
" ]"; " ]";
String expectedStr = str0;
JSONArray jsonArray = new JSONArray(str0); JSONArray jsonArray = new JSONArray(str0);
String expectedStr = str0;
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Writer writer = jsonArray.write(stringWriter, 0, 0); Writer writer = jsonArray.write(stringWriter, 0, 0);
String actualStr = writer.toString(); String actualStr = writer.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
expectedStr = str0;
expectedStr = str2;
stringWriter = new StringWriter();
writer = jsonArray.write(stringWriter, 2, 1);
actualStr = writer.toString();
assertEquals(expectedStr, actualStr);
}
/**
* Exercise the JSONArray write(Appendable, int, int) method
*/
/*
@Test
public void write3ParamAppendable() {
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
String str2 =
"[\n" +
" \"value1\",\n" +
" \"value2\",\n" +
" {\n" +
" \"key1\": 1,\n" +
" \"key2\": false,\n" +
" \"key3\": 3.14\n" +
" }\n" +
" ]";
JSONArray jsonArray = new JSONArray(str0);
String expectedStr = str0;
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonArray.write(stringBuilder, 0, 0); Appendable appendable = jsonArray.write(stringBuilder, 0, 0);
actualStr = appendable.toString(); String actualStr = appendable.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
expectedStr = str2; expectedStr = str2;
stringBuilder = new StringBuilder(); stringBuilder = new StringBuilder();
appendable = jsonArray.write(stringBuilder, 2, 1); appendable = jsonArray.write(stringBuilder, 2, 1);
actualStr = appendable.toString(); actualStr = appendable.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
} }
*/
/** /**
* Exercise JSONArray toString() method with various indent levels. * Exercise JSONArray toString() method with various indent levels.

View file

@ -1916,18 +1916,30 @@ public class JSONObjectTest {
Writer writer = jsonObject.write(stringWriter); Writer writer = jsonObject.write(stringWriter);
String actualStr = writer.toString(); String actualStr = writer.toString();
assertTrue("write() expected " +expectedStr+ assertTrue("write() expected " +expectedStr+
"but found " +actualStr, " but found " +actualStr,
expectedStr.equals(actualStr));
StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonObject.write(stringBuilder);
actualStr = appendable.toString();
assertTrue("write() expected " +expectedStr+
"but found " +actualStr,
expectedStr.equals(actualStr)); expectedStr.equals(actualStr));
} }
/** /**
* Exercise the JSONObject write(Appendable, int, int) method * Exercise the JSONObject write() method
*/
/*
@Test
public void writeAppendable() {
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
String expectedStr = str;
JSONObject jsonObject = new JSONObject(str);
StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonObject.write(stringBuilder);
String actualStr = appendable.toString();
assertTrue("write() expected " +expectedStr+
" but found " +actualStr,
expectedStr.equals(actualStr));
}
*/
/**
* Exercise the JSONObject write(Writer, int, int) method
*/ */
@Test @Test
public void write3Param() { public void write3Param() {
@ -1941,23 +1953,50 @@ public class JSONObjectTest {
" 3.14\n" + " 3.14\n" +
" ]\n" + " ]\n" +
" }"; " }";
String expectedStr = str0;
JSONObject jsonObject = new JSONObject(str0); JSONObject jsonObject = new JSONObject(str0);
String expectedStr = str0;
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Writer writer = jsonObject.write(stringWriter,0,0); Writer writer = jsonObject.write(stringWriter,0,0);
String actualStr = writer.toString(); String actualStr = writer.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
expectedStr = str0;
expectedStr = str2;
stringWriter = new StringWriter();
writer = jsonObject.write(stringWriter,2,1);
actualStr = writer.toString();
assertEquals(expectedStr, actualStr);
}
/**
* Exercise the JSONObject write(Appendable, int, int) method
*/
/*
@Test
public void write3ParamAppendable() {
String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}";
String str2 =
"{\n" +
" \"key1\": \"value1\",\n" +
" \"key2\": [\n" +
" 1,\n" +
" false,\n" +
" 3.14\n" +
" ]\n" +
" }";
JSONObject jsonObject = new JSONObject(str0);
String expectedStr = str0;
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
Appendable appendable = jsonObject.write(stringBuilder,0,0); Appendable appendable = jsonObject.write(stringBuilder,0,0);
actualStr = appendable.toString(); String actualStr = appendable.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
expectedStr = str2; expectedStr = str2;
stringBuilder = new StringBuilder(); stringBuilder = new StringBuilder();
appendable = jsonObject.write(stringBuilder,2,1); appendable = jsonObject.write(stringBuilder,2,1);
actualStr = appendable.toString(); actualStr = appendable.toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
} }
*/
/** /**
* Exercise the JSONObject equals() method * Exercise the JSONObject equals() method