Skip to content

Commit 974a5f7

Browse files
authored
Merge pull request #74 from johnjaylward/AndroidSupport
Correct expected position information in error messages
2 parents 441fec7 + 899cf52 commit 974a5f7

File tree

10 files changed

+381
-148
lines changed

10 files changed

+381
-148
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,26 @@ A unit test has the following stages:
101101
| CDL.java | 98.8% | Reasonable test cases. |
102102
| Cookie.java | 98.9% | Reasonable test cases. |
103103
| CookieList.java |96.5% | Reasonable test cases. |
104-
| EnumTest.java | n/a | Just documenting how enums are handled. |
105-
| HTTP.java | 98.7%| Coverage > 90% |
104+
| HTTP.java | 98.8%| Coverage > 90% |
106105
| HTTPTokener.java |93.2% | No test |
107-
| JSONArray.java |95.9% | Reasonable test cases |
108-
| JSONException.java | 26.7% | No test |
109-
| JSONML.java | 86.8%| In progress |
110-
| JSONObject | 94.0% | Reasonable test cases |
111-
| JSONObject.Null | 87.5% | No test |
106+
| JSONArray.java |88.3% | Reasonable test cases. Need new tests for newer API functions |
107+
| JSONException.java | 100% | No test |
108+
| JSONML.java | 84.4%| In progress |
109+
| JSONObject | 96.7% | Reasonable test cases |
110+
| JSONObject.Null | 77.8% | No test |
111+
| JSONPointer | 96.3% | Reasonable test cases |
112+
| JSONPointerException | 100% | No test |
112113
| JSONString.java | | No test |
113114
| JSONStringer.java | 93.8%| Coverage > 90% |
114-
| JSONTokener.java | 72.1% | In progress |
115-
| JSONWriter.java | 87.5% | No test |
116-
| Property.java | 94.8% | Coverage > 90% |
117-
| XML.java | 87.4% | In progress |
118-
| XMLTokener.java| 82.7%| No test |
115+
| JSONTokener.java | 87.5% | In progress |
116+
| JSONWriter.java | 89.15% | No test |
117+
| Property.java | 95.8% | Coverage > 90% |
118+
| XML.java | 77.3% | In progress |
119+
| XMLTokener.java| 82.4%| No test |
119120

120121
| Files used in test |
121122
| ------------- |
123+
| EnumTest.java |
122124
| MyBean.java |
123125
| MyBigNumberBean.java |
124126
| MyEnum.java |

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 2]",
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 2]",
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 2]",
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

0 commit comments

Comments
 (0)