001    package nl.cwi.sen1.visplugin.table;
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     * Test class for the TableVisualizationWindow.
016     *
017     * @author Arend van Beelen
018     * @date 13-03-2007
019     */
020    public class TableVisualizationWindowTest extends TestCase {
021    
022        private static String m_testFilename = "test_export_from_window.rstore";
023    
024        private Factory m_factory;
025        private RTuple m_testRTuple;
026    
027        private TableVisualizationWindow m_window;
028    
029        /**
030         * Set-up code for the unit tests.
031         *
032         * @author Arend van Beelen
033         * @date 13-03-2007
034         */
035        public void setUp() {
036            // clean up any previous occurences of the test file
037            (new File(m_testFilename)).delete();
038    
039            m_factory = VisualizationFactorySingleton.getFactoryInstance();
040    
041            String relStrStr = "rtuple(\"STRING_TABLE\"," + "relation([str,str]),"
042                    + "set(["
043                    + "tuple([str(\"Row 1\"),str(\"abc\")]),"
044                    + "tuple([str(\"Row 2\"),str(\"bcd\")]),"
045                    + "tuple([str(\"Row 3\"),str(\"abd\")])"
046                    + "]))";
047            m_testRTuple = m_factory.RTupleFromString(relStrStr);
048    
049            m_window = new TableVisualizationWindow();
050        }
051    
052        /**
053         * Tests the exportToRStore() method.
054         * Cannot be tested due to the studioComponent in exportToRstore.
055         *
056         * @author Arend van Beelen
057         * @date 13-03-2007
058         */
059        public void testExportToRStore() {
060            m_window.render(m_testRTuple);
061    
062            m_window.exportToRStore(m_testFilename);
063    
064            String contents = readFile(m_testFilename);
065    
066            RStore rStore = m_factory.RStoreFromString(contents);
067            assertEquals(m_testRTuple, rStore.getRtuples().getRTupleAt(0));
068        }
069    
070        /**
071         * Reads an entire file and returns the contents as a string.
072         *
073         * @param filename Filename of the file to read.
074         * @return A string containing the file contents.
075         *
076         * @author Anton Gerdessen
077         * @author Arend van Beelen
078         * @date 13-03-2007
079         */
080        private String readFile(String filename) {
081            StringBuilder stringBuilder = new StringBuilder();
082            BufferedReader in = null;
083            try {
084                in = new BufferedReader(new FileReader(filename));
085                String line;
086                while ((line = in.readLine()) != null) {
087                    stringBuilder.append(line);
088                }
089                
090            } catch (IOException exception) {
091                    System.err.println("Unable to read from file with name: "+filename);
092            }finally{
093                    if(in != null){
094                            try{
095                                    in.close();
096                            }catch(IOException ioex){
097                                    System.err.println("A fatal error occured: Unable to close reader.");
098                            }
099                    }
100            }
101            
102            return stringBuilder.toString();
103        }
104    }