~ubuntu-branches/ubuntu/quantal/commons-csv/quantal

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/csv/CSVParserTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Jan-Pascal van Best
  • Date: 2007-07-27 09:45:30 UTC
  • Revision ID: james.westby@ubuntu.com-20070727094530-iy6ls22i7yj3p0sg
Tags: upstream-0.1-SNAPSHOT+svn558885
ImportĀ upstreamĀ versionĀ 0.1-SNAPSHOT+svn558885

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.commons.csv;
 
18
 
 
19
import java.io.IOException;
 
20
import java.io.Reader;
 
21
import java.io.StringReader;
 
22
import java.util.Arrays;
 
23
 
 
24
import junit.framework.Test;
 
25
import junit.framework.TestCase;
 
26
import junit.framework.TestSuite;
 
27
 
 
28
/**
 
29
 * CSVParserTest
 
30
 *
 
31
 * The test are organized in three different sections:
 
32
 * The 'setter/getter' section, the lexer section and finally the parser 
 
33
 * section. In case a test fails, you should follow a top-down approach for 
 
34
 * fixing a potential bug (its likely that the parser itself fails if the lexer
 
35
 * has problems...).
 
36
 */
 
37
public class CSVParserTest extends TestCase {
 
38
  
 
39
  /**
 
40
   * TestCSVParser.
 
41
   */
 
42
  class TestCSVParser extends CSVParser {
 
43
    /**
 
44
     * Test parser to investigate the type of the internal Token.
 
45
     * @param in a Reader
 
46
     */
 
47
    TestCSVParser(Reader in) {
 
48
      super(in);
 
49
    }
 
50
    /**
 
51
     * Calls super.nextToken() and prints out a String representation of token
 
52
     * type and content.
 
53
     * @return String representation of token type and content
 
54
     * @throws IOException like {@link CSVParser#nextToken()}
 
55
     */
 
56
    public String testNextToken() throws IOException {
 
57
      Token t = super.nextToken();
 
58
      String tmp = Integer.toString(t.type) + ";" + t.content + ";";
 
59
      System.out.println("token=" + tmp);
 
60
      return tmp;
 
61
    }
 
62
  }
 
63
  
 
64
  /**
 
65
   * Constructor for JUnit.
 
66
   * @param name Name to be used in JUnit Test Environment
 
67
   */
 
68
  public CSVParserTest(String name) {
 
69
    super(name);
 
70
  }
 
71
 
 
72
  /**
 
73
   * Returns a Test suite for JUnit.
 
74
   * @return Test suite for JUnit
 
75
   */
 
76
  public static Test suite() {
 
77
    return new TestSuite(CSVParserTest.class);
 
78
  }
 
79
 
 
80
 
 
81
  // ======================================================
 
82
  //   lexer tests
 
83
  // ======================================================
 
84
  
 
85
  // Single line (without comment)
 
86
  public void testNextToken1() throws IOException {
 
87
    String code = "abc,def, hijk,  lmnop,   qrst,uv ,wxy   ,z , ,";
 
88
    TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
89
    parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
90
    System.out.println("---------\n" + code + "\n-------------");
 
91
    assertEquals(CSVParser.TT_TOKEN + ";abc;", parser.testNextToken());
 
92
    assertEquals(CSVParser.TT_TOKEN + ";def;", parser.testNextToken());
 
93
    assertEquals(CSVParser.TT_TOKEN + ";hijk;", parser.testNextToken());
 
94
    assertEquals(CSVParser.TT_TOKEN + ";lmnop;", parser.testNextToken());
 
95
    assertEquals(CSVParser.TT_TOKEN + ";qrst;", parser.testNextToken());
 
96
    assertEquals(CSVParser.TT_TOKEN + ";uv;", parser.testNextToken());
 
97
    assertEquals(CSVParser.TT_TOKEN + ";wxy;", parser.testNextToken());
 
98
    assertEquals(CSVParser.TT_TOKEN + ";z;", parser.testNextToken());
 
99
    assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
 
100
    assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());  
 
101
  }
 
102
  
 
103
  // multiline including comments (and empty lines)
 
