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:
parent
971614ac8b
commit
0e612ba8a4
7 changed files with 92 additions and 77 deletions
|
@ -61,11 +61,11 @@ public class CDLTest {
|
|||
String badLine = "Col1, \"Col2\nVal1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 12 [character 0 line 2]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 12 [character 0 line 2]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,11 +78,11 @@ public class CDLTest {
|
|||
String badLine = "Col1, Col2\n\"Val1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 23 [character 12 line 3]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 22 [character 11 line 3]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -96,11 +96,11 @@ public class CDLTest {
|
|||
String badLine = "C\0ol1, Col2\nVal1, Val2";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Bad character 'o' (111). at 3 [character 4 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad character 'o' (111). at 2 [character 3 line 1]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -114,11 +114,11 @@ public class CDLTest {
|
|||
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
|
||||
try {
|
||||
CDL.toJSONArray(badLine);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 27 [character 16 line 3]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing close quote '\"'. at 26 [character 15 line 3]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -136,7 +136,22 @@ public class CDLTest {
|
|||
assertTrue(cdlStr.contains("Col2"));
|
||||
assertTrue(cdlStr.contains("Val1"));
|
||||
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 {
|
||||
CDL.toJSONArray(badLine);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
System.out.println("Message" + e.getMessage());
|
||||
assertTrue("Expecting an exception message",
|
||||
"Bad character 'V' (86). at 20 [character 9 line 3]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad character 'V' (86). at 20 [character 9 line 3]",
|
||||
e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
|
@ -186,8 +201,8 @@ public class CDLTest {
|
|||
public void onlyColumnNames() {
|
||||
String columnNameStr = "col1, col2, col3";
|
||||
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
|
||||
assertTrue("CDL should return null when only 1 row is given",
|
||||
jsonArray == null);
|
||||
assertNull("CDL should return null when only 1 row is given",
|
||||
jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,8 +212,8 @@ public class CDLTest {
|
|||
public void emptyLinesToJSONArray() {
|
||||
String str = " , , , \n , , , ";
|
||||
JSONArray jsonArray = CDL.toJSONArray(str);
|
||||
assertTrue("JSONArray should be null for no content",
|
||||
jsonArray == null);
|
||||
assertNull("JSONArray should be null for no content",
|
||||
jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -208,8 +223,8 @@ public class CDLTest {
|
|||
public void emptyJSONArrayToString() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
String str = CDL.toString(jsonArray);
|
||||
assertTrue("CDL should return null for toString(null)",
|
||||
str == null);
|
||||
assertNull("CDL should return null for toString(null)",
|
||||
str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -218,8 +233,8 @@ public class CDLTest {
|
|||
@Test
|
||||
public void nullJSONArraysToString() {
|
||||
String str = CDL.toString(null, null);
|
||||
assertTrue("CDL should return null for toString(null)",
|
||||
str == null);
|
||||
assertNull("CDL should return null for toString(null)",
|
||||
str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -47,14 +47,14 @@ public class CookieListTest {
|
|||
String cookieStr = "thisCookieHasNoEqualsChar";
|
||||
try {
|
||||
CookieList.toJSONObject(cookieStr);
|
||||
assertTrue("should throw an exception", false);
|
||||
fail("should throw an exception");
|
||||
} catch (JSONException e) {
|
||||
/**
|
||||
* Not sure of the missing char, but full string compare fails
|
||||
*/
|
||||
assertTrue("Expecting an exception message",
|
||||
e.getMessage().startsWith("Expected '=' and instead saw '") &&
|
||||
e.getMessage().endsWith("' at 27 [character 28 line 1]"));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,11 +43,11 @@ public class CookieTest {
|
|||
String cookieStr = "thisCookieHasNoEqualsChar";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
e.getMessage().startsWith("Expected '=' and instead saw '")
|
||||
&& e.getMessage().endsWith("' at 27 [character 28 line 1]"));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,11 +61,11 @@ public class CookieTest {
|
|||
String cookieStr = "this=Cookie;myAttribute";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Missing '=' in cookie parameter. at 25 [character 26 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Missing '=' in cookie parameter. at 23 [character 24 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,11 +79,11 @@ public class CookieTest {
|
|||
String cookieStr = "";
|
||||
try {
|
||||
Cookie.toJSONObject(cookieStr);
|
||||
assertTrue("Expecting an exception", false);
|
||||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
e.getMessage().startsWith("Expected '=' and instead saw '") &&
|
||||
e.getMessage().endsWith("' at 2 [character 3 line 1]"));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected '=' and instead saw '' at 0 [character 1 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -74,9 +74,9 @@ public class JSONArrayTest {
|
|||
try {
|
||||
assertNull("Should throw an exception", new JSONArray(str));
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expected an exception message",
|
||||
"A JSONArray text must start with '[' at 1 [character 2 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expected an exception message",
|
||||
"A JSONArray text must start with '[' at 0 [character 1 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ public class JSONMLTest {
|
|||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Bad XML at 24 [character 25 line 1]",
|
||||
"Bad XML at 23 [character 24 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ public class JSONMLTest {
|
|||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ public class JSONMLTest {
|
|||
fail("Expecting an exception");
|
||||
} catch (JSONException e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1853,36 +1853,36 @@ public class JSONObjectTest {
|
|||
String str = "abc";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"A JSONObject text must begin with '{' at 1 [character 2 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"A JSONObject text must begin with '{' at 1 [character 2 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// does not end with '}'
|
||||
String str = "{";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"A JSONObject text must end with '}' at 2 [character 3 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"A JSONObject text must end with '}' at 1 [character 2 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// key with no ':'
|
||||
String str = "{\"myKey\" = true}";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Expected a ':' after a key at 10 [character 11 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected a ':' after a key at 10 [character 11 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// entries with no ',' separator
|
||||
String str = "{\"myKey\":true \"myOtherKey\":false}";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Expected a ',' or '}' at 15 [character 16 line 1]".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected a ',' or '}' at 15 [character 16 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// append to wrong key
|
||||
|
@ -1891,9 +1891,9 @@ public class JSONObjectTest {
|
|||
jsonObject.append("myKey", "hello");
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"JSONObject[myKey] is not a JSONArray.".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"JSONObject[myKey] is not a JSONArray.",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// increment wrong key
|
||||
|
@ -1902,9 +1902,9 @@ public class JSONObjectTest {
|
|||
jsonObject.increment("myKey");
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Unable to increment [\"myKey\"].".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Unable to increment [\"myKey\"].",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// invalid key
|
||||
|
@ -1913,18 +1913,18 @@ public class JSONObjectTest {
|
|||
jsonObject.get(null);
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Null key.".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Null key.",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// invalid numberToString()
|
||||
JSONObject.numberToString((Number)null);
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("Expecting an exception message",
|
||||
"Null pointer".
|
||||
equals(e.getMessage()));
|
||||
assertEquals("Expecting an exception message",
|
||||
"Null pointer",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// null put key
|
||||
|
|
|
@ -103,7 +103,7 @@ public class XMLTest {
|
|||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ public class XMLTest {
|
|||
fail("Expecting a JSONException");
|
||||
} catch (JSONException e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue