Initialize format used by several test methods in setUp

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1460367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2013-03-24 13:36:16 +00:00
parent 9e3f3cccb2
commit dbbae1eccd
1 changed files with 18 additions and 10 deletions

View File

@ -34,6 +34,7 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -44,6 +45,13 @@ import org.junit.Test;
*/ */
public class CSVLexerTest { public class CSVLexerTest {
private CSVFormat formatWithEscaping;
@Before
public void setUp() {
formatWithEscaping = CSVFormat.newBuilder().withEscape('\\').build();
}
private Lexer getLexer(final String input, final CSVFormat format) { private Lexer getLexer(final String input, final CSVFormat format) {
return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input))); return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input)));
} }
@ -193,7 +201,7 @@ public class CSVLexerTest {
* \,, * \,,
*/ */
final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne"; final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
final CSVFormat format = CSVFormat.newBuilder().withEscape('\\').withIgnoreEmptyLines(false).build(); final CSVFormat format = formatWithEscaping.toBuilder().withIgnoreEmptyLines(false).build();
assertTrue(format.isEscaping()); assertTrue(format.isEscaping());
final Lexer parser = getLexer(code, format); final Lexer parser = getLexer(code, format);
@ -274,38 +282,38 @@ public class CSVLexerTest {
@Test @Test
public void testEscapedCR() throws Exception { public void testEscapedCR() throws Exception {
final Lexer lexer = getLexer("character\\" + CR + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\" + CR + "Escaped", formatWithEscaping);
assertTokenEquals(EOF, "character" + CR + "Escaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character" + CR + "Escaped", lexer.nextToken(new Token()));
} }
@Test @Test
public void testEscapedLF() throws Exception { public void testEscapedLF() throws Exception {
final Lexer lexer = getLexer("character\\" + LF + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\" + LF + "Escaped", formatWithEscaping);
assertTokenEquals(EOF, "character" + LF + "Escaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character" + LF + "Escaped", lexer.nextToken(new Token()));
} }
@Test @Test
public void testEscapedTab() throws Exception { public void testEscapedTab() throws Exception {
final Lexer lexer = getLexer("character\\" + TAB + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\" + TAB + "Escaped", formatWithEscaping);
assertTokenEquals(EOF, "character" + TAB + "Escaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character" + TAB + "Escaped", lexer.nextToken(new Token()));
} }
@Test @Test
public void testEscapeBackspace() throws Exception { public void testEscapeBackspace() throws Exception {
final Lexer lexer = getLexer("character\\" + BACKSPACE + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\" + BACKSPACE + "Escaped", formatWithEscaping);
assertTokenEquals(EOF, "character" + BACKSPACE + "Escaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character" + BACKSPACE + "Escaped", lexer.nextToken(new Token()));
} }
@Test @Test
public void testEscapeFF() throws Exception { public void testEscapeFF() throws Exception {
final Lexer lexer = getLexer("character\\" + FF + "Escaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\" + FF + "Escaped", formatWithEscaping);
assertTokenEquals(EOF, "character" + FF + "Escaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character" + FF + "Escaped", lexer.nextToken(new Token()));
} }
@Test @Test
public void testEscapedMySqlNullValue() throws Exception { public void testEscapedMySqlNullValue() throws Exception {
// MySQL uses \N to symbolize null values. We have to restore this // MySQL uses \N to symbolize null values. We have to restore this
final Lexer lexer = getLexer("character\\\\NEscaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\\\NEscaped", formatWithEscaping);
assertTokenEquals(EOF, "character\\NEscaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character\\NEscaped", lexer.nextToken(new Token()));
} }
@ -313,7 +321,7 @@ public class CSVLexerTest {
@Test @Test
@Ignore @Ignore
public void testEscapedCharacter() throws Exception { public void testEscapedCharacter() throws Exception {
final Lexer lexer = getLexer("character\\aEscaped", CSVFormat.newBuilder().withEscape('\\').build()); final Lexer lexer = getLexer("character\\aEscaped", formatWithEscaping);
assertTokenEquals(EOF, "character\\aEscaped", lexer.nextToken(new Token())); assertTokenEquals(EOF, "character\\aEscaped", lexer.nextToken(new Token()));
} }
@ -327,8 +335,8 @@ public class CSVLexerTest {
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void testEscapingAtEOF() throws Exception { public void testEscapingAtEOF() throws Exception {
final String code = "escaping at EOF is evil!"; final String code = "escaping at EOF is evil\\";
final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build()); final Lexer lexer = getLexer(code, formatWithEscaping);
lexer.nextToken(new Token()); lexer.nextToken(new Token());
} }