104
  public void testNextToken2() throws IOException {
 
105
    /*   file:   1,2,3,
 
106
     *           a,b x,c
 
107
     *
 
108
     *           # this is a comment 
 
109
     *           d,e,
 
110
     * 
 
111
     */
 
112
    String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n";
 
113
    TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
114
    parser.getStrategy().setIgnoreEmptyLines(false);
 
115
    parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
116
    parser.getStrategy().setCommentStart('#');
 
117
    System.out.println("---------\n" + code + "\n-------------");
 
118
    assertEquals(CSVParser.TT_TOKEN + ";1;", parser.testNextToken());
 
119
    assertEquals(CSVParser.TT_TOKEN + ";2;", parser.testNextToken());
 
120
    assertEquals(CSVParser.TT_TOKEN + ";3;", parser.testNextToken());
 
121
    assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
 
122
    assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
123
    assertEquals(CSVParser.TT_TOKEN + ";b x;", parser.testNextToken());
 
124
    assertEquals(CSVParser.TT_EORECORD + ";c;", parser.testNextToken());
 
125
    assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
 
126
    assertEquals(CSVParser.TT_TOKEN + ";d;", parser.testNextToken());
 
127
    assertEquals(CSVParser.TT_TOKEN + ";e;", parser.testNextToken());
 
128
    assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
 
129
    assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());    
 
130
    assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());    
 
131
    
 
132
  }
 
133
 
 
134
  // simple token with escaping
 
135
  public void testNextToken3() throws IOException {
 
136
    /* file: a,\,,b
 
137
     *       \,,
 
138
     */
 
139
    String code = "a,\\,,b\n\\,,";
 
140
    TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
141
    parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
142
    parser.getStrategy().setCommentStart('#');
 
143
    System.out.println("---------\n" + code + "\n-------------");
 
144
    assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
145
    // an unquoted single backslash is not an escape char
 
146
    assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
 
147
    assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
 
148
    assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
149
    // an unquoted single backslash is not an escape char
 
150
    assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
 
151
    assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
 
152
    assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
 
153
  }
 
154
  
 
155
  // encapsulator tokenizer (sinle line)
 
156
  public void testNextToken4() throws IOException {
 
157
    /* file:  a,"foo",b
 
158
     *        a,   " foo",b
 
159
     *        a,"foo "   ,b     // whitespace after closing encapsulator
 
160
     *        a,  " foo " ,b
 
161
     */ 
 
162
     String code = 
 
163
      "a,\"foo\",b\na,   \" foo\",b\na,\"foo \"  ,b\na,  \" foo \"  ,b";
 
164
     TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
165
     parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
166
     System.out.println("---------\n" + code + "\n-------------");
 
167
     assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
168
     assertEquals(CSVParser.TT_TOKEN + ";foo;", parser.testNextToken());
 
169
     assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
170
     assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
171
     assertEquals(CSVParser.TT_TOKEN + "; foo;", parser.testNextToken());
 
172
     assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
173
     assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
174
     assertEquals(CSVParser.TT_TOKEN + ";foo ;", parser.testNextToken());
 
175
     assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
176
     assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
177
     assertEquals(CSVParser.TT_TOKEN + "; foo ;", parser.testNextToken());
 
178
//     assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
179
     assertEquals(CSVParser.TT_EOF + ";b;", parser.testNextToken());    
 
180
  }
 
181
  
 
182
  // encapsulator tokenizer (multi line, delimiter in string)
 
183
  public void testNextToken5() throws IOException {   
 
184
    String code = 
 
185
      "a,\"foo\n\",b\n\"foo\n  baar ,,,\"\n\"\n\t \n\",\"\\\"\""
 
186
      + ",\"\\,\"" 
 
187
      + ",\"\"\"\"";
 
188
    TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
189
    parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
190
    System.out.println("---------\n" + code + "\n-------------");
 
191
    assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
192
    assertEquals(CSVParser.TT_TOKEN + ";foo\n;", parser.testNextToken());
 
193
    assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
 
194
    assertEquals(CSVParser.TT_EORECORD + ";foo\n  baar ,,,;",
 
195
        parser.testNextToken());
 
196
    assertEquals(CSVParser.TT_TOKEN + ";\n\t \n;", parser.testNextToken());
 
197
    assertEquals(CSVParser.TT_TOKEN + ";\";", parser.testNextToken());
 
198
    // escape char in quoted input only escapes delimiter
 
199
    assertEquals(CSVParser.TT_TOKEN + ";\\,;", parser.testNextToken());
 
200
    assertEquals(CSVParser.TT_EOF + ";\";", parser.testNextToken());
 
201
  }
 
