KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSVParserTest.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.3 $
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 import test.net.sf.anupam.csv.beans.Designation;
28 import test.net.sf.anupam.csv.beans.Employee;
29
30 import java.io.FileNotFoundException JavaDoc;
31
32 import net.sf.anupam.csv.exceptions.CSVOException;
33
34 /**
35  * CSVParserTest.
36  *
37  * @author Anupam Sengupta
38  * @version $Revision: 1.3 $
39  */

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

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

51     private Log log = LogFactory
52             .getLog(CSVParserTest.class);
53
54     /**
55      * Constructor for CSVParserTest.
56      *
57      * @param name name of the test
58      */

59     public CSVParserTest(final String JavaDoc name) {
60         super(name);
61     }
62
63     /**
64      * Main method to run the test.
65      *
66      * @param args Program arguments
67      */

68     public static void main(final String JavaDoc [] args) {
69         junit.textui.TestRunner.run(CSVParserTest.class);
70     }
71
72     /**
73      * Test method for 'com.tcs.mis.csv.utilities.CSVParser.getMappedBeans()'.
74      *
75      * @throws net.sf.anupam.csv.exceptions.CSVOException
76      *
77      */

78     public void testGetMappedBeans() throws CSVOException {
79         final CSVParserFactory factory = CSVParserFactory.getSingleton();
80         assertNotNull(factory);
81         try {
82             final CSVParser parser = factory.getCSVParser("employeeBean",
83                     SAMPLE_CSV_FILE, true);
84             assertNotNull(parser);
85             for (Object JavaDoc bean : parser) {
86                 assertTrue(bean instanceof Employee);
87                 final Employee empl = (Employee) bean;
88                 assertEquals("123456", empl.getEmployeeID());
89                 assertEquals("John", empl.getFirstName());
90                 assertEquals("Doe", empl.getLastName());
91                 assertEquals("BILLID01", empl.getClientSuppliedID());
92                 assertEquals("CONTRACTOR007", empl.getClientSuppliedSecondaryID());
93                 final Designation desgn = empl.getDesignation();
94                 assertNotNull(desgn);
95                 assertEquals("Lead", desgn.getDesignation());
96                 log.info(empl);
97             }
98         } catch (final FileNotFoundException JavaDoc e) {
99             fail("Unexpected exception: "
100                     + e.getLocalizedMessage());
101         }
102
103     }
104
105 }
106
Popular Tags