001    package nl.cwi.sen1.visplugin.graphplugin;
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    import aterm.ATerm;
009    
010    /**
011     * Test class for the GraphChartVisualization Plugin. Parts of the code copied
012     * and adapted from the PieCharVisualizationTest.
013     * 
014     * @author Anton Gerdessen
015     * @date 09-3-2007
016     */
017    public class GraphVisualizationTest extends TestCase {
018    
019        private GraphVisualizationController m_controller;
020        private GraphVisualizationWindow m_window;
021        private Factory m_factory;
022        private RTuple m_relStrStr;
023        private RTuple m_relIntInt;
024        private RType m_typeRelStrStr;
025        private RType m_typeRelIntInt;
026    
027        /**
028         * Test setup.
029         * 
030         * @author Anton Gerdessen
031         * @date 09-3-2007
032         */
033        protected void setUp() throws Exception {
034            super.setUp();
035    
036            // Create the visualisation itself.
037            m_factory = VisualizationFactorySingleton.getFactoryInstance();
038            m_controller = new GraphVisualizationController();
039            m_controller.initPluginController(VisualizationFactorySingleton
040                    .getPureFactoryInstance());
041            m_window = (GraphVisualizationWindow) m_controller.createWindow();
042            m_window.setFactory(m_factory);
043    
044            // Setup two Rtuples for the tests.
045            String relStrStr =  "rtuple(\"OK_GRAPH\"," + 
046                                "relation([str,str])," + 
047                                "set([tuple([str(\"a\")," + 
048                                "str(\"b\")])," + 
049                                "tuple([str(\"b\")," + 
050                                "str(\"b\")])," + 
051                                "tuple([str(\"b\"),"+ 
052                                "str(\"c\")])," + 
053                                "tuple([str(\"c\")," + 
054                                "str(\"a\")])]))";
055    
056            String relIntInt =  "rtuple(\"TEST_GRAPH\"," + 
057                                "relation([int,int])," + 
058                                "set([tuple([int(nat-con(10))," + 
059                                "int(nat-con(10))])]))";
060    
061            // Setup the relation type and relation themselves for the preivoulsy
062            // created RTuples.
063            m_typeRelStrStr = m_factory.RTypeFromString("relation([str,str])");
064            m_relStrStr = m_factory.RTupleFromString(relStrStr);
065            m_typeRelIntInt = m_factory.RTypeFromString("relation([int,int])");
066            m_relIntInt = m_factory.RTupleFromString(relIntInt);
067    
068        }
069    
070        /**
071         * Test to see whether the control returns the correct type Window.
072         * 
073         * @author Anton Gerdessen
074         * @date 09-3-2007
075         */
076        public void testPieChartVisualizationController() {
077            GraphVisualizationController controller = new GraphVisualizationController();
078            assertEquals(GraphVisualizationWindow.class, controller.createWindow()
079                    .getClass());
080        }
081    
082        /**
083         * Test to see if the supported types system works.
084         * 
085         * @author Anton Gerdessen
086         * @date 09-3-2007
087         */
088        public void testTypeCheck() {
089            assertTrue(m_window.isTypeSupported(m_relStrStr));
090            assertFalse(m_window.isTypeSupported(m_relIntInt));
091        }
092    
093        /**
094         * Test to see if the name for the chart is correctly found.
095         * 
096         * @author Anton Gerdessen
097         * @date 09-3-2007
098         */
099        public void testGetChartName() {
100            assertEquals("OK_GRAPH", m_window.getRTupleName(m_relStrStr));
101            assertEquals("TEST_GRAPH", m_window.getRTupleName(m_relIntInt));
102        }
103    
104        /**
105         * See if the controller correctly advertises a supported types.
106         * 
107         * @author Anton Gerdessen
108         * @date 09-3-2007
109         */
110        public void testControllerSupportedTypes() {
111            assertTrue(isRTypeInATermArray(m_controller.getSupportedTypes(),
112                    m_typeRelStrStr));
113            assertFalse(isRTypeInATermArray(m_controller.getSupportedTypes(),
114                    m_typeRelIntInt));
115        }
116    
117        /**
118         * Test if a specific RType is part of an ATerm array.
119         * 
120         * @author A. Belgraver
121         * @author R. van Remortel
122         * @author Aldert Boerhoop (reviewer)
123         * @author Anton Gerdessen (reviewer)
124         * @param arr
125         *            Array of ATerms
126         * @param type
127         *            RType to find in ATerm array
128         * @return true if it is in the array, false if it isn't
129         * @date 09-3-2007
130         */
131        private boolean isRTypeInATermArray(ATerm[] arr, RType type) {
132            for (int i = 0; i < arr.length; i++) {
133                if (arr[i].equals(type)) {
134                    return true;
135                }
136            }
137            return false;
138        }
139    }