From 3a1806ba26c3088de58ed5898f278dcd075b0552 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sun, 24 Mar 2013 13:29:22 +0000 Subject: [PATCH] Split up escape testing into separate methods, add escaping tests that should work after CSV-58 is resolved (currently ignored) git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1460364 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVLexerTest.java | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/CSVLexerTest.java b/src/test/java/org/apache/commons/csv/CSVLexerTest.java index 63beb02b..f8b591b4 100644 --- a/src/test/java/org/apache/commons/csv/CSVLexerTest.java +++ b/src/test/java/org/apache/commons/csv/CSVLexerTest.java @@ -34,6 +34,7 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.io.StringReader; +import org.junit.Ignore; import org.junit.Test; /** @@ -289,20 +290,56 @@ public class CSVLexerTest { } @Test - public void testEscaping() throws Exception { - final String code = "plain," + - "CR!" + CR + "Escaped," + - "LF!" + LF +"Escaped," + - "TAB!" + TAB +"Escaped," + - "BACKSPACE!" + BACKSPACE +"Escaped," + - "FF!" + FF +"Escaped"; - final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build()); - assertTokenEquals(TOKEN, "plain", lexer.nextToken(new Token())); - assertTokenEquals(TOKEN, "CR" + CR + "Escaped", lexer.nextToken(new Token())); - assertTokenEquals(TOKEN, "LF" + LF + "Escaped", lexer.nextToken(new Token())); - assertTokenEquals(TOKEN, "TAB" + TAB + "Escaped", lexer.nextToken(new Token())); - assertTokenEquals(TOKEN, "BACKSPACE" + BACKSPACE + "Escaped", lexer.nextToken(new Token())); - assertTokenEquals(EOF, "FF" + FF + "Escaped", lexer.nextToken(new Token())); + public void testEscapedCR() throws Exception { + final Lexer lexer = getLexer("character\\" + CR + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character" + CR + "Escaped", lexer.nextToken(new Token())); + } + + @Test + public void testEscapedLF() throws Exception { + final Lexer lexer = getLexer("character\\" + LF + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character" + LF + "Escaped", lexer.nextToken(new Token())); + } + + @Test + public void testEscapedTab() throws Exception { + final Lexer lexer = getLexer("character\\" + TAB + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character" + TAB + "Escaped", lexer.nextToken(new Token())); + } + + @Test + public void testEscapeBackspace() throws Exception { + final Lexer lexer = getLexer("character\\" + BACKSPACE + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character" + BACKSPACE + "Escaped", lexer.nextToken(new Token())); + } + + @Test + public void testEscapeFF() throws Exception { + final Lexer lexer = getLexer("character\\" + FF + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character" + FF + "Escaped", lexer.nextToken(new Token())); + } + + @Test + public void testEscapedMySqlNullValue() throws Exception { + // MySQL uses \N to symbolize null values. We have to restore this + final Lexer lexer = getLexer("character\\\\NEscaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character\\NEscaped", lexer.nextToken(new Token())); + } + + // FIXME this should work after CSV-58 is resolved. Currently the result will be "characteraEscaped" + @Test + @Ignore + public void testEscapedCharacter() throws Exception { + final Lexer lexer = getLexer("character\\aEscaped", CSVFormat.newBuilder().withEscape('\\').build()); + assertTokenEquals(EOF, "character\\aEscaped", lexer.nextToken(new Token())); + } + + // FIXME this should work after CSV-58 is resolved. Currentyl the result will be "characterCREscaped" + @Test + @Ignore + public void testEscapedControlCharacter() throws Exception { + final Lexer lexer = getLexer("character!rEscaped", CSVFormat.newBuilder().withEscape('!').build()); + assertTokenEquals(EOF, "character!rEscaped", lexer.nextToken(new Token())); } @Test(expected = IOException.class)