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