202
  
 
203
  // change delimiters, comment, encapsulater
 
204
  public void testNextToken6() throws IOException {
 
205
    /* file: a;'b and \' more
 
206
     *       '
 
207
     *       !comment;;;;
 
208
     *       ;;
 
209
     */
 
210
    String code = "a;'b and \\' more\n'\n!comment;;;;\n;;";
 
211
    TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
212
    parser.setStrategy( new CSVStrategy(';', '\'', '!') );
 
213
    System.out.println("---------\n" + code + "\n-------------");
 
214
    assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
 
215
    assertEquals(
 
216
      CSVParser.TT_EORECORD + ";b and ' more\n;", 
 
217
      parser.testNextToken());
 
218
  }
 
219
  
 
220
  
 
221
  // ======================================================
 
222
  //   parser tests
 
223
  // ======================================================
 
224
  
 
225
  String code = 
 
226
    "a,b,c,d\n"
 
227
    + " a , b , 1 2 \n"
 
228
    + "\"foo baar\", b,\n"
 
229
    + "   \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
 
230
  String[][] res = { 
 
231
    {"a", "b", "c", "d"},
 
232
    {"a", "b", "1 2"}, 
 
233
    {"foo baar", "b", ""}, 
 
234
    {"foo\n,,\n\",,\n\"", "d", "e"}
 
235
  };
 
236
  public void testGetLine() throws IOException {
 
237
    CSVParser parser = new CSVParser(new StringReader(code));
 
238
    System.out.println("---------\n" + code + "\n-------------");
 
239
    String[] tmp = null;
 
240
    for (int i = 0; i < res.length; i++) {
 
241
      tmp = parser.getLine();
 
242
      assertTrue(Arrays.equals(res[i], tmp));
 
243
    }
 
244
    tmp = parser.getLine();
 
245
    assertTrue(tmp == null);
 
246
  }
 
247
  
 
248
  public void testNextValue() throws IOException {
 
249
    CSVParser parser = new CSVParser(new StringReader(code));
 
250
    System.out.println("---------\n" + code + "\n-------------");
 
251
    String tmp = null;
 
252
    for (int i = 0; i < res.length; i++) {
 
253
      for (int j = 0; j < res[i].length; j++) {
 
254
        tmp = parser.nextValue();
 
255
        assertEquals(res[i][j], tmp);
 
256
      }
 
257
    }
 
258
    tmp = parser.nextValue();
 
259
    assertTrue(tmp == null);    
 
260
  }
 
261
  
 
262
  public void testGetAllValues() throws IOException {
 
263
    CSVParser parser = new CSVParser(new StringReader(code));
 
264
    System.out.println("---------\n" + code + "\n-------------");
 
265
    String[][] tmp = parser.getAllValues();
 
266
    assertEquals(res.length, tmp.length);
 
267
    assertTrue(tmp.length > 0);
 
268
    for (int i = 0; i < res.length; i++) {
 
269
      assertTrue(Arrays.equals(res[i], tmp[i])); 
 
270
    }
 
271
  }
 
272
  
 
273
  public void testExcelStrategy1() throws IOException {
 
274
    String code = 
 
275
      "value1,value2,value3,value4\r\na,b,c,d\r\n  x,,,"
 
276
      + "\r\n\r\n\"\"\"hello\"\"\",\"  \"\"world\"\"\",\"abc\ndef\",\r\n";
 
277
    String[][] res = {
 
278
      {"value1", "value2", "value3", "value4"},
 
279
      {"a", "b", "c", "d"},
 
280
      {"  x", "", "", ""},
 
281
      {""},
 
282
      {"\"hello\"", "  \"world\"", "abc\ndef", ""}
 
283
    };
 
284
    CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
 
285
    System.out.println("---------\n" + code + "\n-------------");
 
286
    String[][] tmp = parser.getAllValues();
 
287
    assertEquals(res.length, tmp.length);
 
288
    assertTrue(tmp.length > 0);
 
289
    for (int i = 0; i < res.length; i++) {
 
290
      assertTrue(Arrays.equals(res[i], tmp[i])); 
 
291
    }
 
292
  }
 
