/**
 * 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.service.container;

import java.io.File;

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.JonasAdminUtils;

import com.meterware.httpunit.Button;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Class for testing web container in jonasAdmin
 * @author Paul Kemler
 *
 */
public class F_JonasAdminWebContainer extends JonasAdminTestCase {

    /**
     * URL of Web containers
     */
    private static final String URL_JONASADMIN_WEB_CONTAINERS = "ListWebContainers.do";

    /**
     * URL of Web Container
     */
    private static final String URL_JONASADMIN_WEBAPP = "EditWebApp.do";

    /**
     * URL of edit war
     */
    private static final String URL_JONASADMIN_EDIT_WAR = "EditWar.do";

    /**
     * URL of servlets
     */
    private static final String URL_JONASADMIN_SERVLETS = "warServlets.jsp";

    /**
     * URL of web.xml
     */
    private static final String URL_JONASADMIN_WEB_XML = "warWebXml.jsp";

    /**
     * URL of web.xml
     */
    private static final String URL_JONASADMIN_JONAS_XML = "warJonasXml.jsp";

    /**
     * URL of web container: ALARM
     */
    private static final String URL_JONASADMIN_WEBAPP_ALARM = "alarm";

    /**
     * URL of the cancel button
     */
    private static final String BUTTON_SERVLET_CANCEL = "EditWebAppCatalina.do";

    /**
     * URL of the save all button
     */
    private static final String BUTTON_SERVLET_SAVE_ALL = "SaveCatalina.do";

    /**
     * URL of the stop button
     */
    private static final String BUTTON_WEBAPP_STOP = "javascript:submitHidden('webAppCatalinaForm','action','stop')";

    /**
     * URL of the start button
     */
    private static final String BUTTON_WEBAPP_START = "javascript:submitHidden('webAppCatalinaForm','action','start')";

    /**
     * URL of the reload button
     */
    private static final String BUTTON_WEBAPP_RELOAD = "javascript:submitHidden('webAppCatalinaForm','action','reload')";

    /**
     * number of tabs when you are in web containers
     */
    private static final int NUMBER_OF_TABS_FOR_WEB_CONTAINERS = 1;

    /**
     * number of tabs when you are in ROOT container
     */
    private static final int NUMBER_OF_TABS_FOR_ROOT = 6;

    /**
     * number of tabs when you are in stopped ROOT container
     */
    private static final int NUMBER_OF_TABS_FOR_STOPPED_ROOT = 4;

