3434import java .io .IOException ;
3535import java .io .StringReader ;
3636
37+ import org .junit .Before ;
3738import org .junit .Ignore ;
3839import org .junit .Test ;
3940
4445 */
4546public class CSVLexerTest {
4647
48+ private CSVFormat formatWithEscaping ;
49+
50+ @ Before
51+ public void setUp () {
52+ formatWithEscaping = CSVFormat .newBuilder ().withEscape ('\\' ).build ();
53+ }
54+
4755 private Lexer getLexer (final String input , final CSVFormat format ) {
4856 return new CSVLexer (format , new ExtendedBufferedReader (new StringReader (input )));
4957 }
@@ -193,7 +201,7 @@ public void testNextToken3Escaping() throws IOException {
193201 * \,,
194202 */
195203 final String code = "a,\\ ,,b\\ \\ \n \\ ,,\\ \n c,d\\ \r \n e" ;
196- final CSVFormat format = CSVFormat . newBuilder (). withEscape ( '\\' ).withIgnoreEmptyLines (false ).build ();
204+ final CSVFormat format = formatWithEscaping . toBuilder ( ).withIgnoreEmptyLines (false ).build ();
197205 assertTrue (format .isEscaping ());
198206 final Lexer parser = getLexer (code , format );
199207
@@ -274,46 +282,46 @@ public void testDelimiterIsWhitespace() throws IOException {
274282
275283 @ Test
276284 public void testEscapedCR () throws Exception {
277- final Lexer lexer = getLexer ("character\\ " + CR + "Escaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
285+ final Lexer lexer = getLexer ("character\\ " + CR + "Escaped" , formatWithEscaping );
278286 assertTokenEquals (EOF , "character" + CR + "Escaped" , lexer .nextToken (new Token ()));
279287 }
280288
281289 @ Test
282290 public void testEscapedLF () throws Exception {
283- final Lexer lexer = getLexer ("character\\ " + LF + "Escaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
291+ final Lexer lexer = getLexer ("character\\ " + LF + "Escaped" , formatWithEscaping );
284292 assertTokenEquals (EOF , "character" + LF + "Escaped" , lexer .nextToken (new Token ()));
285293 }
286294
287295 @ Test
288296 public void testEscapedTab () throws Exception {
289- final Lexer lexer = getLexer ("character\\ " + TAB + "Escaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
297+ final Lexer lexer = getLexer ("character\\ " + TAB + "Escaped" , formatWithEscaping );
290298 assertTokenEquals (EOF , "character" + TAB + "Escaped" , lexer .nextToken (new Token ()));
291299 }
292300
293301 @ Test
294302 public void testEscapeBackspace () throws Exception {
295- final Lexer lexer = getLexer ("character\\ " + BACKSPACE + "Escaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
303+ final Lexer lexer = getLexer ("character\\ " + BACKSPACE + "Escaped" , formatWithEscaping );
296304 assertTokenEquals (EOF , "character" + BACKSPACE + "Escaped" , lexer .nextToken (new Token ()));
297305 }
298306
299307 @ Test
300308 public void testEscapeFF () throws Exception {
301- final Lexer lexer = getLexer ("character\\ " + FF + "Escaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
309+ final Lexer lexer = getLexer ("character\\ " + FF + "Escaped" , formatWithEscaping );
302310 assertTokenEquals (EOF , "character" + FF + "Escaped" , lexer .nextToken (new Token ()));
303311 }
304312
305313 @ Test
306314 public void testEscapedMySqlNullValue () throws Exception {
307315 // MySQL uses \N to symbolize null values. We have to restore this
308- final Lexer lexer = getLexer ("character\\ \\ NEscaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
316+ final Lexer lexer = getLexer ("character\\ \\ NEscaped" , formatWithEscaping );
309317 assertTokenEquals (EOF , "character\\ NEscaped" , lexer .nextToken (new Token ()));
310318 }
311319
312320 // FIXME this should work after CSV-58 is resolved. Currently the result will be "characteraEscaped"
313321 @ Test
314322 @ Ignore
315323 public void testEscapedCharacter () throws Exception {
316- final Lexer lexer = getLexer ("character\\ aEscaped" , CSVFormat . newBuilder (). withEscape ( '\\' ). build () );
324+ final Lexer lexer = getLexer ("character\\ aEscaped" , formatWithEscaping );
317325 assertTokenEquals (EOF , "character\\ aEscaped" , lexer .nextToken (new Token ()));
318326 }
319327
@@ -327,8 +335,8 @@ public void testEscapedControlCharacter() throws Exception {
327335
328336 @ Test (expected = IOException .class )
329337 public void testEscapingAtEOF () throws Exception {
330- final String code = "escaping at EOF is evil! " ;
331- final Lexer lexer = getLexer (code , CSVFormat . newBuilder (). withEscape ( '!' ). build () );
338+ final String code = "escaping at EOF is evil\\ " ;
339+ final Lexer lexer = getLexer (code , formatWithEscaping );
332340
333341 lexer .nextToken (new Token ());
334342 }
0 commit comments