View Javadoc
1   package org.dbunit.dataset.csv;
2   
3   import junit.framework.TestCase;
4   import org.dbunit.Assertion;
5   import org.dbunit.dataset.CachedDataSet;
6   import org.dbunit.dataset.DataSetException;
7   import org.dbunit.dataset.IDataSet;
8   import org.dbunit.testutil.TestUtils;
9   
10  import java.io.File;
11  
12  /**
13   * Created By:   fede
14   * Date:         10-mar-2004 
15   * Time:         17.21.34
16   *
17   * Last Checkin: $Author$
18   * Date:         $Date$
19   * Revision:     $Revision$
20   */
21  public class CsvDataSetWriterTest extends TestCase {
22      private static final String DEST = "target/csv/orders-out";
23      private static final String SOURCE = TestUtils.getFileName("csv/orders");
24  
25      public void testProduceAndWriteBackToDisk () throws Exception {
26          produceToFolder (SOURCE, DEST);
27          IDataSet expected = produceToMemory (SOURCE);
28          IDataSet actual = produceToMemory(DEST);
29          Assertion.assertEquals(expected, actual);
30      }
31  
32      private IDataSet produceToMemory(String source) throws DataSetException {
33          CsvProducer producer = new CsvProducer(source);
34          CachedDataSet cached = new CachedDataSet();
35          producer.setConsumer(cached);
36          producer.produce();
37          return cached;
38      }
39  
40      private void produceToFolder(String source, String dest) throws DataSetException {
41          CsvProducer producer = new CsvProducer(source);
42          new File(dest).delete();
43          CsvDataSetWriter writer = new CsvDataSetWriter(dest);
44          producer.setConsumer(writer);
45          producer.produce();
46      }
47  
48      public void testEscapeQuote () {
49          assertEquals("\\\"foo\\\"", CsvDataSetWriter.escape("\"foo\""));
50      }
51  
52      public void testEscapeEscape () {
53          assertEquals("\\\\foo\\\\", CsvDataSetWriter.escape("\\foo\\"));
54      }
55  
56  }