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

More test corrections for correct position reports in error messages

This commit is contained in:
John J. Aylward 2017-06-21 19:56:00 -04:00
parent 971614ac8b
commit 0e612ba8a4
7 changed files with 92 additions and 77 deletions

View file

@ -61,11 +61,11 @@ public class CDLTest {
String badLine = "Col1, \"Col2\nVal1, Val2"; String badLine = "Col1, \"Col2\nVal1, Val2";
try { try {
CDL.toJSONArray(badLine); CDL.toJSONArray(badLine);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 12 [character 0 line 2]". "Missing close quote '\"'. at 12 [character 0 line 2]",
equals(e.getMessage())); e.getMessage());
} }
} }
@ -78,11 +78,11 @@ public class CDLTest {
String badLine = "Col1, Col2\n\"Val1, Val2"; String badLine = "Col1, Col2\n\"Val1, Val2";
try { try {
CDL.toJSONArray(badLine); CDL.toJSONArray(badLine);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 23 [character 12 line 3]". "Missing close quote '\"'. at 22 [character 11 line 3]",
equals(e.getMessage())); e.getMessage());
} }
} }
@ -96,11 +96,11 @@ public class CDLTest {
String badLine = "C\0ol1, Col2\nVal1, Val2"; String badLine = "C\0ol1, Col2\nVal1, Val2";
try { try {
CDL.toJSONArray(badLine); CDL.toJSONArray(badLine);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Bad character 'o' (111). at 3 [character 4 line 1]". "Bad character 'o' (111). at 2 [character 3 line 1]",
equals(e.getMessage())); e.getMessage());
} }
} }
@ -114,11 +114,11 @@ public class CDLTest {
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\""; String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
try { try {
CDL.toJSONArray(badLine); CDL.toJSONArray(badLine);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 27 [character 16 line 3]". "Missing close quote '\"'. at 26 [character 15 line 3]",
equals(e.getMessage())); e.getMessage());
} }
} }
@ -128,7 +128,7 @@ public class CDLTest {
*/ */
@Test @Test
public void singleEscapedQuote(){ public void singleEscapedQuote(){
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\""; String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
JSONArray jsonArray = CDL.toJSONArray(singleEscape); JSONArray jsonArray = CDL.toJSONArray(singleEscape);
String cdlStr = CDL.toString(jsonArray); String cdlStr = CDL.toString(jsonArray);
@ -136,7 +136,22 @@ public class CDLTest {
assertTrue(cdlStr.contains("Col2")); assertTrue(cdlStr.contains("Col2"));
assertTrue(cdlStr.contains("Val1")); assertTrue(cdlStr.contains("Val1"));
assertTrue(cdlStr.contains("\"Val2")); assertTrue(cdlStr.contains("\"Val2"));
}
/**
* Assert that there is no error for a single escaped quote within a properly
* embedded quote when not the last value.
*/
@Test
public void singleEscapedQuoteMiddleString(){
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
String cdlStr = CDL.toString(jsonArray);
assertTrue(cdlStr.contains("Col1"));
assertTrue(cdlStr.contains("Col2"));
assertTrue(cdlStr.contains("Val1"));
assertTrue(cdlStr.contains("\"Val2"));
} }
/** /**
@ -149,12 +164,12 @@ public class CDLTest {
try { try {
CDL.toJSONArray(badLine); CDL.toJSONArray(badLine);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
System.out.println("Message" + e.getMessage()); System.out.println("Message" + e.getMessage());
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Bad character 'V' (86). at 20 [character 9 line 3]". "Bad character 'V' (86). at 20 [character 9 line 3]",
equals(e.getMessage())); e.getMessage());
} }
@ -186,8 +201,8 @@ public class CDLTest {
public void onlyColumnNames() { public void onlyColumnNames() {
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", assertNull("CDL should return null when only 1 row is given",
jsonArray == null); jsonArray);
} }
/** /**
@ -197,8 +212,8 @@ public class CDLTest {
public void emptyLinesToJSONArray() { public void emptyLinesToJSONArray() {
String str = " , , , \n , , , "; String str = " , , , \n , , , ";
JSONArray jsonArray = CDL.toJSONArray(str); JSONArray jsonArray = CDL.toJSONArray(str);
assertTrue("JSONArray should be null for no content", assertNull("JSONArray should be null for no content",
jsonArray == null); jsonArray);
} }
/** /**
@ -208,8 +223,8 @@ public class CDLTest {
public void emptyJSONArrayToString() { public void emptyJSONArrayToString() {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
String str = CDL.toString(jsonArray); String str = CDL.toString(jsonArray);
assertTrue("CDL should return null for toString(null)", assertNull("CDL should return null for toString(null)",
str == null); str);
} }
/** /**
@ -218,8 +233,8 @@ public class CDLTest {
@Test @Test
public void nullJSONArraysToString() { public void nullJSONArraysToString() {
String str = CDL.toString(null, null); String str = CDL.toString(null, null);
assertTrue("CDL should return null for toString(null)", assertNull("CDL should return null for toString(null)",
str == null); str);
} }
/** /**

View file

@ -47,14 +47,14 @@ public class CookieListTest {
String cookieStr = "thisCookieHasNoEqualsChar"; String cookieStr = "thisCookieHasNoEqualsChar";
try { try {
CookieList.toJSONObject(cookieStr); CookieList.toJSONObject(cookieStr);
assertTrue("should throw an exception", false); fail("should throw an exception");
} catch (JSONException e) { } catch (JSONException e) {
/** /**
* Not sure of the missing char, but full string compare fails * Not sure of the missing char, but full string compare fails
*/ */
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
e.getMessage().startsWith("Expected '=' and instead saw '") && "Expected '=' and instead saw '' at 25 [character 26 line 1]",
e.getMessage().endsWith("' at 27 [character 28 line 1]")); e.getMessage());
} }
} }

