mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
98% coverage
This commit is contained in:
parent
d9e04ec5b6
commit
5acbee2719
1 changed files with 107 additions and 56 deletions
163
CDLTest.java
163
CDLTest.java
|
@ -12,11 +12,7 @@ import org.json.CDL;
|
||||||
* Tests for CDL.java.
|
* Tests for CDL.java.
|
||||||
* CDL provides an application level API, but it is not used by the
|
* CDL provides an application level API, but it is not used by the
|
||||||
* reference app. To test it, strings will be converted to JSON-Java classes
|
* reference app. To test it, strings will be converted to JSON-Java classes
|
||||||
* and then converted back. Since each row is an unordered JSONObject,
|
* and then converted back.
|
||||||
* can't use a simple string compare to check for equality.
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2015-03-22
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class CDLTest {
|
public class CDLTest {
|
||||||
|
|
||||||
|
@ -25,40 +21,90 @@ public class CDLTest {
|
||||||
* and all subsequent rows are values. All keys and values should be legal.
|
* and all subsequent rows are values. All keys and values should be legal.
|
||||||
*/
|
*/
|
||||||
String lines = new String(
|
String lines = new String(
|
||||||
"Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" +
|
"Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" +
|
||||||
"val1, val2, val3, val4, val5, val6, val7\n" +
|
"val1, val2, val3, val4, val5, val6, val7\n" +
|
||||||
"1, 2, 3, 4, 5, 6, 7\n" +
|
"1, 2, 3, 4\t, 5, 6, 7\n" +
|
||||||
"true, false, true, true, false, false, false\n" +
|
"true, false, true, true, false, false, false\n" +
|
||||||
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
|
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
|
||||||
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n"
|
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n"
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Something I did not expect is that CDL.toJSONArray() adds all values as
|
* CDL.toJSONArray() adds all values asstrings, with no filtering or
|
||||||
* strings, with no filtering or conversions. I suppose this makes it
|
* conversions. For testing, this means that the expected JSONObject
|
||||||
* easier to emit it as CDL later. For testing, it means that the
|
* values all must be quoted in the cases where the JSONObject parsing
|
||||||
* expected JSONObject values all must be quoted in the cases where the
|
* might normally convert the value into a non-string.
|
||||||
* JSONObject parsing might normally convert the value into a non-string.
|
|
||||||
*/
|
*/
|
||||||
String expectedLines = new String(
|
String expectedLines = new String(
|
||||||
"[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+
|
"[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+
|
||||||
"{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+
|
"{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, "+
|
||||||
"{Col 1:true, Col 2:false, Col 3:true, Col 4:true, Col 5:false, Col 6:false, Col 7:false}, "+
|
"{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, "+
|
||||||
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+
|
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+
|
||||||
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]");
|
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]");
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected=NullPointerException.class)
|
||||||
public void shouldThrowExceptionOnNullString() {
|
public void exceptionOnNullString() {
|
||||||
|
/**
|
||||||
|
* Attempts to create a JSONArray from a null string
|
||||||
|
*/
|
||||||
String nullStr = null;
|
String nullStr = null;
|
||||||
CDL.toJSONArray(nullStr);
|
CDL.toJSONArray(nullStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected=JSONException.class)
|
||||||
|
public void unbalancedQuoteInName() {
|
||||||
|
/**
|
||||||
|
* Attempts to create a JSONArray from a string with unbalanced quotes
|
||||||
|
* in column title line
|
||||||
|
*/
|
||||||
|
String badLine = "Col1, \"Col2\nVal1, Val2";
|
||||||
|
CDL.toJSONArray(badLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=JSONException.class)
|
||||||
|
public void unbalancedQuoteInValue() {
|
||||||
|
/**
|
||||||
|
* Attempts to create a JSONArray from a string with unbalanced quotes
|
||||||
|
* in value line
|
||||||
|
*/
|
||||||
|
String badLine = "Col1, Col2\n\"Val1, Val2";
|
||||||
|
CDL.toJSONArray(badLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=JSONException.class)
|
||||||
|
public void nullInName() {
|
||||||
|
/**
|
||||||
|
* Attempts to create a JSONArray from a string with null char
|
||||||
|
* in column title line
|
||||||
|
*/
|
||||||
|
String badLine = "C\0ol1, Col2\nVal1, Val2";
|
||||||
|
CDL.toJSONArray(badLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=NullPointerException.class)
|
||||||
|
public void nullJSONArrayToString() {
|
||||||
|
/**
|
||||||
|
* call toString with a null array
|
||||||
|
*/
|
||||||
|
CDL.toString((JSONArray)null);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
/**
|
public void emptyString() {
|
||||||
* Note: This test reveals a bug in the method JavaDoc. It should
|
/**
|
||||||
* mention it might return null, or it should return an empty JSONArray.
|
* Create a JSONArray from an empty string
|
||||||
*/
|
*/
|
||||||
public void shouldHandleOnlyColumnNames() {
|
String emptyStr = "";
|
||||||
|
JSONArray jsonArray = CDL.toJSONArray(emptyStr);
|
||||||
|
assertTrue("CDL should return null when the input string is empty",
|
||||||
|
jsonArray == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onlyColumnNames() {
|
||||||
|
/**
|
||||||
|
* Create a JSONArray with only 1 row
|
||||||
|
*/
|
||||||
String columnNameStr = "col1, col2, col3";
|
String columnNameStr = "col1, col2, col3";
|
||||||
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
|
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
|
||||||
assertTrue("CDL should return null when only 1 row is given",
|
assertTrue("CDL should return null when only 1 row is given",
|
||||||
|
@ -66,38 +112,39 @@ public class CDLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
/**
|
public void emptyLinesToJSONArray() {
|
||||||
* Note: This test reveals a bug in the method JavaDoc. It should
|
/**
|
||||||
* mention it might return null, or it should return an empty JSONArray.
|
* Create a JSONArray from string containing only whitespace and commas
|
||||||
*/
|
*/
|
||||||
public void shouldHandleEmptyString() {
|
String str = " , , , \n , , , ";
|
||||||
String emptyStr = "";
|
JSONArray jsonArray = CDL.toJSONArray(str);
|
||||||
JSONArray jsonArray = CDL.toJSONArray(emptyStr);
|
assertTrue("JSONArray should be null for no content",
|
||||||
assertTrue("CDL should return null when the input string is empty",
|
|
||||||
jsonArray == null);
|
jsonArray == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
|
||||||
public void shouldHandleUnbalancedQuoteInName() {
|
|
||||||
String badLine = "Col1, \"Col2\nVal1, Val2";
|
|
||||||
CDL.toJSONArray(badLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
|
||||||
public void shouldHandleUnbalancedQuoteInValue() {
|
|
||||||
String badLine = "Col1, Col2\n\"Val1, Val2";
|
|
||||||
CDL.toJSONArray(badLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
|
||||||
public void shouldHandleNullInName() {
|
|
||||||
String badLine = "C\0ol1, Col2\nVal1, Val2";
|
|
||||||
CDL.toJSONArray(badLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toStringShouldCheckSpecialChars() {
|
public void emptyJSONArrayToString() {
|
||||||
|
/**
|
||||||
|
* call toString with a null array
|
||||||
|
*/
|
||||||
|
JSONArray jsonArray = new JSONArray();
|
||||||
|
String str = CDL.toString(jsonArray);
|
||||||
|
assertTrue("CDL should return null for toString(null)",
|
||||||
|
str == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nullJSONArraysToString() {
|
||||||
|
/**
|
||||||
|
* call toString with a null arrays for names and values
|
||||||
|
*/
|
||||||
|
String str = CDL.toString(null, null);
|
||||||
|
assertTrue("CDL should return null for toString(null)",
|
||||||
|
str == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkSpecialChars() {
|
||||||
/**
|
/**
|
||||||
* Given a JSONArray that was not built by CDL, some chars may be
|
* Given a JSONArray that was not built by CDL, some chars may be
|
||||||
* found that would otherwise be filtered out by CDL.
|
* found that would otherwise be filtered out by CDL.
|
||||||
|
@ -119,16 +166,21 @@ public class CDLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConvertCDLToJSONArray() {
|
public void textToJSONArray() {
|
||||||
// this array is built by CDL
|
/**
|
||||||
|
* Create a JSONArray from a string of lines
|
||||||
|
*/
|
||||||
JSONArray jsonArray = CDL.toJSONArray(lines);
|
JSONArray jsonArray = CDL.toJSONArray(lines);
|
||||||
// This array is built from JSON parsing
|
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedLines);
|
JSONArray expectedJsonArray = new JSONArray(expectedLines);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateJSONArrayUsingJSONArray() {
|
public void jsonArrayToJSONArray() {
|
||||||
|
/**
|
||||||
|
* Create a JSONArray from a JSONArray of titles and a
|
||||||
|
* string of value lines
|
||||||
|
*/
|
||||||
String nameArrayStr = "[Col1, Col2]";
|
String nameArrayStr = "[Col1, Col2]";
|
||||||
String values = "V1, V2";
|
String values = "V1, V2";
|
||||||
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
||||||
|
@ -138,11 +190,10 @@ public class CDLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConvertCDLToJSONArrayAndBackToString() {
|
public void textToJSONArrayAndBackToString() {
|
||||||
/**
|
/**
|
||||||
* This is the first test of normal functionality.
|
* Create a JSONArray from a string of lines,
|
||||||
* The string contains a typical variety of values
|
* then convert to string and then back to JSONArray
|
||||||
* that might be found in a real CDL.
|
|
||||||
*/
|
*/
|
||||||
JSONArray jsonArray = CDL.toJSONArray(lines);
|
JSONArray jsonArray = CDL.toJSONArray(lines);
|
||||||
String jsonStr = CDL.toString(jsonArray);
|
String jsonStr = CDL.toString(jsonArray);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue