001: /*
002: * CSVParserFactoryTest.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 net.sf.anupam.csv.mapping.CSVBeanMapping;
026: import net.sf.anupam.csv.mapping.CSVFieldMapping;
027: import net.sf.anupam.csv.exceptions.CSVOException;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.io.FileNotFoundException;
032:
033: /**
034: * CSVParserFactoryTest.
035: *
036: * @author Anupam Sengupta
037: * @version $Revision: 1.2 $
038: */
039: public class CSVParserFactoryTest extends TestCase {
040:
041: /**
042: * The sample data file to use for the test.
043: */
044: private static final String SAMPLE_CSV_FILE = "test/net/sf/anupam/csv/beans/sample.csv";
045:
046: /**
047: * The logger to use.
048: */
049: private Log log = LogFactory.getLog(CSVParserFactoryTest.class);
050:
051: /**
052: * Constructor for CSVParserFactoryTest.
053: *
054: * @param name name of the test
055: */
056: public CSVParserFactoryTest(final String name) {
057: super (name);
058: }
059:
060: /**
061: * Main method to run the test.
062: *
063: * @param args program arguments
064: */
065: public static void main(final String[] args) {
066: junit.textui.TestRunner.run(CSVParserFactoryTest.class);
067: }
068:
069: /**
070: * Test the getBeanMapping method.
071: *
072: * @throws net.sf.anupam.csv.exceptions.CSVOException
073: * thrown if there is a test failure
074: */
075: public void testGetBeanMapping() throws CSVOException {
076: final CSVParserFactory parserFactory = CSVParserFactory
077: .getSingleton();
078:
079: assertNotNull(parserFactory);
080:
081: final CSVBeanMapping beanMapping = parserFactory
082: .getBeanMapping("employeeBean");
083: assertNotNull(beanMapping);
084: log.debug(beanMapping);
085: for (CSVFieldMapping fieldMapping : beanMapping) {
086: assertNotNull(fieldMapping);
087: log.debug(fieldMapping);
088: }
089: }
090:
091: /**
092: * Tests the getCSVParser method.
093: *
094: * @throws CSVOException
095: */
096: public void testGetCSVParser() throws CSVOException {
097: final CSVParserFactory parserFactory = CSVParserFactory
098: .getSingleton();
099:
100: assertNotNull(parserFactory);
101:
102: try {
103: parserFactory.getCSVParser("employeeBean", SAMPLE_CSV_FILE,
104: true);
105: } catch (final FileNotFoundException e) {
106: fail("Unexpected exception " + e.getLocalizedMessage());
107: }
108: }
109:
110: /**
111: * Tests the getCSVParser method.
112: *
113: * @throws CSVOException
114: */
115: public void testGetCSVParserForException() throws CSVOException {
116: final CSVParserFactory parserFactory = CSVParserFactory
117: .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 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
136: .getSingleton();
137:
138: assertNotNull(parserFactory);
139:
140: try {
141: parserFactory.getCSVParser("", SAMPLE_CSV_FILE, true);
142: fail("Should have thrown a FileNotFoundException");
143: } catch (final Throwable e) {
144: // Do nothing
145: }
146: }
147: }
|