    /**
     * Constructor with a specified name
     * @param s name
     */
    public F_JonasAdminWebContainer(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_JonasAdminWebContainer(testtorun));
        }
    }

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

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

        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);
                }
            }
        }
    }

    /**
     * Test a Web Container that it is not in an ear
     * @throws Exception if error occurs
     *
     */
    public void testWebContainerNoInEar() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell cell;
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        // Go to web containers
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_WEB_CONTAINERS);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 1;

        // Verify there is a link for each context
        table = utils.getTable(wr, 0);
        for (int i = 1; i < table.getRowCount(); i++) {
            cell = table.getTableCell(i, 0);
            assertEquals("There is no one link for the context : " + cell.getText(), 1, cell.getLinks().length);
        }

        // Verify there is 1 tab
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_WEB_CONTAINERS, selectedTab, "Problem in 'Web Containers' tab.");

        // Go to ROOT
        int rowRoot = utils.getFirstRow("ROOT", table, 0).intValue();
        if (rowRoot == -1) {
            fail("'ROOT' is not found in the table of Web Containers page. ");
        }
        link = table.getTableCell(rowRoot, 0).getLinks()[0];
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 2;

        // Verify CONTEXT
        if (jProp.isCatalina()) {
            table = utils.getTable(wr, 4);
            testContextTableCatalina(table, "/", "localhost");
        } else {
            table = utils.getTable(wr, 1);
            testContextTableJetty(table, "/", "JOnAS Package index", "true");
        }

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        if (!jProp.isCatalina()) {
            if (NUMBER_OF_TABS_FOR_ROOT != (tabTable.getColumnCount() + 1) / 2) {
                // There is not Servlet tab.
                throw new IllegalStateException("Not implemented: JETTY. ");
            }
        }
        testTabs(tabTable, NUMBER_OF_TABS_FOR_ROOT, selectedTab, "Problem in 'ROOT' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_EDIT_WAR + " is not found in the third tab. ", tabTable.getTableCell(0,
                4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EDIT_WAR));
        assertTrue("The link " + URL_JONASADMIN_SERVLETS + " is not found in the fourth tab. ", tabTable.getTableCell(
                0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_SERVLETS));
        assertTrue("The link " + URL_JONASADMIN_WEB_XML + " is not found in the fifth tab. ", tabTable.getTableCell(0,
                8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_XML));
        assertTrue("The link " + URL_JONASADMIN_JONAS_XML + " is not found in the sixth tab. ", tabTable.getTableCell(
                0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_JONAS_XML));

        if (jProp.isCatalina()) {
            // Verify Save button
            wr = testSaveButton(wr);

            // Verify settings
            wr = testModifySettings(wr);

            // Verify actions
            wr = testActions(wr, utils);
        }

        // Go to EditWar
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_EDIT_WAR);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 3;

        // Verify Web container
        table = utils.getTable(wr, 1);
        testWebContainerTable(table, "", "localhost", "ctxroot.war", "true");

        // Verify it is not 'In application container'
        try {
            table = utils.getTable(wr, 3);
            fail("There is the 'In application container' table. ");
        } catch (Exception e) {
            // It is ok
        }

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_ROOT, selectedTab, "Problem in 'autoload/ctxroot.war' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_WEBAPP + " is not found in the second tab. ", tabTable.getTableCell(0,
                2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEBAPP));
        assertTrue("The link " + URL_JONASADMIN_SERVLETS + " is not found in the fourth tab. ", tabTable.getTableCell(
                0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_SERVLETS));
        assertTrue("The link " + URL_JONASADMIN_WEB_XML + " is not found in the fifth tab. ", tabTable.getTableCell(0,
                8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_XML));
        assertTrue("The link " + URL_JONASADMIN_JONAS_XML + " is not found in the sixth tab. ", tabTable.getTableCell(
                0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_JONAS_XML));

        // Go to Servlet tab
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVLETS);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 4;

        // Verify servlets
        wr = testServlet(wr, utils);

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_ROOT, selectedTab, "Problem in 'Servlets' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_WEBAPP + " is not found in the second tab. ", tabTable.getTableCell(0,
                2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEBAPP));
        assertTrue("The link " + URL_JONASADMIN_EDIT_WAR + " is not found in the third tab. ", tabTable.getTableCell(0,
                4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EDIT_WAR));
        assertTrue("The link " + URL_JONASADMIN_WEB_XML + " is not found in the fifth tab. ", tabTable.getTableCell(0,
                8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_XML));
        assertTrue("The link " + URL_JONASADMIN_JONAS_XML + " is not found in the sixth tab. ", tabTable.getTableCell(
                0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_JONAS_XML));

        // Go to Web.xml tab
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_WEB_XML);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 5;

        // Verify Web Xml
        // TODO

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_ROOT, selectedTab, "Problem in 'Web xml' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_WEBAPP + " is not found in the second tab. ", tabTable.getTableCell(0,
                2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEBAPP));
        assertTrue("The link " + URL_JONASADMIN_EDIT_WAR + " is not found in the third tab. ", tabTable.getTableCell(0,
                4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EDIT_WAR));
        assertTrue("The link " + URL_JONASADMIN_SERVLETS + " is not found in the fourth tab. ", tabTable.getTableCell(
                0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_SERVLETS));
        assertTrue("The link " + URL_JONASADMIN_JONAS_XML + " is not found in the sixth tab. ", tabTable.getTableCell(
                0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_JONAS_XML));

        // Go to Jonas Xml tab
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_JONAS_XML);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 6;

        // Verify jonas.xml
        // TODO

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_ROOT, selectedTab, "Problem in 'JOnAS xml' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_WEBAPP + " is not found in the second tab. ", tabTable.getTableCell(0,
                2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEBAPP));
        assertTrue("The link " + URL_JONASADMIN_EDIT_WAR + " is not found in the third tab. ", tabTable.getTableCell(0,
                4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EDIT_WAR));
        assertTrue("The link " + URL_JONASADMIN_SERVLETS + " is not found in the fourth tab. ", tabTable.getTableCell(
                0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_SERVLETS));
        assertTrue("The link " + URL_JONASADMIN_WEB_XML + " is not found in the fifth tab. ", tabTable.getTableCell(0,
                8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_XML));

    }

    /**
     * Test a Web Container that it is in an ear
     * @throws Exception if error occurs
     *
     */
    public void testWebContainerInEar() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        TableCell cell;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        // Add alarm.ear
        useEar("alarm");

        // Go to web containers
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_WEB_CONTAINERS);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);

        // Go to ALARM
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_WEBAPP_ALARM);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);

        // Go to EditWar
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_EDIT_WAR);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);

        // Verify 'In application container'
        table = utils.getTable(wr, 1);
        try {
            table = utils.getTable(wr, 4);
        } catch (Exception e) {
            fail("There is not the 'In application container' table. ");
        }
        testInApplicationContainer(table);

    }

    /**
     * Test path and host in the context table
     * @param table the context table
     * @param path the expected value of the path
     * @param host the expected value of the host
     * @throws Exception if an error occurs for going to the url
     */
    private void testContextTableCatalina(WebTable table, String path, String host) throws Exception {
        WebConversation wc2 = new WebConversation();
        // Path         /
        assertEquals("It is not the expected path. ", path, table.getTableCell(0, 2).getText());
        String urlRoot = prefixUrl + path;
        int code = wc2.getResponse(urlRoot).getResponseCode();
        assertEquals("The page " + urlRoot + " is not found. ", 200, code);
        // Host         localhost
        assertEquals("It is not the expected host. ", host, table.getTableCell(1, 2).getText());
    }

    /**
     * Test path and host in the context table
     * @param table the context table
     * @param path the expected value of the path
     * @param name the expected value of the context name
     * @param started the expected value of 'Started'
     * @throws Exception if an error occurs for going to the url
     */
    private void testContextTableJetty(WebTable table, String path, String name, String started) throws Exception {
        WebConversation wc2 = new WebConversation();
        // Path         /
        assertEquals("It is not the expected path. ", path, table.getTableCell(0, 2).getText());
        String urlRoot = prefixUrl + path;
        int code = wc2.getResponse(urlRoot).getResponseCode();
        assertEquals("The page " + urlRoot + " is not found. ", 200, code);
        // Name         JOnAS Package index
        assertEquals("It is not the expected name of the context. ", name, table.getTableCell(1, 2).getText());
        // Started      true
        assertEquals("It is not the expected value of 'Started'. ", started, table.getTableCell(2, 2).getText());
    }

    /**
     * Test context root, host name, path and Java 2 delegation model in the web container table
     * @param table the web container table
     * @param contextRoot the expected value of the context root
     * @param hostName the expected value of the host name
     * @param path the end of the path value
     * @param model the expected value of the Java 2 delegation model
     */
    private void testWebContainerTable(WebTable table, String contextRoot, String hostName, String path, String model) {
        // Context root
        assertEquals("It is not the context root. ", contextRoot, table.getTableCell(0, 2).getText());
        // Host name       localhost
        assertEquals("It is not the host name. ", hostName, table.getTableCell(1, 2).getText());
        // Path        /home/kemlerp/base/jonas/webapps/autoload/ctxroot.war
        assertTrue("It is not the end of the path. ", table.getTableCell(2, 2).getText().endsWith(path));
        // Java 2 delegation model     true
        assertEquals("It is not the Java 2 delegation model value. ", model, table.getTableCell(3, 2).getText());
    }

    /**
     * Test if the save button goes to the servlet server page and if save all and cancel buttons return to the previous page
     * @param wr the content frame in the WebApp tab and catalina is used.
     * @return wr the content frame in the WebApp tab.
     * @throws Exception if an error occurs.
     */
    private WebResponse testSaveButton(WebResponse wr) throws Exception {
        String beginTime;
        String endTime;
        WebLink link;
        WebForm form = wr.getForms()[0];
        String previousPage = wr.getElementsWithAttribute("class", "contentTitle")[0].getText();
        // Get the 3rd button : 'btnSave'
        Button button = form.getButtons()[2];
        button.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        assertTrue("The save button doesn't go to 'jonasserver/servletServer.jsp'. ", wr.getText().indexOf(
                "jonasserver/servletServer.jsp") != -1);
        // Get time
        beginTime = getTime();
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, BUTTON_SERVLET_SAVE_ALL);
        WebResponse wrSaveAll = wc.sendRequest(link.getRequest());
        // Get time
        endTime = getTime();
        // Delete created files: server.xml
        JonasAdminFiles.recoverServerConf(beginTime, endTime);
        assertEquals("The cancel button of the servlet server page doesn't go back to '" + previousPage + "' page. !",
                previousPage, wrSaveAll.getElementsWithAttribute("class", "contentTitle")[0].getText());

        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, BUTTON_SERVLET_CANCEL);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        assertEquals("The cancel button of the servlet server page doesn't go back to '" + previousPage + "' page. !",
                previousPage, wr.getElementsWithAttribute("class", "contentTitle")[0].getText());

        return wr;
    }

    /**
     * Test modification of settings
     * @param wr the content frame in the WebApp tab.
     * @return wr the content frame in the WebApp tab.
     * @throws Exception if an error occurs.
     */
    private WebResponse testModifySettings(WebResponse wr) throws Exception {
        WebForm form = wr.getForms()[0];
        form.setParameter("cookies", "false");
        form.setParameter("reloadable", "true");
        form.setParameter("swallowOutput", "true");
        Button button = form.getSubmitButton("btn_apply");
        button.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        form = wr.getForms()[0];
        String cookies = form.getParameterValue("cookies");
        String reloadable = form.getParameterValue("reloadable");
        String swallowOutput = form.getParameterValue("swallowOutput");

        assertEquals("The value of the cookie parameter is not 'false'. ", "false", cookies);
        assertEquals("The value of the reloadable parameter is not 'true'. ", "true", reloadable);
        assertEquals("The value of the swallowOutput parameter is not 'true'. ", "true", swallowOutput);

        return wr;
    }

    /**
     * Test actions Start, Stop and Reload
     * @param wr the content frame in the WebApp tab.
     * @param selectedTab the selected Tab.
     * @return the content frame in the WebApp tab.
     * @throws Exception if an error occurs.
     */
    private WebResponse testActions(WebResponse wr, JonasAdminUtils utils) throws Exception {
        WebLink link;
        TableCell reloadCell;
        TableCell startCell;
        TableCell stopCell;
        WebTable tabTable;
        WebConversation wc2 = new WebConversation();
        int selectedTab = 2;
        WebTable table = utils.getTable(wr, 1).getTableCell(0, 0).getTables()[0];
        startCell = table.getTableCell(0, 1);
        // No link for start button
        assertEquals("There is a link for the 'Start' button. ", 0, startCell.getLinks().length);
        // Disabled button
        assertEquals("The 'Start' button is not grey. ", 1,
                startCell.getElementsWithAttribute("class", "btnDisabled").length);

        // Stop action
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, BUTTON_WEBAPP_STOP);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        table = utils.getTable(wr, 1).getTableCell(0, 0).getTables()[0];
        reloadCell = table.getTableCell(0, 0);
        startCell = table.getTableCell(0, 1);
        stopCell = table.getTableCell(0, 2);
        // No link for stop button and reload button
        assertEquals("There is a link for the 'Reload' button. ", 0, reloadCell.getLinks().length);
        assertEquals("There is a link for the 'Stop' button. ", 0, stopCell.getLinks().length);
        // Disabled button
        assertEquals("The 'Reload' button is not grey. ", 1, reloadCell
                .getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Start' button is grey. ", 0,
                startCell.getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Stop' button is not grey. ", 1,
                stopCell.getElementsWithAttribute("class", "btnDisabled").length);
        // Verify URL
        String urlRoot = prefixUrl + "/";
        int code = wc2.getResponse(urlRoot).getResponseCode();
        assertTrue("The page " + urlRoot + " is found. ", code != 200);

        // Verify tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_FOR_STOPPED_ROOT, selectedTab, "Problem in 'ROOT' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_WEB_CONTAINERS + " is not found in the first tab. ", tabTable
                .getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_WEB_CONTAINERS));
        assertTrue("The link " + URL_JONASADMIN_EDIT_WAR + " is not found in the second tab. ", tabTable.getTableCell(
                0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EDIT_WAR));
        assertTrue("The link " + URL_JONASADMIN_JONAS_XML + " is not found in the sixth tab. ", tabTable.getTableCell(
                0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_JONAS_XML));

        // Start action
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, BUTTON_WEBAPP_START);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        table = utils.getTable(wr, 1).getTableCell(0, 0).getTables()[0];
        reloadCell = table.getTableCell(0, 0);
        startCell = table.getTableCell(0, 1);
        stopCell = table.getTableCell(0, 2);
        // No link for start button
        assertEquals("There is a link for the 'Start' button. ", 0, startCell.getLinks().length);
        // Disabled button
        assertEquals("The 'Reload' button is grey. ", 0,
                reloadCell.getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Start' button is not grey. ", 1,
                startCell.getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Stop' button is grey. ", 0, stopCell.getElementsWithAttribute("class", "btnDisabled").length);
        // Verify URL
        code = wc2.getResponse(urlRoot).getResponseCode();
        assertEquals("The page " + urlRoot + " is not found. ", 200, code);

        // Reload action
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, BUTTON_WEBAPP_RELOAD);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        table = utils.getTable(wr, 1).getTableCell(0, 0).getTables()[0];
        reloadCell = table.getTableCell(0, 0);
        startCell = table.getTableCell(0, 1);
        stopCell = table.getTableCell(0, 2);
        // No link for start button
        assertEquals("There is a link for the 'Start' button. ", 0, startCell.getLinks().length);
        // Disabled button
        assertEquals("The 'Reload' button is grey. ", 0,
                reloadCell.getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Start' button is not grey. ", 1,
                startCell.getElementsWithAttribute("class", "btnDisabled").length);
        assertEquals("The 'Stop' button is grey. ", 0, stopCell.getElementsWithAttribute("class", "btnDisabled").length);
        // Verify URL
        code = wc2.getResponse(urlRoot).getResponseCode();
        assertEquals("The page " + urlRoot + " is not found. ", 200, code);

        return wr;
    }

    /**
     * Test servlet infos
     * @param wr the content frame in the Servlet tab.
     * @param utils jonasAdmin utils
     * @return WebResponse the content frame
     * @throws Exception if an error occurs
     */
    private WebResponse testServlet(WebResponse wr, JonasAdminUtils utils) throws Exception {
        WebLink link;
        WebTable table;
        table = utils.getTable(wr, 0);
        int nbServlets = Integer.parseInt(table.getTableCell(0, 2).getText());

        table = utils.getTable(wr, 2);
        // Number of rows = number of servlets
        assertEquals("There are not '" + nbServlets + "' rows in the 'Name' table. ", nbServlets, table.getRowCount());

        // Verify Servlets with a MBean link
        for (int i = 0; i < nbServlets; i++) {
            String name = table.getTableCell(i, 0).getText();
            int code = wc.getResponse(prefixUrl + "/jonasAdmin/ListMBeanDetails.do?select=" + name).getResponseCode();
            assertEquals("The MBean details for '" + name + "' are not found. ", 200, code);
        }

        wc.getResponse(urlWelcome);
        wr = wc.getFrameContents(FRAME_TREE);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_WEB_CONTAINERS);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        table = utils.getTable(wr, 0);
        int rowRoot = utils.getFirstRow("ROOT", table, 0).intValue();
        if (rowRoot != -1) {
            link = table.getTableCell(rowRoot, 0).getLinks()[0];
            link.click();
        } else {
            fail("The WebContainer 'ROOT' was not found in the web container list. ");
        }
        wr = wc.getFrameContents(FRAME_CONTENT);
        link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVLETS);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);

        return wr;
    }

    private void testInApplicationContainer(WebTable table) throws Exception {
        // Verify path
        String path = table.getTableCell(0, 2).getText();
        assertTrue("The path doesn't end with 'apps" + File.separator + "alarm.ear'. ", path.endsWith("apps"
                + File.separator + "alarm.ear"));

        // Verify link
        String name = table.getTableCell(0, 0).getText();
        assertEquals("The name of the application container is not 'alarm.ear'. ", "alarm.ear", name);
        WebLink link = table.getTableCell(0, 0).getLinks()[0];
        try {
            int code = wc.sendRequest(link.getRequest()).getResponseCode();
            assertEquals("The page 'alarm.ear' is not found (link = '" + link.getURLString() + "'). ", 200, code);
        } catch (Exception e) {
            fail("The link '" + link.getURLString() + "' creates an error. ");
        }
    }

    /**
     * Tear Down cleanUp action
     * @throws Exception if an error occurs
     */
    public void tearDown() throws Exception {
        super.tearDown();
    }
}
