Skip to content

Commit 0e612ba

Browse files
committed
More test corrections for correct position reports in error messages
1 parent 971614a commit 0e612ba

7 files changed

Lines changed: 92 additions & 77 deletions

File tree

src/test/java/org/json/junit/CDLTest.java

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public void unbalancedQuoteInName() {
6161
String badLine = "Col1, \"Col2\nVal1, Val2";
6262
try {
6363
CDL.toJSONArray(badLine);
64-
assertTrue("Expecting an exception", false);
64+
fail("Expecting an exception");
6565
} catch (JSONException e) {
66-
assertTrue("Expecting an exception message",
67-
"Missing close quote '\"'. at 12 [character 0 line 2]".
68-
equals(e.getMessage()));
66+
assertEquals("Expecting an exception message",
67+
"Missing close quote '\"'. at 12 [character 0 line 2]",
68+
e.getMessage());
6969
}
7070
}
7171

@@ -78,11 +78,11 @@ public void unbalancedQuoteInValue() {
7878
String badLine = "Col1, Col2\n\"Val1, Val2";
7979
try {
8080
CDL.toJSONArray(badLine);
81-
assertTrue("Expecting an exception", false);
81+
fail("Expecting an exception");
8282
} catch (JSONException e) {
83-
assertTrue("Expecting an exception message",
84-
"Missing close quote '\"'. at 23 [character 12 line 3]".
85-
equals(e.getMessage()));
83+
assertEquals("Expecting an exception message",
84+
"Missing close quote '\"'. at 22 [character 11 line 3]",
85+
e.getMessage());
8686

8787
}
8888
}
@@ -96,11 +96,11 @@ public void nullInName() {
9696
String badLine = "C\0ol1, Col2\nVal1, Val2";
9797
try {
9898
CDL.toJSONArray(badLine);
99-
assertTrue("Expecting an exception", false);
99+
fail("Expecting an exception");
100100
} catch (JSONException e) {
101-
assertTrue("Expecting an exception message",
102-
"Bad character 'o' (111). at 3 [character 4 line 1]".
103-
equals(e.getMessage()));
101+
assertEquals("Expecting an exception message",
102+
"Bad character 'o' (111). at 2 [character 3 line 1]",
103+
e.getMessage());
104104

105105
}
106106
}
@@ -114,11 +114,11 @@ public void unbalancedEscapedQuote(){
114114
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
115115
try {
116116
CDL.toJSONArray(badLine);
117-
assertTrue("Expecting an exception", false);
117+
fail("Expecting an exception");
118118
} catch (JSONException e) {
119-
assertTrue("Expecting an exception message",
120-
"Missing close quote '\"'. at 27 [character 16 line 3]".
121-
equals(e.getMessage()));
119+
assertEquals("Expecting an exception message",
120+
"Missing close quote '\"'. at 26 [character 15 line 3]",
121+
e.getMessage());
122122

123123
}
124124
}
@@ -128,15 +128,30 @@ public void unbalancedEscapedQuote(){
128128
*/
129129
@Test
130130
public void singleEscapedQuote(){
131-
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
131+
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
132132
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
133133

134134
String cdlStr = CDL.toString(jsonArray);
135135
assertTrue(cdlStr.contains("Col1"));
136136
assertTrue(cdlStr.contains("Col2"));
137137
assertTrue(cdlStr.contains("Val1"));
138138
assertTrue(cdlStr.contains("\"Val2"));
139+
}
140+
141+
/**
142+
* Assert that there is no error for a single escaped quote within a properly
143+
* embedded quote when not the last value.
144+
*/
145+
@Test
146+
public void singleEscapedQuoteMiddleString(){
147+
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
148+
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
139149

150+
String cdlStr = CDL.toString(jsonArray);
151+
assertTrue(cdlStr.contains("Col1"));
152+
assertTrue(cdlStr.contains("Col2"));
153+
assertTrue(cdlStr.contains("Val1"));
154+
assertTrue(cdlStr.contains("\"Val2"));
140155
}
141156

142157
/**
@@ -149,12 +164,12 @@ public void badEscapedQuote(){
149164

150165
try {
151166
CDL.toJSONArray(badLine);
152-
assertTrue("Expecting an exception", false);
167+
fail("Expecting an exception");
153168
} catch (JSONException e) {
154169
System.out.println("Message" + e.getMessage());
155-
assertTrue("Expecting an exception message",
156-
"Bad character 'V' (86). at 20 [character 9 line 3]".
157-
equals(e.getMessage()));
170+
assertEquals("Expecting an exception message",
171+
"Bad character 'V' (86). at 20 [character 9 line 3]",
172+
e.getMessage());
158173

159174
}
160175

@@ -186,8 +201,8 @@ public void emptyString() {
186201
public void onlyColumnNames() {
187202
String columnNameStr = "col1, col2, col3";
188203
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
189-
assertTrue("CDL should return null when only 1 row is given",
190-
jsonArray == null);
204+
assertNull("CDL should return null when only 1 row is given",
205+
jsonArray);
191206
}
192207

193208
/**
@@ -197,8 +212,8 @@ public void onlyColumnNames() {
197212
public void emptyLinesToJSONArray() {
198213
String str = " , , , \n , , , ";
199214
JSONArray jsonArray = CDL.toJSONArray(str);
200-
assertTrue("JSONArray should be null for no content",
201-
jsonArray == null);
215+
assertNull("JSONArray should be null for no content",
216+
jsonArray);
202217
}
203218

204219
/**
@@ -208,8 +223,8 @@ public void emptyLinesToJSONArray() {
208223
public void emptyJSONArrayToString() {
209224
JSONArray jsonArray = new JSONArray();
210225
String str = CDL.toString(jsonArray);
211-
assertTrue("CDL should return null for toString(null)",
212-
str == null);
226+
assertNull("CDL should return null for toString(null)",
227+
str);
213228
}
214229

215230
/**
@@ -218,8 +233,8 @@ public void emptyJSONArrayToString() {
218233
@Test
219234
public void nullJSONArraysToString() {
220235
String str = CDL.toString(null, null);
221-
assertTrue("CDL should return null for toString(null)",
222-
str == null);
236+
assertNull("CDL should return null for toString(null)",
237+
str);
223238
}
224239

225240
/**

src/test/java/org/json/junit/CookieListTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public void malFormedCookieListException() {
4747
String cookieStr = "thisCookieHasNoEqualsChar";
4848
try {
4949
CookieList.toJSONObject(cookieStr);
50-
assertTrue("should throw an exception", false);
50+
fail("should throw an exception");
5151
} catch (JSONException e) {
5252
/**
5353
* Not sure of the missing char, but full string compare fails
5454
*/
55-
assertTrue("Expecting an exception message",
56-
e.getMessage().startsWith("Expected '=' and instead saw '") &&
57-
e.getMessage().endsWith("' at 27 [character 28 line 1]"));
55+
assertEquals("Expecting an exception message",
56+
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
57+
e.getMessage());
5858
}
5959
}
6060

