1 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 ; 32 33 39 public class CSVParserFactoryTest 40 extends TestCase { 41 42 45 private static final String SAMPLE_CSV_FILE = "test/net/sf/anupam/csv/beans/sample.csv"; 46 47 50 private Log log = LogFactory 51 .getLog(CSVParserFactoryTest.class); 52 53 58 public CSVParserFactoryTest(final String name) { 59 super(name); 60 } 61 62 67 public static void main(final String [] args) { 68 junit.textui.TestRunner.run(CSVParserFactoryTest.class); 69 } 70 71 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 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 e) { 106 fail("Unexpected exception " 107 + e.getLocalizedMessage()); 108 } 109 } 110 111 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 e) { 125 } 127 } 128 129 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 e) { 143 } 145 } 146 } 147 | Popular Tags |