001    package nl.cwi.sen1.visplugin.csvexport;
002    
003    import java.io.ByteArrayOutputStream;
004    import java.io.IOException;
005    import java.nio.charset.Charset;
006    
007    import junit.framework.TestCase;
008    import nl.cwi.sen1.relationstores.Factory;
009    import nl.cwi.sen1.relationstores.types.RTuple;
010    import nl.cwi.sen1.visplugin.VisualizationFactorySingleton;
011    
012    import com.csvreader.CsvWriter;
013    
014    
015    /**
016     * JUnit Testcase used to test the Exporter class
017     * @author Chris Woolderink
018     * @author Antoine Savelkoul
019     * @date 12-03-2007
020     */
021    public class ExporterTest extends TestCase {
022            
023        private Factory m_factory;
024            
025            /**
026             * This method is executed before the start of a test
027             * @author Chris Woolderink
028             * @author Antoine Savelkoul
029             * @date 12-03-2007
030             */
031            protected void setUp() throws Exception {
032                    super.setUp();
033    
034            m_factory = VisualizationFactorySingleton.getFactoryInstance();
035            }
036    
037            /**
038             * This method is executed after the end of a test
039             * @author Chris Woolderink
040             * @author Antoine Savelkoul
041             * @date 12-03-2007
042             */
043            protected void tearDown() throws Exception {
044                    super.tearDown();
045            }
046    
047            /**
048             * Export from a RTuple with "String String"-relation 
049             */
050            public void testExportFromRTupleStrStr() {
051            // Setup the relation type and relation themselves.
052            String relStrStr =  "rtuple(\"STRING_TABLE\"," +
053            "relation([str,str])," +
054            "set([" +
055            "tuple([str(\"Row 1, Column 1\"),str(\"Row 1, Column 2\")])," +
056            "tuple([str(\"Row 2, Column 1\"),str(\"Row 2, Column 2\")])," +
057            "tuple([str(\"Row 3, Column 1\"),str(\"Row 3, Column 2\")])" +
058            "]))";
059            RTuple tupleRelStrStr = m_factory.RTupleFromString(relStrStr);
060            
061            String csvExpected =
062                    "\"Row 1, Column 1\",\"Row 1, Column 2\"\n" +
063                    "\"Row 2, Column 1\",\"Row 2, Column 2\"\n" +
064                    "\"Row 3, Column 1\",\"Row 3, Column 2\"\n";
065            
066            String result = rTupleToCsv(tupleRelStrStr, Exporter.ExportSelection.DATA_ONLY);
067                    
068            assertEquals(csvExpected, result);
069            }
070    
071            /**
072             * Export from a RTuple with "String Integer"-relation 
073             */
074            public void testExportFromRTupleStrInt() {
075            // Setup the relation type and relation themselves.
076            String relStrInt =  "rtuple(\"STRING_TABLE\"," +
077            "relation([str,int])," +
078            "set([" +
079            "tuple([str(\"Row 1, Column 1\"),int(nat-con(1))])," +
080            "tuple([str(\"Row 2, Column 1\"),int(nat-con(2))])," +
081            "tuple([str(\"Row 3, Column 1\"),int(nat-con(3))])" +
082            "]))";
083            RTuple tupleRelStrInt = m_factory.RTupleFromString(relStrInt);
084            
085            String csvExpectedWithoutHeader =
086                    "\"Row 1, Column 1\",1\n" +
087                    "\"Row 2, Column 1\",2\n" +
088                    "\"Row 3, Column 1\",3\n";
089    
090            String result1 = rTupleToCsv(tupleRelStrInt, Exporter.ExportSelection.DATA_ONLY);
091            assertEquals(csvExpectedWithoutHeader, result1);
092            
093            String csvExpectedWithHeader =
094                    "str,int\n" +
095                    "\"Row 1, Column 1\",1\n" +
096                    "\"Row 2, Column 1\",2\n" +
097                    "\"Row 3, Column 1\",3\n";
098            
099            String result2 = rTupleToCsv(tupleRelStrInt, Exporter.ExportSelection.DATA_WITH_HEADERS);
100            assertEquals(csvExpectedWithHeader, result2);
101            }
102    
103            /**
104             * Converts a RTuple to CSV formatted text by calling:
105             * Exporter.export(rTuple, csvWriter)
106             * @param rTuple  The RTuple to export
107             * @return  a String with the CSV formatted output
108             * @author Antoine Savelkoul
109             * @author Chris Woolderink
110             * @date 13-03-2007
111             */
112            private String rTupleToCsv(RTuple rTuple, Exporter.ExportSelection selection) {
113                    ByteArrayOutputStream byteArrayOutputStreamSimple = new ByteArrayOutputStream();
114                    CsvWriter csvWriter = new CsvWriter(byteArrayOutputStreamSimple,',',Charset.forName("UTF-8"));
115                    String csvOutput = null;
116                    
117                    try {
118                            Exporter.export(rTuple, csvWriter, selection);
119                            csvWriter.close();
120                            byteArrayOutputStreamSimple.close();
121                            csvOutput = byteArrayOutputStreamSimple.toString();
122                    }
123                    catch( IOException e ) {
124                            fail();
125                    }
126                    
127                    return csvOutput;
128            }
129    
130    }