001    package nl.cwi.sen1.visplugin.table.model;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.io.IOException;
007    
008    import junit.framework.TestCase;
009    import nl.cwi.sen1.relationstores.Factory;
010    import nl.cwi.sen1.relationstores.types.RStore;
011    import nl.cwi.sen1.relationstores.types.RTuple;
012    import nl.cwi.sen1.visplugin.VisualizationFactorySingleton;
013    
014    /**
015     * Tests the SortableTableModelExporter class.
016     *
017     * @author Anton Gerdessen
018     * @author Arend van Beelen
019     * @date 13-03-2007
020     */
021    public class SortableTableModelExporterTest extends TestCase {
022    
023        private static String m_testFilename = "test_export.rstore";
024    
025        private Factory m_factory;
026        private RTuple m_testRTuple;
027        private RTuple m_testRTupleSelection;
028    
029        private SortableTableModel m_model;
030        private SortableTableModelExporter m_exporter;
031    
032        /**
033         * Set-up code for the unit tests.
034         *
035         * @author Anton Gerdessen
036         * @author Arend van Beelen
037         * @date 13-03-2007
038         */
039        public void setUp() {
040            // clean up any previous occurences of the test file
041            (new File(m_testFilename)).delete();
042    
043            m_factory = VisualizationFactorySingleton.getFactoryInstance();
044    
045            String relStrStr = "rtuple(\"STRING_TABLE\"," + "relation([str,str]),"
046                    + "set(["
047                    + "tuple([str(\"Row 1\"),str(\"abc\")]),"
048                    + "tuple([str(\"Row 2\"),str(\"bcd\")]),"
049                    + "tuple([str(\"Row 3\"),str(\"abd\")])"
050                    + "]))";
051            m_testRTuple = m_factory.RTupleFromString(relStrStr);
052    
053            relStrStr = "rtuple(\"STRING_TABLE\"," + "relation([str,str]),"
054                    + "set(["
055                    + "tuple([str(\"Row 1\"),str(\"abc\")]),"
056                    + "tuple([str(\"Row 3\"),str(\"abd\")])"
057                    + "]))";
058            m_testRTupleSelection = m_factory.RTupleFromString(relStrStr);
059    
060            m_model = new SortableTableModel();
061            m_model.setRTupleData(m_testRTuple);
062            m_exporter = new SortableTableModelExporter(m_model);
063        }
064    
065        /**
066         * Tests the exportToRStore() function while exporting the entire table.
067         *
068         * @author Anton Gerdessen
069         * @author Arend van Beelen
070         * @date 13-03-2007
071         */
072        public void testExportToRStoreEntireTable() {
073            int[] selectedRows = {};
074            m_exporter.exportToRStore(m_testFilename, selectedRows);
075    
076            String contents = readFile(m_testFilename);
077    
078            RStore rStore = m_factory.RStoreFromString(contents);
079            assertEquals(m_testRTuple, rStore.getRtuples().getRTupleAt(0));
080        }
081    
082        /**
083         * Tests the exportToRStore() function while exporting only the first and
084         * third row.
085         *
086         * @author Anton Gerdessen
087         * @author Arend van Beelen
088         * @date 13-03-2007
089         */
090        public void testExportToRStoreTableSelection() {
091            int[] selectedRows = {0, 2};
092            m_exporter.exportToRStore(m_testFilename, selectedRows);
093    
094            String contents = readFile(m_testFilename);
095    
096            RStore rStore = m_factory.RStoreFromString(contents);
097            assertEquals(m_testRTupleSelection, rStore.getRtuples().getRTupleAt(0));
098        }
099    
100        /**
101         * Reads an entire file and returns the contents as a string.
102         *
103         * @param filename Filename of the file to read.
104         * @return A string containing the file contents.
105         *
106         * @author Anton Gerdessen
107         * @author Arend van Beelen
108         * @date 13-03-2007
109         */
110        private String readFile(String filename) {
111            StringBuilder stringBuilder = new StringBuilder();
112            BufferedReader in = null;
113            try {
114                in = new BufferedReader(new FileReader(filename));
115                String line;
116                while ((line = in.readLine()) != null) {
117                    stringBuilder.append(line);
118                }
119                in.close();
120            } catch (IOException exception) {
121                    System.err.println("Unable to read from file with name: "+filename);
122            }finally{
123                    if(in != null){
124                            try{
125                                    in.close();
126                            }catch(IOException ioex){
127                                    System.err.println("A fatal error occured: Unable to close stream.");
128                            }
129                    }
130            }
131            return stringBuilder.toString();
132        }
133    }