293
  
 
294
  public void testExcelStrategy2() throws Exception {
 
295
    String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
 
296
    String[][] res = {
 
297
      {"foo", "baar"},
 
298
      {""},
 
299
      {"hello", ""},
 
300
      {""},
 
301
      {"world", ""}
 
302
    };
 
303
    CSVParser parser = new CSVParser(new StringReader(code));
 
304
    parser.setStrategy(CSVStrategy.EXCEL_STRATEGY);
 
305
    System.out.println("---------\n" + code + "\n-------------");
 
306
    String[][] tmp = parser.getAllValues();
 
307
    assertEquals(res.length, tmp.length);
 
308
    assertTrue(tmp.length > 0);
 
309
    for (int i = 0; i < res.length; i++) {
 
310
      for (int j = 0; j < tmp[i].length; j++) {
 
311
        System.out.println("'" + tmp[i][j] + "'");
 
312
      }
 
313
      assertTrue(Arrays.equals(res[i], tmp[i])); 
 
314
    }
 
315
  }
 
316
  
 
317
  public void testEndOfFileBehaviourExcel() throws Exception {
 
318
    String[] codes = {
 
319
        "hello,\r\n\r\nworld,\r\n",
 
320
        "hello,\r\n\r\nworld,",
 
321
        "hello,\r\n\r\nworld,\"\"\r\n",
 
322
        "hello,\r\n\r\nworld,\"\"",
 
323
        "hello,\r\n\r\nworld,\n",
 
324
        "hello,\r\n\r\nworld,",
 
325
        "hello,\r\n\r\nworld,\"\"\n",
 
326
        "hello,\r\n\r\nworld,\"\""
 
327
        };
 
328
    String[][] res = {
 
329
      {"hello", ""},
 
330
      {""},  // ExcelStrategy does not ignore empty lines
 
331
      {"world", ""}
 
332
    };
 
333
    String code;
 
334
    for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
 
335
      code = codes[codeIndex];
 
336
      CSVParser parser = new CSVParser(new StringReader(code));
 
337
      parser.setStrategy(CSVStrategy.EXCEL_STRATEGY);
 
338
      System.out.println("---------\n" + code + "\n-------------");
 
339
      String[][] tmp = parser.getAllValues();
 
340
      assertEquals(res.length, tmp.length);
 
341
      assertTrue(tmp.length > 0);
 
342
      for (int i = 0; i < res.length; i++) {
 
343
        for (int j = 0; j < tmp[i].length; j++) {
 
344
          System.out.println("'" + tmp[i][j] + "'");
 
345
        }
 
346
        assertTrue(Arrays.equals(res[i], tmp[i]));
 
347
      }
 
348
    }
 
349
  }
 
350
  
 
351
  public void testEndOfFileBehaviorCSV() throws Exception {
 
352
    String[] codes = {
 
353
        "hello,\r\n\r\nworld,\r\n",
 
354
        "hello,\r\n\r\nworld,",
 
355
        "hello,\r\n\r\nworld,\"\"\r\n",
 
356
        "hello,\r\n\r\nworld,\"\"",
 
357
        "hello,\r\n\r\nworld,\n",
 
358
        "hello,\r\n\r\nworld,",
 
359
        "hello,\r\n\r\nworld,\"\"\n",
 
360
        "hello,\r\n\r\nworld,\"\""
 
361
        };
 
362
    String[][] res = {
 
363
      {"hello", ""},  // CSV Strategy ignores empty lines
 
364
      {"world", ""}
 
365
    };
 
366
    String code;
 
367
    for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
 
368
      code = codes[codeIndex];
 
369
      CSVParser parser = new CSVParser(new StringReader(code));
 
370
      parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
371
      System.out.println("---------\n" + code + "\n-------------");
 
372
      String[][] tmp = parser.getAllValues();
 
373
      assertEquals(res.length, tmp.length);
 
374
      assertTrue(tmp.length > 0);
 
375
      for (int i = 0; i < res.length; i++) {
 
376
        for (int j = 0; j < tmp[i].length; j++) {
 
377
          System.out.println("'" + tmp[i][j] + "'");
 
378
        }
 
379
        assertTrue(Arrays.equals(res[i], tmp[i]));
 
380
      }
 
381
    }
 
