001: /*
002: * TestCSVReader.java
003: *
004: * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019: *
020: * Version $Revision: 1.2 $
021: */
022: package net.sf.anupam.csv;
023:
024: import junit.framework.TestCase;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import java.io.InputStreamReader;
029: import java.io.Reader;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.NoSuchElementException;
033:
034: /**
035: * TestCSVReader.
036: *
037: * @author Tata Consultancy Services
038: * @version $Revision: 1.2 $
039: */
040: public class CSVReaderTest extends TestCase {
041:
042: /**
043: * The sample data file to use for the test.
044: */
045: private static final String sampleCSVFileName = "test/net/sf/anupam/csv/beans/sample.csv";
046:
047: /**
048: * Logger to use.
049: */
050: private static final Log LOG = LogFactory
051: .getLog(CSVReaderTest.class);
052:
053: /**
054: * The CSV reader to use in the test.
055: */
056: private Reader csvReader;
057:
058: // ~ Constructors
059: // -----------------------------------------------------------
060:
061: /**
062: * Constructor for TestCSVReader.
063: *
064: * @param testName name of the test
065: */
066: public CSVReaderTest(final String testName) {
067: super (testName);
068: }
069:
070: // ~ Methods
071: // ----------------------------------------------------------------
072:
073: /**
074: * Main method to run the test.
075: *
076: * @param args Program arguments
077: */
078: public static void main(final String[] args) {
079: junit.textui.TestRunner.run(CSVReaderTest.class);
080: }
081:
082: /**
083: * Reads the sample CSV file for this test.
084: *
085: * @throws Exception if the file IO fails
086: * @see junit.framework.TestCase#setUp()
087: */
088: @Override
089: protected void setUp() throws Exception {
090: super .setUp();
091:
092: csvReader = new InputStreamReader(ClassLoader
093: .getSystemResourceAsStream(sampleCSVFileName));
094: LOG.info("Loaded the test CSV file");
095:
096: }
097:
098: /**
099: * Closes the CSV reader.
100: *
101: * @throws Exception if an IO error occurs
102: * @see junit.framework.TestCase#tearDown()
103: */
104: @Override
105: protected void tearDown() throws Exception {
106: super .tearDown();
107: if (csvReader != null) {
108: csvReader.close();
109: }
110: }
111:
112: /**
113: * Test the reader's construction method.
114: */
115: public final void testCSVReader() {
116: final CSVReader reader = new CSVReader(csvReader, true);
117: assertNotNull(reader);
118: reader.close();
119: }
120:
121: /**
122: * Test the reader's iterator() method.
123: */
124: public final void testIterator() {
125: final CSVReader reader = new CSVReader(csvReader, true);
126: assertNotNull(reader);
127: for (List<String> l : reader) {
128: for (String value : l) {
129: assertNotNull(value);
130: LOG.info(value);
131: }
132: }
133: final Iterator iter = reader.iterator();
134:
135: assertFalse(iter.hasNext());
136: try {
137: iter.next();
138: fail("Should have thrown an exception");
139: } catch (final NoSuchElementException e) {
140: // Do nothing
141: }
142: reader.close();
143: }
144:
145: }
|