001: package org.apache.turbine.util.parser;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.InputStreamReader;
026: import java.io.OutputStreamWriter;
027: import java.io.Reader;
028: import java.io.StringReader;
029: import java.util.Arrays;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.NoSuchElementException;
033:
034: import junit.framework.TestSuite;
035:
036: import org.apache.commons.lang.StringUtils;
037: import org.apache.turbine.test.BaseTurbineTest;
038:
039: /**
040: * test whether the Default parameter parser returns its uploaded file items
041: * in the keySet().
042: *
043: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
044: * @version $Id: CSVParserTest.java 534527 2007-05-02 16:10:59Z tv $
045: */
046:
047: public class CSVParserTest extends BaseTurbineTest {
048: public CSVParserTest(String name) throws Exception {
049: super (name, "conf/test/TurbineResources.properties");
050: }
051:
052: public static TestSuite suite() {
053: return new TestSuite(CSVParserTest.class);
054: }
055:
056: public void testSimpleReader() {
057: String readLine = "0,1,2,3,4,5,6,7,8,9\n";
058:
059: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
060: "sechs", "sieben", "acht", "neun", "null", };
061: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
062:
063: Reader myReader = new StringReader(readLine);
064:
065: List fieldNames = Arrays.asList(fields);
066:
067: DataStreamParser p = new CSVParser(myReader, fieldNames,
068: "UTF-8");
069:
070: assertTrue(p.hasNext());
071:
072: ValueParser vp = (ValueParser) p.next();
073:
074: assertEquals("Wrong character encoding set", "UTF-8", vp
075: .getCharacterEncoding());
076:
077: assertFalse(p.hasNext());
078:
079: for (int i = 0; i < fields.length; i++) {
080: assertEquals("Value does not match", Integer
081: .toString(values[i]), vp.getString(fields[i]));
082: assertEquals("Value does not match", values[i], vp
083: .getInt(fields[i]));
084: }
085: }
086:
087: public void testMultipleLines() {
088: String readLine = "0,1,2,3,4,5,6,7,8,9\n"
089: + "10,11,12,13,14,15,16,17,18,19\n"
090: + "20,21,22,23,24,25,26,27,28,29\n"
091: + "30,31,32,33,34,35,36,37,38,39\n"
092: + "40,41,42,43,44,45,46,47,48,49\n"
093: + "50,51,52,53,54,55,56,57,58,59\n";
094:
095: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
096: "sechs", "sieben", "acht", "neun", "null", };
097: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
098:
099: Reader myReader = new StringReader(readLine);
100:
101: List fieldNames = Arrays.asList(fields);
102:
103: DataStreamParser p = new CSVParser(myReader, fieldNames,
104: "UTF-8");
105:
106: int cnt = 0;
107:
108: while (p.hasNext()) {
109: ValueParser vp = (ValueParser) p.next();
110:
111: for (int i = 0; i < fields.length; i++) {
112: assertEquals("Value does not match", Integer
113: .toString(values[i]), vp.getString(fields[i]));
114: assertEquals("Value does not match", values[i], vp
115: .getInt(fields[i]));
116: values[i] += 10;
117: }
118: cnt++;
119: }
120:
121: assertEquals("Wrong number of lines found", 6, cnt);
122: }
123:
124: public void testPadding() {
125: String[] strValues = { " 0", " 1 ", "2 ", " 3 ", " 4",
126: " 5 ", " 6", " 7 ", "8", " 9 " };
127:
128: String readLine = StringUtils.join(strValues, ',') + "\n";
129:
130: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
131: "sechs", "sieben", "acht", "neun", "null", };
132: int[] intValues = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
133:
134: Reader myReader = new StringReader(readLine);
135:
136: List fieldNames = Arrays.asList(fields);
137:
138: DataStreamParser p = new CSVParser(myReader, fieldNames,
139: "UTF-8");
140:
141: assertTrue(p.hasNext());
142:
143: ValueParser vp = (ValueParser) p.next();
144:
145: assertFalse(p.hasNext());
146:
147: for (int i = 0; i < fields.length; i++) {
148: assertEquals("Value does not match", strValues[i], vp
149: .getString(fields[i]));
150: assertEquals("Value does not match", intValues[i], vp
151: .getInt(fields[i]));
152: }
153: }
154:
155: public void testMissing() {
156: String[] strValues = { "100", "", "300", "", "500", "", "700",
157: "", "900", "" };
158:
159: String readLine = StringUtils.join(strValues, ',') + "\n";
160:
161: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
162: "sechs", "sieben", "acht", "neun", "null", };
163: int[] intValues = { 100, 200, 300, 400, 500, 600, 700, 800,
164: 900, 0 };
165:
166: Reader myReader = new StringReader(readLine);
167:
168: List fieldNames = Arrays.asList(fields);
169:
170: DataStreamParser p = new CSVParser(myReader, fieldNames,
171: "UTF-8");
172:
173: assertTrue(p.hasNext());
174:
175: ValueParser vp = (ValueParser) p.next();
176:
177: assertFalse(p.hasNext());
178:
179: for (int i = 0; i < fields.length; i++) {
180: assertEquals("Value does not match", strValues[i], vp
181: .getString(fields[i]));
182:
183: if ((i % 2) == 0) {
184: assertEquals("Value does not match", intValues[i], vp
185: .getInt(fields[i]));
186: }
187: }
188: }
189:
190: public void testEmpty() {
191: String readLine = "\n";
192:
193: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
194: "sechs", "sieben", "acht", "neun", "null", };
195:
196: Reader myReader = new StringReader(readLine);
197:
198: List fieldNames = Arrays.asList(fields);
199:
200: DataStreamParser p = new CSVParser(myReader, fieldNames,
201: "UTF-8");
202:
203: assertTrue(p.hasNext());
204:
205: ValueParser vp = (ValueParser) p.next();
206:
207: assertFalse(p.hasNext());
208:
209: for (int i = 0; i < fields.length; i++) {
210: assertEquals("Value does not match", "", vp
211: .getString(fields[i]));
212: }
213: }
214:
215: public void testEOF() {
216: String readLine = "0,1,2,3,4,5,6,7,8,9";
217:
218: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
219: "sechs", "sieben", "acht", "neun", "null", };
220: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
221:
222: Reader myReader = new StringReader(readLine);
223:
224: List fieldNames = Arrays.asList(fields);
225:
226: DataStreamParser p = new CSVParser(myReader, fieldNames,
227: "UTF-8");
228:
229: assertTrue(p.hasNext());
230:
231: ValueParser vp = (ValueParser) p.next();
232:
233: assertFalse(p.hasNext());
234:
235: for (int i = 0; i < fields.length; i++) {
236: assertEquals("Value does not match", Integer
237: .toString(values[i]), vp.getString(fields[i]));
238: assertEquals("Value does not match", values[i], vp
239: .getInt(fields[i]));
240: }
241: }
242:
243: public void testLessFields() {
244: String readLine = "0,1,2,3,4,5,6,7,8,9";
245:
246: String[] fields = { "eins", "zwei", "drei" };
247: int[] values = { 0, 1, 2, };
248:
249: Reader myReader = new StringReader(readLine);
250:
251: List fieldNames = Arrays.asList(fields);
252:
253: DataStreamParser p = new CSVParser(myReader, fieldNames,
254: "UTF-8");
255:
256: assertTrue(p.hasNext());
257:
258: ValueParser vp = (ValueParser) p.next();
259:
260: assertFalse(p.hasNext());
261:
262: for (int i = 0; i < fields.length; i++) {
263: assertEquals("Value does not match", Integer
264: .toString(values[i]), vp.getString(fields[i]));
265: assertEquals("Value does not match", values[i], vp
266: .getInt(fields[i]));
267: }
268: }
269:
270: public void testLessFieldsMultipleLines() {
271: String readLine = "0,1,2,3,4,5,6,7,8,9\n"
272: + "10,11,12,13,14,15,16,17,18,19\n"
273: + "20,21,22,23,24,25,26,27,28,29\n"
274: + "30,31,32,33,34,35,36,37,38,39\n"
275: + "40,41,42,43,44,45,46,47,48,49\n"
276: + "50,51,52,53,54,55,56,57,58,59\n";
277:
278: String[] fields = { "eins", "zwei", "drei", };
279: int[] values = { 0, 1, 2, };
280:
281: Reader myReader = new StringReader(readLine);
282:
283: List fieldNames = Arrays.asList(fields);
284:
285: DataStreamParser p = new CSVParser(myReader, fieldNames,
286: "UTF-8");
287:
288: int cnt = 0;
289:
290: while (p.hasNext()) {
291: ValueParser vp = (ValueParser) p.next();
292:
293: for (int i = 0; i < fields.length; i++) {
294: assertEquals("Value does not match", Integer
295: .toString(values[i]), vp.getString(fields[i]));
296: assertEquals("Value does not match", values[i], vp
297: .getInt(fields[i]));
298: values[i] += 10;
299: }
300: cnt++;
301: }
302:
303: assertEquals("Wrong number of lines found", 6, cnt);
304: }
305:
306: public void testMoreFields() {
307: String readLine = "0,1,2,3\n";
308:
309: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
310: "sechs", "sieben", "acht", "neun", "null", };
311: int[] values = { 0, 1, 2, 3 };
312:
313: Reader myReader = new StringReader(readLine);
314:
315: List fieldNames = Arrays.asList(fields);
316:
317: DataStreamParser p = new CSVParser(myReader, fieldNames,
318: "UTF-8");
319:
320: assertTrue(p.hasNext());
321:
322: ValueParser vp = (ValueParser) p.next();
323:
324: assertFalse(p.hasNext());
325:
326: for (int i = 0; i < values.length; i++) {
327: assertEquals("Value does not match", Integer
328: .toString(values[i]), vp.getString(fields[i]));
329: assertEquals("Value does not match", values[i], vp
330: .getInt(fields[i]));
331: }
332:
333: for (int i = values.length; i < fields.length; i++) {
334: assertEquals("Value does not match", "", vp
335: .getString(fields[i]));
336: }
337: }
338:
339: public void testMoreFieldsMultipleLines() {
340: String readLine = "0,1,2,3\n" + "10,11,12,13\n"
341: + "20,21,22,23\n" + "30,31,32,33\n" + "40,41,42,43\n"
342: + "50,51,52,53\n";
343:
344: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
345: "sechs", "sieben", "acht", "neun", "null", };
346: int[] values = { 0, 1, 2, 3 };
347:
348: Reader myReader = new StringReader(readLine);
349:
350: List fieldNames = Arrays.asList(fields);
351:
352: DataStreamParser p = new CSVParser(myReader, fieldNames,
353: "UTF-8");
354:
355: int cnt = 0;
356:
357: while (p.hasNext()) {
358: ValueParser vp = (ValueParser) p.next();
359:
360: for (int i = 0; i < values.length; i++) {
361: assertEquals("Value does not match", Integer
362: .toString(values[i]), vp.getString(fields[i]));
363: assertEquals("Value does not match", values[i], vp
364: .getInt(fields[i]));
365: values[i] += 10;
366: }
367: for (int i = values.length; i < fields.length; i++) {
368: assertEquals("Value does not match", "", vp
369: .getString(fields[i]));
370: }
371: cnt++;
372: }
373:
374: assertEquals("Wrong number of lines found", 6, cnt);
375: }
376:
377: public void testQuotedReader() {
378: String readLine = "\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"\n";
379:
380: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
381: "sechs", "sieben", "acht", "neun", "null", };
382: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
383:
384: Reader myReader = new StringReader(readLine);
385:
386: List fieldNames = Arrays.asList(fields);
387:
388: DataStreamParser p = new CSVParser(myReader, fieldNames,
389: "UTF-8");
390:
391: assertTrue(p.hasNext());
392:
393: ValueParser vp = (ValueParser) p.next();
394:
395: assertFalse(p.hasNext());
396:
397: for (int i = 0; i < fields.length; i++) {
398: assertEquals("Value does not match", Integer
399: .toString(values[i]), vp.getString(fields[i]));
400: assertEquals("Value does not match", values[i], vp
401: .getInt(fields[i]));
402: }
403: }
404:
405: public void testMissingTrailingQuoteEOL() {
406: String readLine = "\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\n";
407:
408: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
409: "sechs", "sieben", "acht", "neun", "null", };
410: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
411:
412: Reader myReader = new StringReader(readLine);
413:
414: List fieldNames = Arrays.asList(fields);
415:
416: DataStreamParser p = new CSVParser(myReader, fieldNames,
417: "UTF-8");
418:
419: assertTrue(p.hasNext());
420:
421: ValueParser vp = (ValueParser) p.next();
422:
423: assertFalse(p.hasNext());
424:
425: for (int i = 0; i < fields.length; i++) {
426: assertEquals("Value does not match", Integer
427: .toString(values[i]), vp.getString(fields[i]));
428: assertEquals("Value does not match", values[i], vp
429: .getInt(fields[i]));
430: }
431: }
432:
433: public void testMissingTrailingQuoteEOF() {
434: String readLine = "\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9";
435:
436: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
437: "sechs", "sieben", "acht", "neun", "null", };
438: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
439:
440: Reader myReader = new StringReader(readLine);
441:
442: List fieldNames = Arrays.asList(fields);
443:
444: DataStreamParser p = new CSVParser(myReader, fieldNames,
445: "UTF-8");
446:
447: assertTrue(p.hasNext());
448:
449: ValueParser vp = (ValueParser) p.next();
450:
451: assertFalse(p.hasNext());
452:
453: for (int i = 0; i < fields.length; i++) {
454: assertEquals("Value does not match", Integer
455: .toString(values[i]), vp.getString(fields[i]));
456: assertEquals("Value does not match", values[i], vp
457: .getInt(fields[i]));
458: }
459: }
460:
461: public void testQuotedSeparator() {
462: String[] strValues = { "0,1,2,3,4,5,6,7,8,9", "1", "2", "3",
463: "4", "5", "6", "7", "8", "9" };
464:
465: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
466: "sechs", "sieben", "acht", "neun", "null", };
467:
468: String readLine = "\"" + StringUtils.join(strValues, "\",\"")
469: + "\"\n";
470:
471: Reader myReader = new StringReader(readLine);
472:
473: List fieldNames = Arrays.asList(fields);
474:
475: DataStreamParser p = new CSVParser(myReader, fieldNames,
476: "UTF-8");
477:
478: assertTrue(p.hasNext());
479:
480: ValueParser vp = (ValueParser) p.next();
481:
482: assertFalse(p.hasNext());
483:
484: for (int i = 0; i < fields.length; i++) {
485: assertEquals("Value does not match", strValues[i], vp
486: .getString(fields[i]));
487: }
488: }
489:
490: public void testQuotedQuote() {
491: String[] strValues = { "\\\"", };
492:
493: String[] fields = { "eins", };
494:
495: String readLine = "\"" + StringUtils.join(strValues, "\",\"")
496: + "\"\n";
497:
498: Reader myReader = new StringReader(readLine);
499:
500: List fieldNames = Arrays.asList(fields);
501:
502: DataStreamParser p = new CSVParser(myReader, fieldNames,
503: "UTF-8");
504:
505: assertTrue(p.hasNext());
506:
507: ValueParser vp = (ValueParser) p.next();
508:
509: assertFalse(p.hasNext());
510: assertEquals("Value does not match", "\"", vp
511: .getString(fields[0]));
512: }
513:
514: public void testExceptions() {
515: String readLine = "0,1,2,3,4,5,6,7,8,9\n";
516:
517: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
518: "sechs", "sieben", "acht", "neun", "null", };
519:
520: Reader myReader = new StringReader(readLine);
521:
522: List fieldNames = Arrays.asList(fields);
523:
524: DataStreamParser p = new CSVParser(myReader, fieldNames,
525: "UTF-8");
526:
527: assertTrue(p.hasNext());
528:
529: p.next();
530:
531: try {
532: p.remove();
533: fail("remove() succeeded!");
534: } catch (Exception e) {
535: assertEquals("Wrong Exception thrown",
536: UnsupportedOperationException.class, e.getClass());
537: }
538:
539: assertFalse(p.hasNext());
540:
541: try {
542: p.next();
543: fail("next() succeeded!");
544: } catch (Exception e) {
545: assertEquals("Wrong Exception thrown",
546: NoSuchElementException.class, e.getClass());
547: }
548: }
549:
550: public void testEncodingFromReader() {
551: String readLine = "0,1,2,3,4,5,6,7,8,9\n";
552:
553: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
554: "sechs", "sieben", "acht", "neun", "null", };
555: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
556:
557: Reader myReader = new StringReader(readLine);
558:
559: List fieldNames = Arrays.asList(fields);
560:
561: DataStreamParser p = new CSVParser(myReader, fieldNames, null);
562:
563: assertTrue(p.hasNext());
564:
565: ValueParser vp = (ValueParser) p.next();
566:
567: assertEquals("Wrong character encoding set", "US-ASCII", vp
568: .getCharacterEncoding());
569:
570: assertFalse(p.hasNext());
571:
572: for (int i = 0; i < fields.length; i++) {
573: assertEquals("Value does not match", Integer
574: .toString(values[i]), vp.getString(fields[i]));
575: assertEquals("Value does not match", values[i], vp
576: .getInt(fields[i]));
577: }
578: }
579:
580: public void testEncodingFromStream() throws Exception {
581: String readLine = "0,1,2,3,4,5,6,7,8,9\n";
582:
583: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
584: "sechs", "sieben", "acht", "neun", "null", };
585: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
586:
587: File myFile = File.createTempFile("turbine", null, null);
588:
589: assertNotNull(myFile);
590:
591: myFile.deleteOnExit();
592:
593: FileOutputStream fos = new FileOutputStream(myFile);
594:
595: assertNotNull(fos);
596:
597: OutputStreamWriter osw = new OutputStreamWriter(fos,
598: "ISO-8859-1");
599: assertNotNull(osw);
600:
601: osw.write(readLine, 0, readLine.length());
602: osw.flush();
603: fos.flush();
604: osw.close();
605: fos.close();
606:
607: FileInputStream fis = new FileInputStream(myFile);
608: assertNotNull(fis);
609:
610: InputStreamReader myReader = new InputStreamReader(fis,
611: "ISO-8859-1");
612: assertNotNull(myReader);
613:
614: List fieldNames = Arrays.asList(fields);
615:
616: DataStreamParser p = new CSVParser(myReader, fieldNames, null);
617:
618: assertTrue(p.hasNext());
619:
620: ValueParser vp = (ValueParser) p.next();
621:
622: assertEquals("Wrong character encoding set", myReader
623: .getEncoding(), vp.getCharacterEncoding());
624:
625: assertFalse(p.hasNext());
626:
627: for (int i = 0; i < fields.length; i++) {
628: assertEquals("Value does not match", Integer
629: .toString(values[i]), vp.getString(fields[i]));
630: assertEquals("Value does not match", values[i], vp
631: .getInt(fields[i]));
632: }
633: }
634:
635: public void testSetColumnNames() {
636: String readLine = "0,1,2,3,4,5,6,7,8,9\n";
637:
638: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
639: "sechs", "sieben", "acht", "neun", "null", };
640: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
641:
642: Reader myReader = new StringReader(readLine);
643:
644: DataStreamParser p = new CSVParser(myReader, null, null);
645:
646: assertTrue(p.hasNext());
647:
648: p.setColumnNames(Arrays.asList(fields));
649:
650: ValueParser vp = (ValueParser) p.next();
651:
652: assertEquals("Wrong character encoding set", "US-ASCII", vp
653: .getCharacterEncoding());
654:
655: assertFalse(p.hasNext());
656:
657: for (int i = 0; i < fields.length; i++) {
658: assertEquals("Value does not match", Integer
659: .toString(values[i]), vp.getString(fields[i]));
660: assertEquals("Value does not match", values[i], vp
661: .getInt(fields[i]));
662: }
663: }
664:
665: public void testEmptyColumnNames() throws Exception {
666: String readLine = "eins,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,null\n"
667: + "0,1,2,3,4,5,6,7,8,9\n";
668:
669: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
670: "sechs", "sieben", "acht", "neun", "null", };
671:
672: Reader myReader = new StringReader(readLine);
673:
674: List fieldNames = Arrays.asList(fields);
675:
676: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
677:
678: assertNotNull(p.getColumnNames());
679: assertEquals("Number of columns is not 0", 0, p
680: .getColumnNames().size());
681:
682: p.setColumnNames(fieldNames);
683: assertNotNull(p.getColumnNames());
684: assertEquals(
685: "Number of columns is not the number of set fields",
686: fieldNames.size(), p.getColumnNames().size());
687:
688: int cnt = 0;
689: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
690: assertEquals("Column name does not match", ((String) it
691: .next()), fields[cnt++]);
692: }
693: }
694:
695: public void testReadColumnNames() throws Exception {
696: String readLine = "eins,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,null\n"
697: + "0,1,2,3,4,5,6,7,8,9\n";
698:
699: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
700: "sechs", "sieben", "acht", "neun", "null", };
701: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
702:
703: Reader myReader = new StringReader(readLine);
704:
705: List fieldNames = Arrays.asList(fields);
706:
707: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
708:
709: assertTrue(p.hasNext());
710:
711: p.readColumnNames();
712:
713: assertEquals(
714: "Number of columns is not the number of set fields",
715: fieldNames.size(), p.getColumnNames().size());
716: int cnt = 0;
717: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
718: assertEquals("Column name does not match", ((String) it
719: .next()), fields[cnt++]);
720: }
721:
722: assertTrue(p.hasNext());
723:
724: ValueParser vp = (ValueParser) p.next();
725:
726: assertEquals("Wrong character encoding set", "UTF-8", vp
727: .getCharacterEncoding());
728:
729: assertFalse(p.hasNext());
730:
731: for (int i = 0; i < fields.length; i++) {
732: assertEquals("Value does not match", Integer
733: .toString(values[i]), vp.getString(fields[i]));
734: assertEquals("Value does not match", values[i], vp
735: .getInt(fields[i]));
736: }
737: }
738:
739: public void testJustReadColumnNames() throws Exception {
740: String readLine = "eins,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,null\n";
741:
742: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
743: "sechs", "sieben", "acht", "neun", "null", };
744:
745: Reader myReader = new StringReader(readLine);
746:
747: List fieldNames = Arrays.asList(fields);
748:
749: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
750:
751: assertTrue(p.hasNext());
752:
753: p.readColumnNames();
754:
755: assertEquals(
756: "Number of columns is not the number of set fields",
757: fieldNames.size(), p.getColumnNames().size());
758: int cnt = 0;
759: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
760: assertEquals("Column name does not match", ((String) it
761: .next()), fields[cnt++]);
762: }
763:
764: assertFalse(p.hasNext());
765: }
766:
767: public void testJustReadColumnNamesEOF() throws Exception {
768: String readLine = "eins,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,null";
769:
770: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
771: "sechs", "sieben", "acht", "neun", "null", };
772:
773: Reader myReader = new StringReader(readLine);
774:
775: List fieldNames = Arrays.asList(fields);
776:
777: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
778:
779: assertTrue(p.hasNext());
780:
781: p.readColumnNames();
782:
783: assertEquals(
784: "Number of columns is not the number of set fields",
785: fieldNames.size(), p.getColumnNames().size());
786: int cnt = 0;
787: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
788: assertEquals("Column name does not match", ((String) it
789: .next()), fields[cnt++]);
790: }
791:
792: assertFalse(p.hasNext());
793: }
794:
795: public void testJustReadColumnNamesQuoted() throws Exception {
796: String readLine = "\"eins\",\"zwei\",\"drei\",\"vier\",\"fuenf\",\"sechs\",\"sieben\",\"acht\",\"neun\",\"null\"\n";
797:
798: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
799: "sechs", "sieben", "acht", "neun", "null", };
800:
801: Reader myReader = new StringReader(readLine);
802:
803: List fieldNames = Arrays.asList(fields);
804:
805: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
806:
807: assertTrue(p.hasNext());
808:
809: p.readColumnNames();
810:
811: assertEquals(
812: "Number of columns is not the number of set fields",
813: fieldNames.size(), p.getColumnNames().size());
814: int cnt = 0;
815: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
816: assertEquals("Column name does not match", ((String) it
817: .next()), fields[cnt++]);
818: }
819:
820: assertFalse(p.hasNext());
821: }
822:
823: public void testJustReadColumnNamesQuotedEOF() throws Exception {
824: String readLine = "\"eins\",\"zwei\",\"drei\",\"vier\",\"fuenf\",\"sechs\",\"sieben\",\"acht\",\"neun\",\"null\"";
825:
826: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
827: "sechs", "sieben", "acht", "neun", "null", };
828:
829: Reader myReader = new StringReader(readLine);
830:
831: List fieldNames = Arrays.asList(fields);
832:
833: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
834:
835: assertTrue(p.hasNext());
836:
837: p.readColumnNames();
838:
839: assertEquals(
840: "Number of columns is not the number of set fields",
841: fieldNames.size(), p.getColumnNames().size());
842: int cnt = 0;
843: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
844: assertEquals("Column name does not match", ((String) it
845: .next()), fields[cnt++]);
846: }
847:
848: assertFalse(p.hasNext());
849: }
850:
851: public void testJustReadColumnNamesQuotedMissing() throws Exception {
852: String readLine = "\"eins\",\"zwei\",\"drei\",\"vier\",\"fuenf\",\"sechs\",\"sieben\",\"acht\",\"neun\",\"null\n";
853:
854: String[] fields = { "eins", "zwei", "drei", "vier", "fuenf",
855: "sechs", "sieben", "acht", "neun", "null", };
856:
857: Reader myReader = new StringReader(readLine);
858:
859: List fieldNames = Arrays.asList(fields);
860:
861: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
862:
863: assertTrue(p.hasNext());
864:
865: p.readColumnNames();
866:
867: assertEquals(
868: "Number of columns is not the number of set fields",
869: fieldNames.size(), p.getColumnNames().size());
870: int cnt = 0;
871: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
872: assertEquals("Column name does not match", ((String) it
873: .next()), fields[cnt++]);
874: }
875:
876: assertFalse(p.hasNext());
877: }
878:
879: public void testColumnNamesMissing() throws Exception {
880: String readLine = "eins,zwei,drei,vier,,sechs,,acht,neun,\n";
881:
882: String[] fields = { "eins", "zwei", "drei", "vier",
883: "UNKNOWNFIELD5", "sechs", "UNKNOWNFIELD7", "acht",
884: "neun", "UNKNOWNFIELD10", };
885:
886: Reader myReader = new StringReader(readLine);
887:
888: List fieldNames = Arrays.asList(fields);
889:
890: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
891:
892: assertTrue(p.hasNext());
893:
894: p.readColumnNames();
895:
896: assertEquals(
897: "Number of columns is not the number of set fields",
898: fieldNames.size(), p.getColumnNames().size());
899: int cnt = 0;
900: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
901: assertEquals("Column name does not match", ((String) it
902: .next()), fields[cnt++]);
903: }
904:
905: assertFalse(p.hasNext());
906: }
907:
908: public void testAllColumnNamesMissing() throws Exception {
909: String readLine = ",,,\n0,1,2,3,4,5,6,7,8,9\n";
910:
911: String[] fields = { "UNKNOWNFIELD1", "UNKNOWNFIELD2",
912: "UNKNOWNFIELD3", "UNKNOWNFIELD4", };
913: int[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
914:
915: Reader myReader = new StringReader(readLine);
916:
917: List fieldNames = Arrays.asList(fields);
918:
919: DataStreamParser p = new CSVParser(myReader, null, "UTF-8");
920:
921: assertTrue(p.hasNext());
922:
923: p.readColumnNames();
924:
925: assertEquals(
926: "Number of columns is not the number of set fields",
927: fieldNames.size(), p.getColumnNames().size());
928: int cnt = 0;
929: for (Iterator it = p.getColumnNames().iterator(); it.hasNext();) {
930: assertEquals("Column name does not match", ((String) it
931: .next()), fields[cnt++]);
932: }
933:
934: assertTrue(p.hasNext());
935:
936: ValueParser vp = (ValueParser) p.next();
937:
938: assertEquals("Wrong character encoding set", "UTF-8", vp
939: .getCharacterEncoding());
940:
941: assertFalse(p.hasNext());
942:
943: for (int i = 0; i < fields.length; i++) {
944: assertEquals("Value does not match", Integer
945: .toString(values[i]), vp.getString(fields[i]));
946: assertEquals("Value does not match", values[i], vp
947: .getInt(fields[i]));
948: }
949: }
950:
951: }
|