src/test/java/org/json/junit/CookieTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public void malFormedNameValueException() {
4343
String cookieStr = "thisCookieHasNoEqualsChar";
4444
try {
4545
Cookie.toJSONObject(cookieStr);
46-
assertTrue("Expecting an exception", false);
46+
fail("Expecting an exception");
4747
} catch (JSONException e) {
48-
assertTrue("Expecting an exception message",
49-
e.getMessage().startsWith("Expected '=' and instead saw '")
50-
&& e.getMessage().endsWith("' at 27 [character 28 line 1]"));
48+
assertEquals("Expecting an exception message",
49+
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
50+
e.getMessage());
5151
}
5252
}
5353

@@ -61,11 +61,11 @@ public void malFormedAttributeException() {
6161
String cookieStr = "this=Cookie;myAttribute";
6262
try {
6363
Cookie.toJSONObject(cookieStr);
64-
assertTrue("Expecting an exception", false);
64+
fail("Expecting an exception");
6565
} catch (JSONException e) {
66-
assertTrue("Expecting an exception message",
67-
"Missing '=' in cookie parameter. at 25 [character 26 line 1]".
68-
equals(e.getMessage()));
66+
assertEquals("Expecting an exception message",
67+
"Missing '=' in cookie parameter. at 23 [character 24 line 1]",
68+
e.getMessage());
6969
}
7070
}
7171

