/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jonasadmin.test.navigation;

import java.io.IOException;
import java.util.Vector;

import junit.framework.TestSuite;

import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminFiles;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTree;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminUtils;
import org.xml.sax.SAXException;

import com.meterware.httpunit.HttpException;
import com.meterware.httpunit.HttpNotFoundException;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Define a class to test the JOnAS Admin console
 * Test all links, content title, selected tree item
 * @author kemlerp
 *
 */
public class F_JonasAdminNavigation extends JonasAdminTestCase {

    /**
     * SUCCESS CODE
     */
    private static final int HTTP_SUCCESS_CODE = 200;

    /**
     * NAME OF THE EAR LINK
     */
    private static final String EAR_LINK_NAME = "Applications (EAR)";

    /**
     * URL of the bean alarm record
     */
    private static final String URL_JONASADMIN_ALARM_RECORD = "/jonasAdmin/EditEjbEnt.do?select=jonas:j2eeType=EntityBean,name=AlarmRecord,EJBModule=alarm,J2EEApplication=alarm,J2EEServer=jonas";

    /**
     * URL of the synchronize button
     */
    private static final String URL_JONASADMIN_SYNCHRONIZE = "/jonasAdmin/ExecuteEjb.do?action=synchronize";

    /**
     * URL of the reduce instance button
     */
    private static final String URL_JONASADMIN_REDUCE = "/jonasAdmin/ExecuteEjb.do?action=reduceCache";

    /**
     * URL of the reduce pool instance button
     */
    private static final String URL_JONASADMIN_REDUCE_POOL = "/jonasAdmin/ExecuteEjb.do?action=reducePool";

    /**
     * URL of the new group instance button
     */
    private static final String URL_JONASADMIN_NEW_GROUP = "/jonasAdmin/EditNewGroupMemoryRealm.do?action=create";

    /**
     * URL of the new user instance button
     */
    private static final String URL_JONASADMIN_NEW_USER = "/jonasAdmin/EditNewUserMemoryRealm.do?action=create";

    /**
     * Class value of the selected tree control
     */
    private static final String CLASS_TREE_CONTROL_SELECTED = "tree-control-selected";

    /**
     * Name of the property navigation.mbean
     */
    private static final String NAVIGATION_MBEAN = "navigation.mbean";

    /**
     * Name of the property navigation.link
     */
    private static final String NAVIGATION_LINK = "navigation.link";

    /**
     * Name of the property navigation.contentTitle
     */
    private static final String NAVIGATION_CONTENT_TITLE = "navigation.contentTitle";

    /**
     * Name of the property navigation.selectedItem
     */
    private static final String NAVIGATION_SELECTED_ITEM = "navigation.selectedItem";

    /**
     * Name of the property navigation.titleTomThumb
     */
    private static final String NAVIGATION_TITLE_TOM_THUMB = "navigation.titleTomThumb";

    /**
     * Name of the property navigation.rarInEar
     */
    private static final String NAVIGATION_RAR_IN_EAR = "navigation.rarInEar";

    /**
     * Time of beginning testAllLinks
     */
    private String beginTime;

    /**
     * Time of ending testAllLinks
     */
    private String endTime;


    /**
     * Constructor with a specified name
     * @param s name
     */
    public F_JonasAdminNavigation(String s) {
        super(s, URL_JONASADMIN);
    }

