001    package nl.cwi.sen1.visplugin.barchart;
002    
003    import junit.framework.TestCase;
004    import nl.cwi.sen1.relationstores.Factory;
005    import nl.cwi.sen1.relationstores.types.RTuple;
006    import nl.cwi.sen1.relationstores.types.RType;
007    import nl.cwi.sen1.visplugin.VisualizationFactorySingleton;
008    
009    import org.jfree.data.category.CategoryDataset;
010    
011    import aterm.ATerm;
012    
013    /**
014     * Test class for the PieChartVisualization Plugin.
015     * Original code : PieChart plugin ( A. Belgraver, R. van Remortel )
016     * @author Srinivasan Tharmarajah
017     * @date 12-03-2007
018     */
019    public class BarChartVisualizationTest extends TestCase {
020    
021        private BCVisualizationController m_controller;
022        private BCVisualizationWindow m_window;
023        private Factory m_factory;
024        private RTuple m_relStrInt;
025        private RTuple m_relIntStr;
026        private RTuple m_relWrong;
027        private RType m_typeRelStrInt;
028        private RType m_typeRelIntStr;
029        private RType m_typeRelWrong;
030    
031        /**
032         * Test setup.
033         * 
034         * @author A. Belgraver
035         * @author R. van Remortel
036         * @author Srinivasan Tharmarajah
037         * @date 12-03-2007
038         */
039        protected void setUp() throws Exception {
040            super.setUp();
041    
042            // Create the visualisation itself.
043            m_factory = VisualizationFactorySingleton.getFactoryInstance();
044            m_controller = new BCVisualizationController();
045            m_controller.initPluginController(VisualizationFactorySingleton.getPureFactoryInstance());
046            m_window = (BCVisualizationWindow) m_controller.createWindow();
047            m_window.setFactory(m_factory);        
048            
049            // Setup three Rtuples for the tests.
050            String relIntStr =  "rtuple(\"StatementHistogram\", " +
051                                "relation([int,str])," +
052                                "set([tuple([int(nat-con(7))," +
053                                "str(\"Assignment\")]), "+
054                                "tuple([int(nat-con(6)), " +
055                                "str(\"Assignment2\")]), " +
056                                "tuple([int(nat-con(5))," + 
057                                "str(\"Assignment3\")]), " +
058                                "tuple([int(nat-con(2))," +
059                                "str(\"Assignment4\")])]))";
060            String relStrInt =  "rtuple(\"TEST_BAR\"," +
061                                "relation([str,int])," +
062                                "set([tuple([str(\"a\")," +
063                                "int(nat-con(10))])]))";
064            String relWrong =   "rtuple(\"WRONG_BAR\"," +
065                                "relation([int,int,str])," +
066                                "set([tuple([str(\"a\")," +
067                                "int(nat-con(10))])]))";
068    
069            // Setup the relation type and relation themselves for the preivoulsy
070            // created RTuples.
071            m_typeRelIntStr = m_factory.RTypeFromString("relation([int,str])");
072            m_typeRelStrInt = m_factory.RTypeFromString("relation([str,int])");
073            m_typeRelWrong = m_factory.RTypeFromString("relation([int,int,str])");
074            m_relIntStr = m_factory.RTupleFromString(relIntStr);
075            m_relStrInt = m_factory.RTupleFromString(relStrInt);
076            m_relWrong = m_factory.RTupleFromString(relWrong);
077            
078        }
079    
080        /**
081         * Test to see whether the control returns the correct type Window.
082         * 
083         * @author A. Belgraver
084         * @author R. van Remortel
085         * @author Srinivasan Tharmarajah
086         * @author Aldert Boerhoop (reviewer)
087         * @author Anton Gerdessen (reviewer)
088         * @date 12-03-2007
089         */
090        public void testBarChartVisualizationController() {
091            
092            BCVisualizationController controller = new BCVisualizationController();
093            assertEquals(BCVisualizationWindow.class, controller
094                    .createWindow().getClass());
095        }
096    
097        /**
098         * Test to see if the TypeCheck system works.
099         * 
100         * @author A. Belgraver
101         * @author R. van Remortel
102         * @author Srinivasan Tharmarajah
103         * @author Aldert Boerhoop (reviewer)
104         * @author Anton Gerdessen (reviewer)
105         * @date 07-3-2007
106         */
107        public void testTypeCheck() {
108            assertTrue(m_window.isTypeSupported(m_relStrInt));
109            assertTrue(m_window.isTypeSupported(m_relIntStr));
110            assertFalse(m_window.isTypeSupported(m_relWrong));
111        }
112    
113        /**
114         * Test to see if the name for the chart is correctly found.
115         * 
116         * @author A. Belgraver
117         * @author R. van Remortel
118         * @author Srinivasan Tharmarajah
119         * @author Aldert Boerhoop (reviewer)
120         * @author Anton Gerdessen (reviewer)
121         * @date 12-03-2007
122         */
123        public void testGetChartName() {
124            assertEquals("StatementHistogram", m_window.getRTupleName(m_relIntStr));
125            assertEquals("TEST_BAR", m_window.getRTupleName(m_relStrInt));
126        }
127        
128        /**
129         * Test to see if RTuples are correctly converted, rel<str,int> & rel<int,str>.
130         * 
131         * @author A. Belgraver
132         * @author R. van Remortel
133         * @author Srinivasan Tharmarajah
134         * @author Aldert Boerhoop (reviewer)
135         * @author Anton Gerdessen (reviewer)
136         * @date 12-03-2007
137         */
138        public void testConvertRTupleToDataset() {
139            CategoryDataset datasetIntStr = m_window.convertRTupleToDataset(m_relIntStr);
140            assertEquals(4, datasetIntStr.getRowCount());
141            assertTrue(datasetIntStr.getRowKeys().contains("Assignment4"));
142            assertEquals(new Double(7.0), datasetIntStr.getValue(0,0));
143            CategoryDataset datasetStrInt = m_window.convertRTupleToDataset(m_relStrInt);
144            assertEquals(1, datasetStrInt.getRowCount());
145            assertTrue(datasetStrInt.getRowKeys().contains("a"));
146        }
147    
148        /**
149         * See if the controller correctly advertises a supported types.
150         * 
151         * @author A. Belgraver
152         * @author R. van Remortel
153         * @author Srinivasan Tharmarajah
154         * @author Aldert Boerhoop (reviewer)
155         * @author Anton Gerdessen (reviewer)
156         * @date 12-03-2007
157         */
158        public void testControllerSupportedTypes() {
159            assertTrue(isRTypeInATermArray(m_controller.getSupportedTypes(),
160                    m_typeRelStrInt));
161            assertTrue(isRTypeInATermArray(m_controller.getSupportedTypes(),
162                    m_typeRelIntStr));
163            assertFalse(isRTypeInATermArray(m_controller.getSupportedTypes(),
164                    m_typeRelWrong));
165        }
166       
167    
168        /**
169         * Test if a specific RType is part of an ATerm array.
170         * 
171         * @author A. Belgraver
172         * @author R. van Remortel
173         * @author Aldert Boerhoop (reviewer)
174         * @author Anton Gerdessen (reviewer)
175         * @param arr
176         *            Array of ATerms
177         * @param type
178         *            RType to find in ATerm array
179         * @return true if it is in the array, false if it isn't
180         * @date 12-03-2007
181         */
182        private boolean isRTypeInATermArray(ATerm[] arr, RType type) {
183            for (int i = 0; i < arr.length; i++) {
184                if (arr[i].equals(type)) {
185                    return true;
186                }
187            }
188            return false;
189        }
190    }