|
17 | 17 |
|
18 | 18 | package org.apache.commons.csv; |
19 | 19 |
|
| 20 | +import static org.apache.commons.csv.Constants.BACKSPACE; |
| 21 | +import static org.apache.commons.csv.Constants.CR; |
| 22 | +import static org.apache.commons.csv.Constants.FF; |
| 23 | +import static org.apache.commons.csv.Constants.LF; |
| 24 | +import static org.apache.commons.csv.Constants.TAB; |
20 | 25 | import static org.apache.commons.csv.Token.Type.COMMENT; |
21 | 26 | import static org.apache.commons.csv.Token.Type.EOF; |
22 | 27 | import static org.apache.commons.csv.Token.Type.EORECORD; |
@@ -282,4 +287,29 @@ public void testDelimiterIsWhitespace() throws IOException { |
282 | 287 | assertTokenEquals(TOKEN, "five", parser.nextToken(new Token())); |
283 | 288 | assertTokenEquals(EOF, "six", parser.nextToken(new Token())); |
284 | 289 | } |
| 290 | + |
| 291 | + @Test |
| 292 | + public void testEscaping() throws Exception { |
| 293 | + final String code = "plain," + |
| 294 | + "CR!" + CR + "Escaped," + |
| 295 | + "LF!" + LF +"Escaped," + |
| 296 | + "TAB!" + TAB +"Escaped," + |
| 297 | + "BACKSPACE!" + BACKSPACE +"Escaped," + |
| 298 | + "FF!" + FF +"Escaped"; |
| 299 | + final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build()); |
| 300 | + assertTokenEquals(TOKEN, "plain", lexer.nextToken(new Token())); |
| 301 | + assertTokenEquals(TOKEN, "CR" + CR + "Escaped", lexer.nextToken(new Token())); |
| 302 | + assertTokenEquals(TOKEN, "LF" + LF + "Escaped", lexer.nextToken(new Token())); |
| 303 | + assertTokenEquals(TOKEN, "TAB" + TAB + "Escaped", lexer.nextToken(new Token())); |
| 304 | + assertTokenEquals(TOKEN, "BACKSPACE" + BACKSPACE + "Escaped", lexer.nextToken(new Token())); |
| 305 | + assertTokenEquals(EOF, "FF" + FF + "Escaped", lexer.nextToken(new Token())); |
| 306 | + } |
| 307 | + |
| 308 | + @Test(expected = IOException.class) |
| 309 | + public void testEscapingAtEOF() throws Exception { |
| 310 | + final String code = "escaping at EOF is evil!"; |
| 311 | + final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build()); |
| 312 | + |
| 313 | + lexer.nextToken(new Token()); |
| 314 | + } |
285 | 315 | } |
0 commit comments