382
  }
 
383
  
 
384
  public void testEmptyLineBehaviourExcel() throws Exception {
 
385
    String[] codes = {
 
386
        "hello,\r\n\r\n\r\n",
 
387
        "hello,\n\n\n",
 
388
        "hello,\"\"\r\n\r\n\r\n",
 
389
        "hello,\"\"\n\n\n"
 
390
        };
 
391
    String[][] res = {
 
392
      {"hello", ""},
 
393
      {""},  // ExcelStrategy does not ignore empty lines
 
394
      {""}
 
395
    };
 
396
    String code;
 
397
    for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
 
398
      code = codes[codeIndex];
 
399
      CSVParser parser = new CSVParser(new StringReader(code));
 
400
      parser.setStrategy(CSVStrategy.EXCEL_STRATEGY);
 
401
      System.out.println("---------\n" + code + "\n-------------");
 
402
      String[][] tmp = parser.getAllValues();
 
403
      assertEquals(res.length, tmp.length);
 
404
      assertTrue(tmp.length > 0);
 
405
      for (int i = 0; i < res.length; i++) {
 
406
        for (int j = 0; j < tmp[i].length; j++) {
 
407
          System.out.println("'" + tmp[i][j] + "'");
 
408
        }
 
409
        assertTrue(Arrays.equals(res[i], tmp[i]));
 
410
      }
 
411
    }
 
412
  }
 
413
  
 
414
  public void testEmptyLineBehaviourCSV() throws Exception {
 
415
    String[] codes = {
 
416
        "hello,\r\n\r\n\r\n",
 
417
        "hello,\n\n\n",
 
418
        "hello,\"\"\r\n\r\n\r\n",
 
419
        "hello,\"\"\n\n\n"
 
420
        };
 
421
    String[][] res = {
 
422
      {"hello", ""}  // CSV Strategy ignores empty lines
 
423
    };
 
424
    String code;
 
425
    for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
 
426
      code = codes[codeIndex];
 
427
      CSVParser parser = new CSVParser(new StringReader(code));
 
428
      parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
 
429
      System.out.println("---------\n" + code + "\n-------------");
 
430
      String[][] tmp = parser.getAllValues();
 
431
      assertEquals(res.length, tmp.length);
 
432
      assertTrue(tmp.length > 0);
 
433
      for (int i = 0; i < res.length; i++) {
 
434
        for (int j = 0; j < tmp[i].length; j++) {
 
435
          System.out.println("'" + tmp[i][j] + "'");
 
436
        }
 
437
        assertTrue(Arrays.equals(res[i], tmp[i]));
 
438
      }
 
439
    }
 
440
  }
 
441
  
 
442
  public void testBackslashEscaping() throws IOException {
 
443
    String code =
 
444
      "one,two,three\n"
 
445
      + "on\\\"e,two\n"
 
446
      + "on\"e,two\n"
 
447
      + "one,\"tw\\\"o\"\n"
 
448
      + "one,\"t\\,wo\"\n"
 
449
      + "one,two,\"th,ree\"\n"
 
450
      + "\"a\\\\\"\n"
 
451
      + "a\\,b\n"
 
452
      + "\"a\\\\,b\"";
 
453
    String[][] res = {
 
454
        { "one", "two", "three" },
 
455
        { "on\\\"e", "two" },
 
456
        { "on\"e", "two" },
 
457
        { "one", "tw\"o" },
 
458
        { "one", "t\\,wo" },  // backslash in quotes only escapes a delimiter (",")
 
459
        { "one", "two", "th,ree" },
 
460
        { "a\\\\" },     // backslash in quotes only escapes a delimiter (",")
 
461
        { "a\\", "b" },  // a backslash must be returnd 
 
462
        { "a\\\\,b" }    // backslash in quotes only escapes a delimiter (",")
 
463
      };
 
464
    CSVParser parser = new CSVParser(new StringReader(code));
 
465
    System.out.println("---------\n" + code + "\n-------------");
 
466
    String[][] tmp = parser.getAllValues();
 
467
    assertEquals(res.length, tmp.length);
 
468
    assertTrue(tmp.length > 0);
 
469
    for (int i = 0; i < res.length; i++) {
 
470
      for (int j = 0; j < tmp[i].length; j++) {
 
471
        System.out.println("'" + tmp[i][j] + "'");
 
472
      }
 
473
      assertTrue(Arrays.equals(res[i], tmp[i])); 
 
474
    }
 
475
  }
 