View file

@ -43,11 +43,11 @@ public class CookieTest {
String cookieStr = "thisCookieHasNoEqualsChar"; String cookieStr = "thisCookieHasNoEqualsChar";
try { try {
Cookie.toJSONObject(cookieStr); Cookie.toJSONObject(cookieStr);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
e.getMessage().startsWith("Expected '=' and instead saw '") "Expected '=' and instead saw '' at 25 [character 26 line 1]",
&& e.getMessage().endsWith("' at 27 [character 28 line 1]")); e.getMessage());
} }
} }
@ -61,11 +61,11 @@ public class CookieTest {
String cookieStr = "this=Cookie;myAttribute"; String cookieStr = "this=Cookie;myAttribute";
try { try {
Cookie.toJSONObject(cookieStr); Cookie.toJSONObject(cookieStr);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing '=' in cookie parameter. at 25 [character 26 line 1]". "Missing '=' in cookie parameter. at 23 [character 24 line 1]",
equals(e.getMessage())); e.getMessage());
} }
} }
@ -79,11 +79,11 @@ public class CookieTest {
String cookieStr = ""; String cookieStr = "";
try { try {
Cookie.toJSONObject(cookieStr); Cookie.toJSONObject(cookieStr);
assertTrue("Expecting an exception", false); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
e.getMessage().startsWith("Expected '=' and instead saw '") && "Expected '=' and instead saw '' at 0 [character 1 line 1]",
e.getMessage().endsWith("' at 2 [character 3 line 1]")); e.getMessage());
} }
} }

View file

@ -74,9 +74,9 @@ public class JSONArrayTest {
try { try {
assertNull("Should throw an exception", new JSONArray(str)); assertNull("Should throw an exception", new JSONArray(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expected an exception message", assertEquals("Expected an exception message",
"A JSONArray text must start with '[' at 1 [character 2 line 1]". "A JSONArray text must start with '[' at 0 [character 1 line 1]",
equals(e.getMessage())); e.getMessage());
} }
} }

View file

@ -98,7 +98,7 @@ public class JSONMLTest {
fail("Expecting an exception"); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Bad XML at 24 [character 25 line 1]", "Bad XML at 23 [character 24 line 1]",
e.getMessage()); e.getMessage());
} }
} }
@ -226,7 +226,7 @@ public class JSONMLTest {
fail("Expecting an exception"); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Misshaped meta tag at 216 [character 13 line 7]", "Misshaped meta tag at 215 [character 12 line 7]",
e.getMessage()); e.getMessage());
} }
} }
@ -256,7 +256,7 @@ public class JSONMLTest {
fail("Expecting an exception"); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Misshaped meta tag at 215 [character 13 line 7]", "Misshaped meta tag at 214 [character 12 line 7]",
e.getMessage()); e.getMessage());
} }
} }

View file

@ -1853,36 +1853,36 @@ public class JSONObjectTest {
String str = "abc"; String str = "abc";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception",new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"A JSONObject text must begin with '{' at 1 [character 2 line 1]". "A JSONObject text must begin with '{' at 1 [character 2 line 1]",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// does not end with '}' // does not end with '}'
String str = "{"; String str = "{";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception",new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 2 [character 3 line 1]". "A JSONObject text must end with '}' at 1 [character 2 line 1]",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// key with no ':' // key with no ':'
String str = "{\"myKey\" = true}"; String str = "{\"myKey\" = true}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception",new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ':' after a key at 10 [character 11 line 1]". "Expected a ':' after a key at 10 [character 11 line 1]",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// entries with no ',' separator // entries with no ',' separator
String str = "{\"myKey\":true \"myOtherKey\":false}"; String str = "{\"myKey\":true \"myOtherKey\":false}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception",new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ',' or '}' at 15 [character 16 line 1]". "Expected a ',' or '}' at 15 [character 16 line 1]",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// append to wrong key // append to wrong key
@ -1891,9 +1891,9 @@ public class JSONObjectTest {
jsonObject.append("myKey", "hello"); jsonObject.append("myKey", "hello");
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"JSONObject[myKey] is not a JSONArray.". "JSONObject[myKey] is not a JSONArray.",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// increment wrong key // increment wrong key
@ -1902,9 +1902,9 @@ public class JSONObjectTest {
jsonObject.increment("myKey"); jsonObject.increment("myKey");
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Unable to increment [\"myKey\"].". "Unable to increment [\"myKey\"].",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// invalid key // invalid key
@ -1913,18 +1913,18 @@ public class JSONObjectTest {
jsonObject.get(null); jsonObject.get(null);
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Null key.". "Null key.",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// invalid numberToString() // invalid numberToString()
JSONObject.numberToString((Number)null); JSONObject.numberToString((Number)null);
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertEquals("Expecting an exception message",
"Null pointer". "Null pointer",
equals(e.getMessage())); e.getMessage());
} }
try { try {
// null put key // null put key

View file

@ -103,7 +103,7 @@ public class XMLTest {
fail("Expecting a JSONException"); fail("Expecting a JSONException");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Misshaped meta tag at 215 [character 13 line 7]", "Misshaped meta tag at 214 [character 12 line 7]",
e.getMessage()); e.getMessage());
} }
} }
@ -128,7 +128,7 @@ public class XMLTest {
fail("Expecting a JSONException"); fail("Expecting a JSONException");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Misshaped meta tag at 214 [character 13 line 7]", "Misshaped meta tag at 213 [character 12 line 7]",
e.getMessage()); e.getMessage());
} }
} }