@@ -79,11 +79,11 @@ public void emptyStringCookieException() {
7979
String cookieStr = "";
8080
try {
8181
Cookie.toJSONObject(cookieStr);
82-
assertTrue("Expecting an exception", false);
82+
fail("Expecting an exception");
8383
} catch (JSONException e) {
84-
assertTrue("Expecting an exception message",
85-
e.getMessage().startsWith("Expected '=' and instead saw '") &&
86-
e.getMessage().endsWith("' at 2 [character 3 line 1]"));
84+
assertEquals("Expecting an exception message",
85+
"Expected '=' and instead saw '' at 0 [character 1 line 1]",
86+
e.getMessage());
8787
}
8888
}
8989

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public void emptStr() {
7474
try {
7575
assertNull("Should throw an exception", new JSONArray(str));
7676
} catch (JSONException e) {
77-
assertTrue("Expected an exception message",
78-
"A JSONArray text must start with '[' at 1 [character 2 line 1]".
79-
equals(e.getMessage()));
77+
assertEquals("Expected an exception message",
78+
"A JSONArray text must start with '[' at 0 [character 1 line 1]",
79+
e.getMessage());
8080
}
8181
}
8282

src/test/java/org/json/junit/JSONMLTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void nonXMLException() {
9898
fail("Expecting an exception");
9999
} catch (JSONException e) {
100100
assertEquals("Expecting an exception message",
101-
"Bad XML at 24 [character 25 line 1]",
101+
"Bad XML at 23 [character 24 line 1]",
102102
e.getMessage());
103103
}
104104
}
@@ -226,7 +226,7 @@ public void invalidBangInTagException() {
226226
fail("Expecting an exception");
227227
} catch (JSONException e) {
228228
assertEquals("Expecting an exception message",
229-
"Misshaped meta tag at 216 [character 13 line 7]",
229+
"Misshaped meta tag at 215 [character 12 line 7]",
230230
e.getMessage());
231231
}
232232
}
@@ -256,7 +256,7 @@ public void invalidBangNoCloseInTagException() {
256256
fail("Expecting an exception");
257257
} catch (JSONException e) {
258258
assertEquals("Expecting an exception message",
259-
"Misshaped meta tag at 215 [character 13 line 7]",
259+
"Misshaped meta tag at 214 [character 12 line 7]",
260260
e.getMessage());
261261
}
262262
}

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,36 +1853,36 @@ public void jsonObjectParsingErrors() {
18531853
String str = "abc";
18541854
assertNull("Expected an exception",new JSONObject(str));
18551855
} catch (JSONException e) {
1856-
assertTrue("Expecting an exception message",
1857-
"A JSONObject text must begin with '{' at 1 [character 2 line 1]".
1858-
equals(e.getMessage()));
1856+
assertEquals("Expecting an exception message",
1857+
"A JSONObject text must begin with '{' at 1 [character 2 line 1]",
1858+
e.getMessage());
18591859
}
18601860
try {
18611861
// does not end with '}'
18621862
String str = "{";
18631863
assertNull("Expected an exception",new JSONObject(str));
18641864
} catch (JSONException e) {
1865-
assertTrue("Expecting an exception message",
1866-
"A JSONObject text must end with '}' at 2 [character 3 line 1]".
1867-
equals(e.getMessage()));
1865+
assertEquals("Expecting an exception message",
1866+
"A JSONObject text must end with '}' at 1 [character 2 line 1]",
1867+
e.getMessage());
18681868
}
18691869
try {
18701870
// key with no ':'
18711871
String str = "{\"myKey\" = true}";
18721872
assertNull("Expected an exception",new JSONObject(str));
18731873
} catch (JSONException e) {
1874-
assertTrue("Expecting an exception message",
1875-
"Expected a ':' after a key at 10 [character 11 line 1]".
1876-
equals(e.getMessage()));
1874+
assertEquals("Expecting an exception message",
1875+
"Expected a ':' after a key at 10 [character 11 line 1]",
1876+
e.getMessage());
18771877
}
18781878
try {
18791879
// entries with no ',' separator
18801880
String str = "{\"myKey\":true \"myOtherKey\":false}";
18811881
assertNull("Expected an exception",new JSONObject(str));
18821882
} catch (JSONException e) {
1883-
assertTrue("Expecting an exception message",
1884-
"Expected a ',' or '}' at 15 [character 16 line 1]".
1885-
equals(e.getMessage()));
1883+
assertEquals("Expecting an exception message",
1884+
"Expected a ',' or '}' at 15 [character 16 line 1]",
1885+
e.getMessage());
18861886
}
18871887
try {
18881888
// append to wrong key
@@ -1891,9 +1891,9 @@ public void jsonObjectParsingErrors() {
18911891
jsonObject.append("myKey", "hello");
18921892
fail("Expected an exception");
18931893
} catch (JSONException e) {
1894-
assertTrue("Expecting an exception message",
1895-
"JSONObject[myKey] is not a JSONArray.".
1896-
equals(e.getMessage()));
1894+
assertEquals("Expecting an exception message",
1895+
"JSONObject[myKey] is not a JSONArray.",
1896+
e.getMessage());
18971897
}
18981898
try {
18991899
// increment wrong key
@@ -1902,9 +1902,9 @@ public void jsonObjectParsingErrors() {
19021902
jsonObject.increment("myKey");
19031903
fail("Expected an exception");
19041904
} catch (JSONException e) {
1905-
assertTrue("Expecting an exception message",
1906-
"Unable to increment [\"myKey\"].".
1907-
equals(e.getMessage()));
1905+
assertEquals("Expecting an exception message",
1906+
"Unable to increment [\"myKey\"].",
1907+
e.getMessage());
19081908
}
19091909
try {
19101910
// invalid key
@@ -1913,18 +1913,18 @@ public void jsonObjectParsingErrors() {
19131913
jsonObject.get(null);
19141914
fail("Expected an exception");
19151915
} catch (JSONException e) {
1916-
assertTrue("Expecting an exception message",
1917-
"Null key.".
1918-
equals(e.getMessage()));
1916+
assertEquals("Expecting an exception message",
1917+
"Null key.",
1918+
e.getMessage());
19191919
}
19201920
try {
19211921
// invalid numberToString()
19221922
JSONObject.numberToString((Number)null);
19231923
fail("Expected an exception");
19241924
} catch (JSONException e) {
1925-
assertTrue("Expecting an exception message",
1926-
"Null pointer".
1927-
equals(e.getMessage()));
1925+
assertEquals("Expecting an exception message",
1926+
"Null pointer",
1927+
e.getMessage());
19281928
}
19291929
try {
19301930
// null put key

src/test/java/org/json/junit/XMLTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void shouldHandleInvalidBangInTag() {
103103
fail("Expecting a JSONException");
104104
} catch (JSONException e) {
105105
assertEquals("Expecting an exception message",
106-
"Misshaped meta tag at 215 [character 13 line 7]",
106+
"Misshaped meta tag at 214 [character 12 line 7]",
107107
e.getMessage());
108108
}
109109
}
@@ -128,7 +128,7 @@ public void shouldHandleInvalidBangNoCloseInTag() {
128128
fail("Expecting a JSONException");
129129
} catch (JSONException e) {
130130
assertEquals("Expecting an exception message",
131-
"Misshaped meta tag at 214 [character 13 line 7]",
131+
"Misshaped meta tag at 213 [character 12 line 7]",
132132
e.getMessage());
133133
}
134134
}

0 commit comments

Comments
 (0)