    /**
     * Main method
     * @param args the arguments
     */
    public static void main(String[] args) {

        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String sArg = args[argn];
            if (sArg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_JonasAdminNavigation(testtorun));
        }
    }

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_JonasAdminNavigation.class);
    }

    /**
     * Setup need for these tests
     * jonasAdmin is required
     * @throws Exception if it fails
     */
    protected void setUp() throws Exception {
        super.setUp();

        // Get time
        beginTime = getTime();

        if (wc.getCurrentPage().getURL() == null) {
            useWar("jonasAdmin");
            // login to jonas admin
            try {
                JonasAdminAuth.doValidAuth(wc, url);
            } catch (Exception e) {
                fail("authentification failed :  " + e);
            }
        } else {
            // if there was an error, the connection must be restablished
            try {
                wc.getFrameContents(FRAME_TREE);
            } catch (Exception e) {
                wc.getResponse(urlLogOut);
                // login to jonas admin
                try {
                    JonasAdminAuth.doValidAuth(wc, url);
                } catch (Exception auth) {
                    fail("authentification failed :  " + auth);
                }
            }
        }
        undeployAllEar();
        undeployAllJar();

        useEar("autoload/mejb");
    }

    /**
     * Test all about the navigation
     * @throws Exception if error occurs
     *
     */
    public void testAll() throws Exception {

        Vector viewedURLs = new Vector();
        WebResponse wr;

        viewedURLs.add(URL_JONASADMIN);

        // Disable errors of javascript
        HttpUnitOptions.setExceptionsThrownOnScriptError(false);
        // Disable exception thrown on error status
        HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);

        // add links
        // deploy ear
        if (jProp.isEar()) {
            useEar("alarm");
            if (jProp.isResource()) {
                if (configFile != null) {
                    if (configFile.get(NAVIGATION_RAR_IN_EAR).equals("on")) {
                        useEar("earsample");
                    }
                } else {
                    useEar("earsample");
                }
            }
            // Reload the page
            wc.getResponse(getAbsoluteUrl(URL_JONASADMIN));
        }

        // Get the frame "tree"
        WebResponse treeFrame = wc.getFrameContents(FRAME_TREE);

        // Open tree
        try {
            treeFrame = JonasAdminTree.openTree(treeFrame, wc);
        } catch (IOException e) {
            fail("failed to open tree. " + e);
        }

        // When EJB service is actived
        if (jProp.isEjb()) {
            // Particular cases because of values of the session do not correspond at the page
            // 1-"/jonasAdmin/ExecuteEjb.do?action=synchronize"
            testParticularCase("name=AlarmRecord", "synchronize", URL_JONASADMIN_SYNCHRONIZE, viewedURLs);

            // 2-"/jonasAdmin/ExecuteEjb.do?action=reduceCache"
            testParticularCase("name=AlarmRecord", "reduceCache", URL_JONASADMIN_REDUCE, viewedURLs);

            // 3-"/jonasAdmin/ExecuteEjb.do?action=reducePool"
            testParticularCase("EditEjbSbl.do?select=jonas:j2eeType=StatelessSessionBean,name=MEJB,EJBModule=mejb,J2EEApplication=mejb,J2EEServer=jonas", "reducePool", URL_JONASADMIN_REDUCE_POOL, viewedURLs);
        }

        // 4-"/jonasAdmin/EditNewGroupMemoryRealm.do?action=create"
        testParticularCase("memrlm_1", "Groups.do", "action=create", URL_JONASADMIN_NEW_GROUP, viewedURLs);

        // 5-"/jonasAdmin/EditNewUserMemoryRealm.do?action=create"
        testParticularCase("memrlm_1", "Users.do", "action=create", URL_JONASADMIN_NEW_USER, viewedURLs);

        // Verify all links are not viewTree.do
        try {
            verifyLinks(treeFrame, viewedURLs);
        } catch (IOException e) {
            throw new IllegalStateException("Problem to verify all links. " + e);
        } catch (Exception e) {
            throw new IllegalStateException("Problem to verify all links. " + e);
        }

        // Test MBean
        if (configFile.get(NAVIGATION_MBEAN).equals("on")) {
            testMBean(viewedURLs);
        }
    }

    /**
     * Verify all links in frame
     * @param frame The frame
     * @param viewedURLs All URLs have just been viewed
     * @throws IOException if error occurs to get response
     * @throws Exception if error occurs to get response
     */
    public void verifyLinks(WebResponse frame, Vector viewedURLs) throws Exception, IOException {

        WebLink[] links = null;

        // Disable errors of javascript
        HttpUnitOptions.setExceptionsThrownOnScriptError(false);
        // Disable exception thrown on error status
        HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);

        // Get links
        links = frame.getLinks();
        WebResponse wr;

        for (int i = 0; i < links.length; i++) {
            // Verify all links are not viewTree.do and don't contain "://"
            if (links[i].getURLString().indexOf("viewTree.do") == -1 && links[i].getURLString().indexOf("ListMBeanDetails.do") == -1 && links[i].getURLString().indexOf("://") == -1) {
                if (!viewedURLs.contains(links[i].getURLString())) {
                    executeTests(frame, links[i], viewedURLs);

                    // Verify content frame links
                    WebResponse contentFrame = wc.getFrameContents(FRAME_CONTENT);
                    verifyLinks(contentFrame, viewedURLs);
                }
            }
        }
    }

    /**
     * Test links and content titles for a particular case
     * @param treeLink the link of the tree frame to click
     * @param contentLink the link of the content frame to click
     * @param url the url to test
     * @param viewedURLs All URLs have just been viewed
     * @throws Exception if an error occurs
     */
    private void testParticularCase(String treeLink, String contentLink, String url, Vector viewedURLs) throws Exception {
        WebLink link;
        WebResponse wr;
        WebResponse frame;

        frame = wc.getFrameContents(FRAME_TREE);
        link = frame.getFirstMatchingLink( WebLink.MATCH_URL_STRING, treeLink);
        if (link == null) {
            throw new IllegalStateException("No link found for treelink : '" + treeLink + "'.");
        }
        wr = link.click();

        frame = wc.getFrameContents(FRAME_CONTENT);
        link = frame.getFirstMatchingLink( WebLink.MATCH_URL_STRING, contentLink);

        if (link == null) {
            throw new IllegalStateException("No link found for treelink : '" + treeLink + "'.");
        }
        executeTests(frame, link, viewedURLs);
    }

    /**
     * Test links and content titles for a particular case
     * @param treeLink the link of the tree frame to click
     * @param contentLink the link of the content frame to click
     * @param contentLink2 the second link of the content frame to click
     * @param url the url to test
     * @param viewedURLs All URLs have just been viewed
     * @throws Exception if an error occurs
     */
    private void testParticularCase(String treeLink, String contentLink, String contentLink2, String url, Vector viewedURLs) throws Exception {

        WebLink link;
        WebResponse wr;
        WebResponse frame;

        frame = wc.getFrameContents(FRAME_TREE);
        link = frame.getFirstMatchingLink(WebLink.MATCH_URL_STRING, treeLink);
        if (link == null) {
            throw new IllegalStateException("No link found for treelink : '" + treeLink + "'.");
        }
        wr = link.click();

        frame = wc.getFrameContents(FRAME_CONTENT);
        link = frame.getFirstMatchingLink(WebLink.MATCH_URL_STRING, contentLink);
        if (link == null) {
            throw new IllegalStateException("No link found for contentLink : '" + contentLink + "'.");
        }
        wr = link.click();

        frame = wc.getFrameContents(FRAME_CONTENT);
        link = frame.getFirstMatchingLink(WebLink.MATCH_URL_STRING, contentLink2);
        if (link == null) {
            throw new IllegalStateException("No link found for contentLink2 : '" + contentLink2 + "'.");
        }

        executeTests(frame, link, viewedURLs);
    }

    /**
     * call the tests
     * @param frame The frame where the link is
     * @param link A no null link
     * @param viewedURLs All URLs have just been viewed
     * @throws SAXException If an error occurs
     */
    private void executeTests(WebResponse frame, WebLink link, Vector viewedURLs) throws SAXException {

        WebResponse wr;
        try {
            wr = link.click();
            // To check if the link is correct
            if (configFile.get(NAVIGATION_LINK).equals("on")) {
                testLinks(wr);
            }
        } catch (HttpNotFoundException e) {
            if (configFile.get(NAVIGATION_LINK).equals("on")) {
                fail("failed to click : link = " + link.getText() + ", URL = " + link.getURLString() + ", page = " + viewedURLs.lastElement() + ". " + e);
            }
        } catch (HttpException e) {
            if (configFile.get(NAVIGATION_LINK).equals("on")) {
                fail("failed to click : link = " + link.getText() + ", URL = " + link.getURLString() + ", page = " + viewedURLs.lastElement() + ". " + e);
            }
        } catch (IOException e) {
            if (configFile.get(NAVIGATION_LINK).equals("on")) {
                fail("failed to click : link = " + link.getText() + ", URL = " + link.getURLString() + ", page = " + viewedURLs.lastElement() + ". " + e);
            }
        }

        // To check if the content title is correct
        if (configFile.get(NAVIGATION_CONTENT_TITLE).equals("on")) {
            testContentTitle(link.getURLString());
        }

        // To check if the selected item is correct for links of the tree frame
        if (frame.getFrameName().equalsIgnoreCase(FRAME_TREE)) {
            if (configFile.get(NAVIGATION_SELECTED_ITEM).equals("on")) {
                testTreeSelected(link.getURLString());
            }
        }

        // To check if the title tom thumb is correct
        if (configFile.get(NAVIGATION_TITLE_TOM_THUMB).equals("on")) {
            testTitleTomThumb(link.getURLString());
        }

        // Add URL to viewed URLs
        viewedURLs.add(link.getURLString());
    }

    /**
     * Test if the response is a success
     * @param wr
     */
    private void testLinks(WebResponse wr) {
        assertEquals("a code of 200 (success) should be returned. ", HTTP_SUCCESS_CODE, wr.getResponseCode());
    }

    /**
     * Test jonasAdmin content titles
     * @param url The url of the link that was clicked
     * @throws SAXException if an error occurs when getElementsWithAttribute is called
     */
    private void testContentTitle(String url) throws SAXException {

        WebResponse tree = null;
        WebResponse content = null;
        String treeTitle = null;
        String contentTitle = null;

        // Verify content title of the content frame
        content = wc.getFrameContents(FRAME_CONTENT);
        tree = wc.getFrameContents(FRAME_TREE);

        // Get title from the tree frame
        if (tree.getElementsWithAttribute("class", CLASS_TREE_CONTROL_SELECTED).length == 1) {
            treeTitle = new String(tree.getElementsWithAttribute("class", CLASS_TREE_CONTROL_SELECTED)[0].getText());
        }

        // Get title from the content frame
        if (content.getElementsWithAttribute("class", "contentTitle").length == 1) {
            contentTitle = new String(content.getElementsWithAttribute("class", "contentTitle")[0].getText());
        }
        if (treeTitle != null && contentTitle != null) {
            assertEquals("The content title (" + contentTitle + ") is different of the link name (" + treeTitle + "). Clicked link was " + url, treeTitle, contentTitle);
        } else {
            if (!url.equals(URL_JONASADMIN)) {
                if (treeTitle != null) {
                    try {
                        fail("Content title is null. Name of the selected link was " + treeTitle + ". Link URL: " + url + " Content frame: " + content.getText());
                    } catch (IOException e) {
                        fail("Content title is null. Name of the selected link was " + treeTitle + ". Link URL: " + url + " Error to get content frame text");
                    }
                } else {
                    if (contentTitle != null) {
                        fail("No link is selected. Content title was " + contentTitle);
                    } else {
                        try {
                            fail("No link and no content title are found. Tree frame: " + tree.getText() + ".  Content frame: " + content.getText());
                        } catch (IOException e) {
                            fail("No link and no content title are found because there is a problem in tree frame or content frame. ");
                        }
                    }
                }
            }
        }

        // To free memory
        content = null;
        tree = null;
    }

    /**
     * Test jonasAdmin selection of tree items
     * @param selectedLink The url of the link that was clicked
     */
    private void testTreeSelected(String selectedLink) {

        WebResponse tree = null;

        // Verify tree frame
        tree = wc.getFrameContents(FRAME_TREE);

        try {
            if (!JonasAdminTree.treeControlSelected(tree, selectedLink)) {
                fail("The name of the link " + selectedLink + " is not selected in the tree.");
            }
        } catch (SAXException sax) {
            fail("" + sax);
        }

        tree = null;
    }

    /**
     * Test the jonasAdmin title tom thumb
     * @param url The url of the link that was clicked
     */
    private void testTitleTomThumb(String url) {
        WebResponse contentFrame = wc.getFrameContents(FRAME_CONTENT);
        WebResponse treeFrame = wc.getFrameContents(FRAME_TREE);
        WebTable thumbTable = null;
        WebTable treeTable = null;
        TableCell thumbCell = null;
        TableCell treeCell = null;
        String text = null;
        String[] thumbs = null;
        String[] treeThumbs = null;
        JonasAdminUtils utils = new JonasAdminUtils();
        int row;
        int column;

        try {
            thumbTable = contentFrame.getTables()[0];
        } catch (SAXException e) {
            fail("There is no table in content frame: " + e);
        }

        thumbCell = thumbTable.getTableCell(0, 0);
        text = thumbCell.getText();
        thumbs = text.split(" > ");


        try {
            treeTable = treeFrame.getTables()[0];
        } catch (SAXException e) {
            fail("There is no table in tree frame: " + e);
        }
        JonasAdminUtils.CoordCell coord = utils.getSelectedItemRow(treeTable);

        if (!url.equals(URL_JONASADMIN)) {
            row = coord.getRow().intValue();
            column = coord.getColumn().intValue();
            // get parents of the selected item
            treeThumbs = new String[thumbs.length];
            int i = thumbs.length;
            while (row > 0  && column > 3 && i > 0) {
                boolean found = false;
                column--;
                i--;
                // get the row parent
                int nextRow = row;
                while (nextRow > 0 && !found) {
                    nextRow--;
                    treeCell = treeTable.getTableCell(nextRow, column-2);
                    if (treeCell.getElementsWithAttribute("src", "images/tree/node_open_middle.gif").length == 1) {
                        treeCell = treeTable.getTableCell(nextRow, column);
                        treeThumbs[i] = treeCell.getText();
                        found = true;
                        assertEquals("For the link " + url, treeThumbs[i], thumbs[i]);
                    } else if (treeCell.getElementsWithAttribute("src", "images/tree/node_open_last.gif").length == 1) {
                        treeCell = treeTable.getTableCell(nextRow, column);
                        treeThumbs[i] = treeCell.getText();
                        found = true;
                        assertEquals("For the link " + url, treeThumbs[i], thumbs[i]);
                    } else if (treeCell.getElementsWithAttribute("src", "images/tree/node_open_first.gif").length == 1) {
                        treeCell = treeTable.getTableCell(nextRow, column);
                        treeThumbs[i] = treeCell.getText();
                        found = true;
                        assertEquals("For the link " + url, treeThumbs[i], thumbs[i]);
                    } else if (treeCell.getElementsWithAttribute("src", "images/tree/node_open.gif").length == 1) {
                        treeCell = treeTable.getTableCell(nextRow, column);
                        treeThumbs[i] = treeCell.getText();
                        found = true;
                        assertEquals("For the link " + url, treeThumbs[i], thumbs[i]);
                    }
                }
                row = nextRow;
            }
        }
    }

    /**
     * Test links, content titles, selected tree of mbeans
     * @throws Exception if an error occurs
     */
    private void testMBean(Vector viewedURLs) throws Exception {
        WebResponse wr = wc.getFrameContents(FRAME_TREE);
        WebLink link;

        // Open MBean branch
        if (wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*j2ee") == null) {
            link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans");
            if (link == null) {
                throw new IllegalStateException("The link viewTree.do?domain*mbeans is not found in tree. ");
            }
            wr = link.click();
        }
        // J2EE MBeans
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*j2ee");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*j2ee is not found in tree. ");
        }
        wr = link.click();

        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*j2ee*");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*j2ee* is not found in tree. ");
        }
        wr = link.click();

        link = wr.getMatchingLinks(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*j2ee*")[1];
        if (link == null) {
            throw new IllegalStateException("The 2nd link viewTree.do?domain*mbeans*j2ee* is not found in tree. ");
        }
        wr = link.click();
        // OWNER MBeans
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*owner");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*owner is not found in tree. ");
        }
        wr = link.click();

        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*owner*");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*owner* is not found in tree. ");
        }
        wr = link.click();

        link = wr.getMatchingLinks(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*owner*")[1];
        if (link == null) {
            throw new IllegalStateException("The 2nd link viewTree.do?domain*mbeans*owner* is not found in tree. ");
        }
        wr = link.click();

        // OTHER MBeans
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*unknown");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*unknow is not found in tree. ");
        }
        wr = link.click();

        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*unknown*");
        if (link == null) {
            throw new IllegalStateException("The link viewTree.do?domain*mbeans*unknow* is not found in tree. ");
        }
        wr = link.click();

        link = wr.getMatchingLinks(WebLink.MATCH_URL_STRING, "viewTree.do?tree=domain*mbeans*unknown*")[1];
        if (link == null) {
            throw new IllegalStateException("The 2nd link viewTree.do?domain*mbeans*unknow* is not found in tree. ");
        }
        wr = link.click();

        // Tests - ListMBeans.do
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListMBeans.do");
        if (link == null) {
            throw new IllegalStateException("ListMBeans.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListJ2eeMBeans.do
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListJ2eeMBeans.do");
        if (link == null) {
            throw new IllegalStateException("ListJ2eeMBeans.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListOwnerMBeans.do
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListOwnerMBeans.do");
        if (link == null) {
            throw new IllegalStateException("ListOwnerMBeans.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListUnknownMBeans.do
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListUnknownMBeans.do");
        if (link == null) {
            throw new IllegalStateException("ListUnknownMBeans.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListMBeanDetails.do
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListMBeanDetails.do");
        if (link == null) {
            throw new IllegalStateException("ListMBeanDetails.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListMBeanAttributes.do
        wr = wc.getFrameContents(FRAME_CONTENT);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListMBeanAttributes.do");
        if (link == null) {
            throw new IllegalStateException("ListMBeansAttributes.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListMBeanOperations.do
        wr = wc.getFrameContents(FRAME_CONTENT);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListMBeanOperations.do");
        if (link == null) {
            throw new IllegalStateException("ListMBeansOperations.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);

        // Tests - ListMBeanProperties.do
        wr = wc.getFrameContents(FRAME_CONTENT);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, "ListMBeanProperties.do");
        if (link == null) {
            throw new IllegalStateException("ListMBeansProperties.do was not found in tree. ");
        }
        executeTests(wr, link, viewedURLs);
    }

    /**
     * Tear Down cleanUp action
     * @throws Exception if an error occurs for close JMX connection
     */
    public void tearDown() throws Exception {
        super.tearDown();
        // Get time
        endTime = getTime();

        // Delete created files: server.xml
        JonasAdminFiles.recoverServerConf(beginTime, endTime);
    }
}