476
  
 
477
    public void testUnicodeEscape() throws IOException {
 
478
      String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
 
479
      CSVParser parser = new CSVParser(new StringReader(code));
 
480
      System.out.println("---------\n" + code + "\n-------------");
 
481
      parser.getStrategy().setUnicodeEscapeInterpretation(true);
 
482
      String[] data = parser.getLine();
 
483
      assertEquals(2, data.length);
 
484
      assertEquals("abc", data[0]);
 
485
      assertEquals("public", data[1]);
 
486
    }
 
487
    
 
488
    public void testCarriageReturnLineFeedEndings() throws IOException {
 
489
     String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
 
490
     CSVParser parser = new CSVParser(new StringReader(code));
 
491
     System.out.println("---------\n" + code + "\n-------------");
 
492
     String[][] data = parser.getAllValues();
 
493
     assertEquals(4, data.length);
 
494
    }
 
495
    
 
496
    public void testIgnoreEmptyLines() throws IOException {
 
497
      String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
 
498
      //String code = "world\r\n\n";
 
499
      //String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
 
500
      CSVParser parser = new CSVParser(new StringReader(code));
 
501
      System.out.println("---------\n" + code + "\n-------------");
 
502
      String[][] data = parser.getAllValues();
 
503
//      for (int i = 0; i < data.length; i++) {
 
504
//        if (i > 0) {
 
505
//          System.out.print('\n');
 
506
//        }
 
507
//        for (int j = 0; j < data[i].length; j++) {
 
508
//          System.out.print("(" + j + ")'" + data[i][j] + "'");
 
509
//        }
 
510
//      }
 
511
//      System.out.println("----------");
 
512
      assertEquals(3, data.length);
 
513
    }
 
514
    
 
515
    public void testLineTokenConsistency() throws IOException {
 
516
      String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
 
517
      CSVParser parser = new CSVParser(new StringReader(code));
 
518
      System.out.println("---------\n" + code + "\n-------------");
 
519
      String[][] data = parser.getAllValues();
 
520
      parser = new CSVParser(new StringReader(code));
 
521
      CSVParser parser1 = new CSVParser(new StringReader(code));
 
522
      for (int i = 0; i < data.length; i++) {
 
523
        assertTrue(Arrays.equals(parser1.getLine(), data[i]));
 
524
        for (int j = 0; j < data[i].length; j++) {
 
525
          assertEquals(parser.nextValue(), data[i][j]);
 
526
        }
 
527
      }
 
528
    }
 
529
 
 
530
    // From SANDBOX-153
 
531
     public void testDelimiterIsWhitespace() throws IOException {
 
532
         String code = "one\ttwo\t\tfour \t five\t six";
 
533
         TestCSVParser parser = new TestCSVParser(new StringReader(code));
 
534
         parser.setStrategy(CSVStrategy.TDF_STRATEGY);
 
535
         System.out.println("---------\n" + code + "\n-------------");
 
536
         assertEquals(CSVParser.TT_TOKEN + ";one;", parser.testNextToken());
 
537
         assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken());
 
538
         assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
 
539
         assertEquals(CSVParser.TT_TOKEN + ";four;", parser.testNextToken());
 
540
         assertEquals(CSVParser.TT_TOKEN + ";five;", parser.testNextToken());
 
541
         assertEquals(CSVParser.TT_EOF + ";six;", parser.testNextToken());
 
542
     }
 
543
}