1 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 ; 29 import java.io.Reader ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.NoSuchElementException ; 33 34 40 public class CSVReaderTest 41 extends TestCase { 42 43 46 private static final String sampleCSVFileName = "test/net/sf/anupam/csv/beans/sample.csv"; 47 48 51 private static final Log LOG = LogFactory 52 .getLog(CSVReaderTest.class); 53 54 57 private Reader csvReader; 58 59 62 67 public CSVReaderTest(final String testName) { 68 super(testName); 69 } 70 71 74 79 public static void main(final String [] args) { 80 junit.textui.TestRunner.run(CSVReaderTest.class); 81 } 82 83 89 @Override 90 protected void setUp() throws Exception { 91 super.setUp(); 92 93 csvReader = new InputStreamReader (ClassLoader 94 .getSystemResourceAsStream(sampleCSVFileName)); 95 LOG.info("Loaded the test CSV file"); 96 97 } 98 99 105 @Override 106 protected void tearDown() throws Exception { 107 super.tearDown(); 108 if (csvReader != null) { 109 csvReader.close(); 110 } 111 } 112 113 116 public final void testCSVReader() { 117 final CSVReader reader = new CSVReader(csvReader, true); 118 assertNotNull(reader); 119 reader.close(); 120 } 121 122 125 public final void testIterator() { 126 final CSVReader reader = new CSVReader(csvReader, true); 127 assertNotNull(reader); 128 for (List <String > l : reader) { 129 for (String value : l) { 130 assertNotNull(value); 131 LOG.info(value); 132 } 133 } 134 final Iterator iter = reader.iterator(); 135 136 assertFalse(iter.hasNext()); 137 try { 138 iter.next(); 139 fail("Should have thrown an exception"); 140 } catch (final NoSuchElementException e) { 141 } 143 reader.close(); 144 } 145 146 } 147 | Popular Tags |