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

cdltest refactor, in progress

This commit is contained in:
stleary 2015-03-20 07:34:03 -05:00
parent fd56452f6b
commit b5b4961a9d

View file

@ -41,6 +41,12 @@ public class CDLTest {
"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"
); );
String jsonArrayLines = 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: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:0.23, Col 2:57.42, Col 3:-234.879, Col 4:2.34e5, Col 5:2.34e5, Col 6:0.0, Col 7:9e-3}, "+
"{Col 1:\"val1\", Col 2:\"val2\", Col 3:val3, Col 4\"val4\", Col 5:val5, Col 6:val6, Col 7:val7}]");
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void shouldThrowExceptionOnNullString() { public void shouldThrowExceptionOnNullString() {
@ -48,6 +54,24 @@ public class CDLTest {
CDL.toJSONArray(nullStr); CDL.toJSONArray(nullStr);
} }
@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
/** /**
* Note: This test reveals a bug in the method JavaDoc. It should * Note: This test reveals a bug in the method JavaDoc. It should
@ -74,6 +98,7 @@ public class CDLTest {
@Test @Test
public void toStringShouldCheckSpecialChars() { public void toStringShouldCheckSpecialChars() {
String method = "toStringShouldCheckSpecialChars():";
/** /**
* 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.
@ -81,49 +106,24 @@ public class CDLTest {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonArray.put(jsonObject); jsonArray.put(jsonObject);
// \r will be filtered from name // \r should be filtered from name
jsonObject.put("Col \r1", "V1"); jsonObject.put("Col \r1", "V1");
// \r will be filtered from value // \r should be filtered from value
jsonObject.put("Col 2", "V2\r"); jsonObject.put("Col 2", "V2\r");
boolean normalize = true; String jsonStr = CDL.toString(jsonArray);
List<List<String>> expectedLines =
sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize);
List<List<String>> jsonArrayLines =
sortColumnsInLines(CDL.toString(jsonArray), normalize);
if (!expectedLines.equals(jsonArrayLines)) {
System.out.println("expected: " +expectedLines);
System.out.println("jsonArray: " +jsonArrayLines);
assertTrue("Should filter out certain chars",
false);
}
}
@Test(expected=JSONException.class) String[] expectedStr = {"\"Col 1\"","Col 2","V1","\"V2\""};
public void shouldHandleUnbalancedQuoteInName() { Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method);
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 shouldConvertCDLToJSONArray() { public void shouldConvertCDLToJSONArray() {
String method = "shouldConvertCDLToJSONArray(): ";
JSONArray jsonArray = CDL.toJSONArray(lines); JSONArray jsonArray = CDL.toJSONArray(lines);
String resultStr = compareJSONArrayToString(jsonArray, lines); String jsonStr = CDL.toString(jsonArray);
if (resultStr != null) { JSONArray expectedJsonArray = new JSONArray(jsonArrayLines);
assertTrue("CDL should convert string to JSONArray: " + assertTrue("CDL should convert string to JSONArray",
resultStr, false); jsonArray.equals(expectedJsonArray));
}
} }
@Test @Test