diff --git a/CDLTest.java b/CDLTest.java index f79f2f0..d5ead3c 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -42,47 +42,18 @@ public class CDLTest { "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); - /** - * If lines were to be converted to a JSONArray without the benefit of CDL, - * this would be the input string - */ - 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) public void shouldThrowExceptionOnNullString() { String nullStr = null; CDL.toJSONArray(nullStr); } - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnUnbalancedQuoteInName() { - String badLine = "Col1, \"Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); - } - - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnUnbalancedQuoteInValue() { - String badLine = "Col1, Col2\n\"Val1, Val2"; - CDL.toJSONArray(badLine); - } - - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnNullInName() { - String badLine = "C\0ol1, Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); - } - @Test /** * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldReturnNullOnOnlyColumnNames() { + public void shouldHandleOnlyColumnNames() { String columnNameStr = "col1, col2, col3"; JSONArray jsonArray = CDL.toJSONArray(columnNameStr); assertTrue("CDL should return null when only 1 row is given", @@ -94,36 +65,15 @@ public class CDLTest { * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldReturnNullOnEmptyString() { + public void shouldHandleEmptyString() { String emptyStr = ""; JSONArray jsonArray = CDL.toJSONArray(emptyStr); assertTrue("CDL should return null when the input string is empty", jsonArray == null); } - @Test - public void shouldConvertCDLToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - JSONArray expectedJsonArray = new JSONArray(jsonArrayLines); - assertTrue("CDL should convert string to JSONArray", - jsonArray.equals(expectedJsonArray)); - } - - @Test - public void shouldCreateJSONArrayUsingJSONArray() { - String names = "[Col1, Col2]"; - String values = "V1, V2"; - JSONArray nameJSONArray = new JSONArray(names); - JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); - - JSONArray expectedJsonArray = new JSONArray("[{Col1:V1},{Col2:V2}]"); - assertTrue("CDL should create JSONArray from a names JSONArray and a "+ - "string of values", jsonArray.equals(expectedJsonArray)); - } - @Test public void toStringShouldCheckSpecialChars() { - String method = "toStringShouldCheckSpecialChars():"; /** * Given a JSONArray that was not built by CDL, some chars may be * found that would otherwise be filtered out by CDL. @@ -131,14 +81,64 @@ public class CDLTest { JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonArray.put(jsonObject); - // \r should be filtered from name + // \r will be filtered from name jsonObject.put("Col \r1", "V1"); - // \r should be filtered from value + // \r will be filtered from value jsonObject.put("Col 2", "V2\r"); - String jsonStr = CDL.toString(jsonArray); + boolean normalize = true; + List> expectedLines = + sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize); + List> 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); + } + } - String expectedStr = {"Col 1, Col 2","V1","\"V2\""}; - // Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method); + @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 + public void shouldConvertCDLToJSONArray() { + JSONArray jsonArray = CDL.toJSONArray(lines); + String resultStr = compareJSONArrayToString(jsonArray, lines); + if (resultStr != null) { + assertTrue("CDL should convert string to JSONArray: " + + resultStr, false); + } + } + + @Test + public void shouldCreateJSONArrayUsingJSONArray() { + String names = "Col1, Col2"; + String nameArrayStr = "[" +names+ "]"; + String values = "V1, V2"; + JSONArray nameJSONArray = new JSONArray(nameArrayStr); + JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); + String combinedStr = names+ "\n" +values; + String resultStr = compareJSONArrayToString(jsonArray, combinedStr); + if (resultStr != null) { + assertTrue("CDL should convert JSONArray and string to JSONArray: " + + resultStr, false); + } } @Test @@ -152,7 +152,16 @@ public class CDLTest { final boolean doNotNormalize = false; JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); - assertTrue("CDL should convert string to JSONArray to string", + // normal sorted + List> sortedLines = sortColumnsInLines(lines, normalize); + // sorted, should already be normalized + List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); + boolean result = sortedLines.equals(sortedJsonStr); + if (!result) { + System.out.println("lines: " +sortedLines); + System.out.println("jsonStr: " +sortedJsonStr); + assertTrue("CDL should convert JSONArray back to original string: " + + lines.equals(jsonStr), false); } }