KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSVParserFactoryTest.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 net.sf.anupam.csv.mapping.CSVBeanMapping;
26 import net.sf.anupam.csv.mapping.CSVFieldMapping;
27 import net.sf.anupam.csv.exceptions.CSVOException;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import java.io.FileNotFoundException JavaDoc;
32
33 /**
34  * CSVParserFactoryTest.
35  *
36  * @author Anupam Sengupta
37  * @version $Revision: 1.2 $
38  */

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

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

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

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

67     public static void main(final String JavaDoc [] args) {
68         junit.textui.TestRunner.run(CSVParserFactoryTest.class);
69     }
70
71     /**
72      * Test the getBeanMapping method.
73      *
74      * @throws net.sf.anupam.csv.exceptions.CSVOException
75      * thrown if there is a test failure
76      */

77     public void testGetBeanMapping() throws CSVOException {
78         final CSVParserFactory parserFactory = CSVParserFactory.getSingleton();
79
80         assertNotNull(parserFactory);
81
82         final CSVBeanMapping beanMapping = parserFactory
83                 .getBeanMapping("employeeBean");
84         assertNotNull(beanMapping);
85         log.debug(beanMapping);
86         for (CSVFieldMapping fieldMapping : beanMapping) {
87             assertNotNull(fieldMapping);
88             log.debug(fieldMapping);
89         }
90     }
91
92     /**
93      * Tests the getCSVParser method.
94      *
95      * @throws CSVOException
96      */

97     public void testGetCSVParser() throws CSVOException {
98         final CSVParserFactory parserFactory = CSVParserFactory.getSingleton();
99
100         assertNotNull(parserFactory);
101
102         try {
103             parserFactory.getCSVParser("employeeBean", SAMPLE_CSV_FILE, true
104             );
105         } catch (final FileNotFoundException JavaDoc e) {
106             fail("Unexpected exception "
107                     + e.getLocalizedMessage());
108         }
109     }
110
111     /**
112      * Tests the getCSVParser method.
113      *
114      * @throws CSVOException
115      */

116     public void testGetCSVParserForException() throws CSVOException {
117         final CSVParserFactory parserFactory = CSVParserFactory.getSingleton();
118
119         assertNotNull(parserFactory);
120
121         try {
122             parserFactory.getCSVParser("employeeBean", "dummy", true);
123             fail("Should have thrown a FileNotFoundException");
124         } catch (final FileNotFoundException JavaDoc e) {
125             // Do nothing
126
}
127     }
128
129     /**
130      * Tests the getCSVParser method.
131      *
132      * @throws CSVOException
133      */

134     public void testGetCSVParserForEmptyBean() throws CSVOException {
135         final CSVParserFactory parserFactory = CSVParserFactory.getSingleton();
136
137         assertNotNull(parserFactory);
138
139         try {
140             parserFactory.getCSVParser("", SAMPLE_CSV_FILE, true);
141             fail("Should have thrown a FileNotFoundException");
142         } catch (final Throwable JavaDoc e) {
143             // Do nothing
144
}
145     }
146 }
147
Popular Tags