KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > anupam > csv > CSVReaderTest


1 /*
2  * TestCSVReader.java
3  *
4  * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Version $Revision: 1.2 $
21  */

22 package net.sf.anupam.csv;
23
24 import junit.framework.TestCase;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.NoSuchElementException JavaDoc;
33
34 /**
35  * TestCSVReader.
36  *
37  * @author Tata Consultancy Services
38  * @version $Revision: 1.2 $
39  */

40 public class CSVReaderTest
41         extends TestCase {
42
43     /**
44      * The sample data file to use for the test.
45      */

46     private static final String JavaDoc sampleCSVFileName = "test/net/sf/anupam/csv/beans/sample.csv";
47
48     /**
49      * Logger to use.
50      */

51     private static final Log LOG = LogFactory
52             .getLog(CSVReaderTest.class);
53
54     /**
55      * The CSV reader to use in the test.
56      */

57     private Reader JavaDoc csvReader;
58
59     // ~ Constructors
60
// -----------------------------------------------------------
61

62     /**
63      * Constructor for TestCSVReader.
64      *
65      * @param testName name of the test
66      */

67     public CSVReaderTest(final String JavaDoc testName) {
68         super(testName);
69     }
70
71     // ~ Methods
72
// ----------------------------------------------------------------
73

74     /**
75      * Main method to run the test.
76      *
77      * @param args Program arguments
78      */

79     public static void main(final String JavaDoc [] args) {
80         junit.textui.TestRunner.run(CSVReaderTest.class);
81     }
82
83     /**
84      * Reads the sample CSV file for this test.
85      *
86      * @throws Exception if the file IO fails
87      * @see junit.framework.TestCase#setUp()
88      */

89     @Override JavaDoc
90     protected void setUp() throws Exception JavaDoc {
91         super.setUp();
92
93         csvReader = new InputStreamReader JavaDoc(ClassLoader
94                 .getSystemResourceAsStream(sampleCSVFileName));
95         LOG.info("Loaded the test CSV file");
96
97     }
98
99     /**
100      * Closes the CSV reader.
101      *
102      * @throws Exception if an IO error occurs
103      * @see junit.framework.TestCase#tearDown()
104      */

105     @Override JavaDoc
106     protected void tearDown() throws Exception JavaDoc {
107         super.tearDown();
108         if (csvReader != null) {
109             csvReader.close();
110         }
111     }
112
113     /**
114      * Test the reader's construction method.
115      */

116     public final void testCSVReader() {
117         final CSVReader reader = new CSVReader(csvReader, true);
118         assertNotNull(reader);
119         reader.close();
120     }
121
122     /**
123      * Test the reader's iterator() method.
124      */

125     public final void testIterator() {
126         final CSVReader reader = new CSVReader(csvReader, true);
127         assertNotNull(reader);
128         for (List JavaDoc<String JavaDoc> l : reader) {
129             for (String JavaDoc value : l) {
130                 assertNotNull(value);
131                 LOG.info(value);
132             }
133         }
134         final Iterator JavaDoc iter = reader.iterator();
135
136         assertFalse(iter.hasNext());
137         try {
138             iter.next();
139             fail("Should have thrown an exception");
140         } catch (final NoSuchElementException JavaDoc e) {
141             // Do nothing
142
}
143         reader.close();
144     }
145
146 }
147
Popular Tags