Skip to content

Commit 77d0873

Browse files
committed
Merge pull request #45 from captainIowa/master
Added unit tests for escaped quotes.
2 parents 052ce94 + e001917 commit 77d0873

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void unbalancedQuoteInName() {
6868
equals(e.getMessage()));
6969
}
7070
}
71-
71+
7272
/**
7373
* Attempts to create a JSONArray from a string with unbalanced quotes
7474
* in value line. Expects a JSONException.
@@ -104,7 +104,62 @@ public void nullInName() {
104104

105105
}
106106
}
107+
108+
/**
109+
* Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
110+
* Expects a JSONException.
111+
*/
112+
@Test
113+
public void unbalancedEscapedQuote(){
114+
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
115+
try {
116+
CDL.toJSONArray(badLine);
117+
assertTrue("Expecting an exception", false);
118+
} catch (JSONException e) {
119+
assertTrue("Expecting an exception message",
120+
"Missing close quote '\"'. at 27 [character 16 line 3]".
121+
equals(e.getMessage()));
122+
123+
}
124+
}
107125

126+
/**
127+
* Assert that there is no error for a single escaped quote within a properly embedded quote.
128+
*/
129+
@Test
130+
public void singleEscapedQuote(){
131+
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
132+
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
133+
134+
String cdlStr = CDL.toString(jsonArray);
135+
assertTrue(cdlStr.contains("Col1"));
136+
assertTrue(cdlStr.contains("Col2"));
137+
assertTrue(cdlStr.contains("Val1"));
138+
assertTrue(cdlStr.contains("\"Val2"));
139+
140+
}
141+
142+
/**
143+
* Attempt to create a JSONArray with an escape quote and no enclosing quotes.
144+
* Expects a JSONException.
145+
*/
146+
@Test
147+
public void badEscapedQuote(){
148+
String badLine = "Col1, Col2\nVal1, \"\"Val2";
149+
150+
try {
151+
CDL.toJSONArray(badLine);
152+
assertTrue("Expecting an exception", false);
153+
} catch (JSONException e) {
154+
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()));
158+
159+
}
160+
161+
}
162+
108163
/**
109164
* call toString with a null array
110165
*/

0 commit comments

Comments
 (0)