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

import java.io.File;

import javax.management.ObjectName;

import junit.framework.TestSuite;

import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
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 resource service
 * @author Paul Kemler
 *
 */
public class F_JonasAdminResource extends JonasAdminTestCase {

    /**
     * URL of Resource Adapters
     */
    private static final String URL_JONASADMIN_RESOURCE = "ListResourceAdapters.do";

    /**
     * URL of earsample.ra-sample
     */
    private static final String URL_JONASADMIN_RA_SAMPLE = "earsample.ra-sample";

    /**
     * URL of db_access_jdbc1
     */
    private static final String URL_JONASADMIN_JONAS_JDBC = "db_access_jdbc1";

    /**
     * URL of Statistics tab
     */
    private static final String URL_JONASADMIN_STATISTICS = "EditResourceAdapterStat.do";

    /**
     * URL of Used by
     */
    private static final String URL_JONASADMIN_USED_BY = "rarUsedBy";

    /**
     * URL of ErsatzEIS
     */
    private static final String URL_JONASADMIN_ERSATZ = "ErsatzEIS";

    /**
     * URL of connection factories
     */
    private static final String URL_JONASADMIN_CONNECTION_FACTORIES = "service/rar15CFs.jsp";

    /**
     * URL of activationSpecs
     */
    private static final String URL_JONASADMIN_ACTIVATION_SPECS = "service/rar15ASs.jsp";

    /**
     * URL of CF Statistics
     */
    private static final String URL_JONASADMIN_STATISTICS_CF = "EditResourceAdapterCFStat.do";

    /**
     * URL of Resource Adapter
     */
    private static final String URL_JONASADMIN_RA = "EditResourceAdapter.do";

    /**
     * URL of Resource Adapter CF
     */
    private static final String URL_JONASADMIN_RA_CF = "EditResourceAdapterCF.do";

    /**
     * number of tabs when you are in resource
     */
    private static final int NUMBER_OF_TABS_FOR_RESOURCE = 1;

    /**
     * number of tabs when you are in earsample.ra-sample
     */
    private static final int NUMBER_OF_TABS_FOR_RA_SAMPLE = 2;

    /**
     * number of tabs when you are in 'db_access_jdbc1' tab
     */
    private static final int NUMBER_OF_TABS_FOR_JDBC = 4;

    /**
     * number of tabs when you are in 'ErsatzEIS'
     */
    private static final int NUMBER_OF_TABS_RA15 = 4;

    /**
     * number of tabs for a connection factory
     */
    private static final int NUMBER_OF_TABS_CF = 6;

    /**
     * number of tabs for a activation spec
     */
    private static final int NUMBER_OF_TABS_AS = 5;

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

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_JonasAdminResource.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 Resource service with rar in an ear.
     * @throws Exception if error occurs
     *
     */
    public void testResourceInEar() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        int selectedTab;
        String name;
        String file = null;
        String path = null;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        // Test only if the resource service is enabled
        if (jProp.isResource()) {

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

            // Go to resource adapters
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_RESOURCE);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 1;

            // Verify 'Resource Adapters' page
            table = utils.getTable(wr, 0);
            testResourceAdapterNames(table);
            name = "earsample.ra-sample";
            int rowRASample = utils.getFirstRow(name, table, 0).intValue();
            if (rowRASample != -1) {
                // Verify File
                file = table.getTableCell(rowRASample, 1).getText();
                assertEquals("It is not the name of the file of the 'earsample.ra-sample'. ", "ra-sample.rar", file);
                // Verify Path
                path = table.getTableCell(rowRASample, 2).getText();
                assertTrue("It is not the end of the path of the 'earsample.ra-sample'. ", path.endsWith(File.separator + "ra-sample.rar"));
            } else {
                fail("The RA 'earsample.ra-sample' is not found in the " + table.getTableCell(0, 0).getText() + ". ");
            }

            // Verify there is 1 tab
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_RESOURCE, selectedTab, "Problem in 'Resource Adapter' tab.");


            // Go to 'earsample.ra-sample'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_RA_SAMPLE);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

            // Verify Resource Adapter Infos
            table = utils.getTable(wr, 1);
            testRAInfos(table, name, file, path);

            // Verify 'In application'
            table = utils.getTable(wr, 4);
            testInApplication(table);

            // Verify 'Properties'
            table = utils.getTable(wr, 7);
            testEarSampleProperties(table);

            // Verify there are 2 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_RA_SAMPLE, selectedTab, "Problem in 'earsample.ra-sample' tab.");
        }

    }

    /**
     * Test Resource service with a JONAS_jdbc rar.
     * @throws Exception if error occurs
     *
     */
    public void testResource10Jdbc() throws Exception {

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

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

        // Test only if the resource service is enabled
        if (jProp.isResource()) {
            // Add db_access_jdbc1
            useRar("db_access_jdbc1");

            // Add alarm.ear to add the 4th tab "used by"
            unUseEar("alarm");
            useEar("alarm");

            // Go to resource adapters
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_RESOURCE);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to 'db_access_jdbc1'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_JONAS_JDBC);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

            // Verify PoolProperties
            testPoolProperties(wr, wc);

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JDBC, selectedTab, "Problem in 'db_access_jdbc1' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_STATISTICS + " is not found in the third tab. ",
                    tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_STATISTICS));
            assertTrue("The link " + URL_JONASADMIN_USED_BY + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_USED_BY) != -1);


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

            // Verify Connections Pool State
            table = utils.getTable(wr, 1);
            testConnectionsPoolState(table);

            // Verify Statistics on getConnection operations
            table = utils.getTable(wr, 4);
            testStatisticsOnGetConnectionOperations(table, wr, wc, utils);

            // Verify Statistics on waiters
            table = utils.getTable(wr, 7);
            testStatisticsOnWaiters(table);

            // Verify Connection to database statistics
            table = utils.getTable(wr, 10);
            testConnectionToDatabaseStatistics(table);

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JDBC, selectedTab, "Problem in 'Statistics' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RA));
            assertTrue("The link " + URL_JONASADMIN_USED_BY + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_USED_BY) != -1);


            // Go to 'Used by'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_USED_BY);
            if (link == null) {
                throw new IllegalStateException("The link " + URL_JONASADMIN_USED_BY  + " is not found from 'Statistics' tab. ");
            }
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 4;

            // Verify 'Used by'
            table = utils.getTable(wr, 0);
            testUsedBy(table);

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JDBC, selectedTab, "Problem in 'Used by' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RA));
            assertTrue("The link " + URL_JONASADMIN_STATISTICS + " is not found in the third tab. ",
                    tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_STATISTICS));
        }
    }

    /**
     * Test Resource Adapter 1.5 with ErsatzEIS.rar
     * @throws Exception if an error occurs
     */
    public void testResource15() throws Exception {
        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        // Test only if the resource service is enabled
        if (jProp.isResource()) {
            // Add ErsatzEIS.rar
            useRar("ErsatzEIS");

            // Go to resource adapters
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_RESOURCE);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to 'ErsatzEIS.rar'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_ERSATZ);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_RA15, selectedTab, "Problem in 'ErsatzEIS' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_CONNECTION_FACTORIES + " is not found in the third tab. ",
                    tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONNECTION_FACTORIES));
            assertTrue("The link " + URL_JONASADMIN_ACTIVATION_SPECS + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_ACTIVATION_SPECS));


            // Go to 'Connection Factories'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_CONNECTION_FACTORIES);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 3;

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_RA15, selectedTab, "Problem in 'Connection Factories' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_RA) != -1);
            assertTrue("The link " + URL_JONASADMIN_ACTIVATION_SPECS + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_ACTIVATION_SPECS));

            // Test 'Connection Factories'
            table = utils.getTable(wr, 0);
            testConnectionFactories(table, wc, utils);


            // Go to 'ActivationSpecs'
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_ACTIVATION_SPECS);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 4;

            // Verify there are 4 tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_RA15, selectedTab, "Problem in 'ActivationSpecs' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
            assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_RA) != -1);
            assertTrue("The link " + URL_JONASADMIN_CONNECTION_FACTORIES + " is not found in the third tab. ",
                    tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONNECTION_FACTORIES));

            // Test 'ActivationSpecs'
            table = utils.getTable(wr, 0);
            testActivationSpecs(table, wc, utils);
        }
    }

    /**
     * Test 'Resource Adapters' table
     * @param table the 'Resource Adapters' table
     */
    private void testResourceAdapterNames(WebTable table) {
        TableCell cell;

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

    /**
     * Test the RA infos
     * @param table the RA info table
     * @param name the RA name
     * @param file the RA file
     * @param path the RA path
     */
    private void testRAInfos(WebTable table, String name, String file, String path) {
        assertEquals("It is not the name of the Resource Adapter. ", name, table.getTableCell(0, 2).getText());
        assertEquals("It is not the file of the Resource Adapter. ", file, table.getTableCell(1, 2).getText());
        assertEquals("It is not the path of the Resource Adapter. ", path, table.getTableCell(2, 2).getText());
    }

    /**
     * Test 'In Application' table
     * @param table 'In Application' table
     */
    private void testInApplication(WebTable table) {
        TableCell cell = table.getTableCell(0, 0);
        // There is 1 link
        assertEquals("There is not 1 link in 'In Application' table. ", 1, cell.getLinks().length);
        // earsample.ear
        assertEquals("There is not 'earsample.ear' in 'In Application' table. ", "earsample.ear", cell.getText());

        cell = table.getTableCell(0, 2);
        // path
        assertTrue("It is not the end of the path. ", cell.getText().endsWith(File.separator + "earsample.ear"));
    }

    /**
     * Test earsample.ra-sample properties
     * @param table the property table
     */
    private void testEarSampleProperties(WebTable table) {
        // EisURL           EIS_URL
        TableCell cell = table.getTableCell(0, 0);
        assertEquals("It is not the name of the 1st property. ", "EisURL", cell.getText());
        cell = table.getTableCell(0, 2);
        assertEquals("It is not the value of the 1st property. ", "EIS_URL", cell.getText());

        // jndiName         eisName
        cell = table.getTableCell(1, 0);
        assertEquals("It is not the name of the 2nd property. ", "jndiName", cell.getText());
        cell = table.getTableCell(1, 2);
        assertEquals("It is not the value of the 2nd property. ", "eisName", cell.getText());
    }

    /**
     * Test pool properties
     * @param wr the content frame of Resource Adapter page
     * @param wc the jonasAdmin WebConversation
     * @throws Exception if an error occurs
     */
    private void testPoolProperties(WebResponse wr, WebConversation wc) throws Exception {
        WebForm form = wr.getForms()[0];
        String jdbcConnCheckLevel = "0";
        String connMaxAge = "0";
        String maxOpentime = "0";
        String jdbcTestStatement = "";
        String maxSize = "-1";
        String minSize = "0";
        String maxWaitTime = "0";
        String maxWaiters = "0";
        String samplingPeriod = "30";

        ResourceUtils.modifyPoolProperties(form, jdbcConnCheckLevel, connMaxAge,
            maxOpentime, jdbcTestStatement, maxSize, minSize, maxWaitTime,
            maxWaiters, samplingPeriod);

        wr = wc.getFrameContents(FRAME_CONTENT);
        form = wr.getForms()[0];
        assertEquals("The value of 'Pool maximum' is not '-1'. ", maxSize, form.getParameterValue("maxSize"));
        assertEquals("The value of 'Maximum age' is not '" + connMaxAge + "'. ", connMaxAge, form.getParameterValue("connMaxAge"));
        assertEquals("The value of 'Maximum open time' is not '" + maxOpentime + "'. ", maxOpentime, form.getParameterValue("maxOpentime"));
        assertEquals("The value of 'Test' is not '" + jdbcTestStatement + "'. ", jdbcTestStatement, form.getParameterValue("jdbcTestStatement"));
        assertEquals("The value of 'Pool minimum' is not '" + minSize + "'. ", minSize, form.getParameterValue("minSize"));
        assertEquals("The value of 'Max Wait Time' is not '" + maxWaitTime + "'. ", maxWaitTime, form.getParameterValue("maxWaitTime"));
        assertEquals("The value of 'Max Waiters' is not '" + maxWaiters + "'. ", maxWaiters, form.getParameterValue("maxWaiters"));
        assertEquals("The value of 'Sampling Period' is not '" + samplingPeriod + "'. ", samplingPeriod, form.getParameterValue("samplingPeriod"));
        assertEquals("The value of 'Checking level' is not '" + jdbcConnCheckLevel + "'. ", jdbcConnCheckLevel, form.getParameterValue("jdbcConnCheckLevel"));
    }

    /**
     * Test Connections Pool State
     * @param table The connection pool state table
     */
    private void testConnectionsPoolState(WebTable table) throws Exception {
    	ObjectName jcaConnFactOn = getJCAConnectionFactory("jdbc_1", "db_access_jdbc1");
        // Current pool size
    	Integer val = getIntegerMBeanAttribute(jcaConnFactOn, "currentOpened");
    	String currentPoolsize = val.toString();
        //assertEquals("It is not the expected value of 'Current pool size'. ", "1", table.getTableCell(0, 2).getText());
    	assertEquals("It is not the expected value of 'Current pool size'. ", currentPoolsize, table.getTableCell(0, 2).getText());
        // Busy connections
    	val = getIntegerMBeanAttribute(jcaConnFactOn, "currentBusy");
        //assertEquals("It is not the expected value of 'Busy connections'. ", "0", table.getTableCell(1, 2).getText());
    	assertEquals("It is not the expected value of 'Busy connections'. ", val.toString(), table.getTableCell(1, 2).getText());
        // Busy in transactions
    	val = getIntegerMBeanAttribute(jcaConnFactOn, "currentInTx");
        //assertEquals("It is not the expected value of 'Busy in transactions'. ", "0", table.getTableCell(2, 2).getText());
    	assertEquals("It is not the expected value of 'Busy in transactions'. ", val.toString(), table.getTableCell(2, 2).getText());
        // Min busy in last period
    	val = getIntegerMBeanAttribute(jcaConnFactOn, "busyMin");
        //assertEquals("It is not the expected value of 'Min busy in last period'. ", "0", table.getTableCell(3, 2).getText());
    	assertEquals("It is not the expected value of 'Min busy in last period'. ", val.toString(), table.getTableCell(3, 2).getText());
        // Max busy in last period
    	val = getIntegerMBeanAttribute(jcaConnFactOn, "busyMax");
        //assertEquals("It is not the expected value of 'Max busy in last period'. ", "0", table.getTableCell(4, 2).getText());
    	assertEquals("It is not the expected value of 'Max busy in last period'. ", val.toString(), table.getTableCell(4, 2).getText());
        // Connection leaks
    	val = getIntegerMBeanAttribute(jcaConnFactOn, "connectionLeaks");
        //assertEquals("It is not the expected value of 'Connection leaks'. ", "0", table.getTableCell(5, 2).getText());
        assertEquals("It is not the expected value of 'Connection leaks'. ", val.toString(), table.getTableCell(5, 2).getText());
    }

    /**
     * Test Statistics On GetConnection Operations
     * @param table The 'Statistics on getConnection operations' table
     * @param wr the content frame
     * @param wc the jonasAdmin connection
     * @param utils jonasAdmin utils
     * @throws Exception if an error occurs
     */
    private void testStatisticsOnGetConnectionOperations(WebTable table, WebResponse wr, WebConversation wc, JonasAdminUtils utils) throws Exception {
        Button button;
        int servedConnections = Integer.parseInt(table.getTableCell(0, 2).getText());
        // Create connections
        createConnections();
        //Refresh
        wc.sendRequest(wr.getRefreshRequest());
        wr = wc.getFrameContents(FRAME_CONTENT);
        table = utils.getTable(wr, 4);

        // Served connections       have increased
        assertTrue("The 'Served connections' number has not increased. ",servedConnections < Integer.parseInt(table.getTableCell(0, 2).getText()));
        // Rejected connections     0
        assertEquals("The value of 'Rejected connections' is not '0'. ", "0", table.getTableCell(1, 2).getText());
        // By waiter overflow       0
        assertEquals("The value of 'By waiter overflow' is not '0'. ", "0", table.getTableCell(2, 2).getText());
        // By waiter timeout        0
        assertEquals("The value of 'By waiter timeout' is not '0'. ", "0", table.getTableCell(3, 2).getText());
        // By any other reason      0
        assertEquals("The value of 'By any other reason' is not '0'. ", "0", table.getTableCell(4, 2).getText());
    }

    /**
     * Test 'Used By' infos and link
     * @param table the 'Used by' table
     * @throws Exception if an error occurs to send request.
     */
    private void testUsedBy(WebTable table) throws Exception {
        WebLink link;
        // Ejb         AlarmRecord
        assertEquals("It is not used by 'AlarmRecord'. ", "AlarmRecord", table.getTableCell(1, 0).getText());
        link = table.getTableCell(1, 0).getLinks()[0];
        int code = wc.sendRequest(link.getRequest()).getResponseCode();
        assertEquals("The '" + link.getURLString() + "' link creates a '" + code + "' error. ", 200, code);
        // Type        EntityBean
        assertEquals("It is not the Type of AlarmRecord. ", "EntityBean", table.getTableCell(1, 1).getText());
    }

    /**
     * Test Statistics On Waiters
     * @param table the 'Statistics On Waiters' table
     * @throws Exception if an error occurs.
     */
    private void testStatisticsOnWaiters(WebTable table) throws Exception {
        // Current number of waiters    0
        int nbWaiters = Integer.parseInt(table.getTableCell(0, 2).getText());
        // Max waiters                  0
        int maxWaiters = Integer.parseInt(table.getTableCell(1, 2).getText());
        // Max waiters in last period   0
        int maxWaitersLastPeriod = Integer.parseInt(table.getTableCell(2, 2).getText());
        // Total waiters                0
        int totalWaiters = Integer.parseInt(table.getTableCell(3, 2).getText());
        // Max waiting time             0
        int maxWaitingTime = Integer.parseInt(table.getTableCell(4, 2).getText());
        // Max waiting in last period   0
        int maxWaitingLastPeriod = Integer.parseInt(table.getTableCell(5, 2).getText());
        // Total waiting                0
        int totalWaiting = Integer.parseInt(table.getTableCell(6, 2).getText());

        // 0 < nbWaiters < maxWaitersLastPeriod < maxWaiters < totalWaiters
        assertTrue("The current number of waiters is not positif. ", 0 <= nbWaiters);
        assertTrue("Current number of waiters > Max waiters. ", nbWaiters <= maxWaitersLastPeriod);
        assertTrue("Max waiters > Max waiters in last period. ", maxWaitersLastPeriod <= maxWaiters);
        assertTrue("Max waiters in last period > Total waiters. ", maxWaiters <= totalWaiters);

        // 0 < maxWaitingLastPeriod < maxWaitingTime < totalWaiting
        assertTrue("Max waiting in last period < 0. ", 0 <= maxWaitingLastPeriod);
        assertTrue("Max waiting time < Max waiting in last period. ", maxWaitingLastPeriod <= maxWaitingTime);
        assertTrue("Max waiting time > Total waiting. ", maxWaitingTime <= totalWaiting);
    }

    /**
     * Test Connection To Database Statistics
     * @param table the 'Connection To Database Statistics' table
     */
    private void testConnectionToDatabaseStatistics(WebTable table) {
        // Number of connections        > 0
        int nbConnections = Integer.parseInt(table.getTableCell(0, 2).getText());
        // Number of failures           0
        int nbFailures = Integer.parseInt(table.getTableCell(1, 2).getText());

        assertTrue("The number of connections is not positif. ", 0 < nbConnections);
        assertEquals("The expected number of failures was '0'. ", 0, nbFailures);
    }

    /**
     * Test connection factories
     * @param table the 'connection factories' table
     * @param wc the jonasAdmin connection
     * @param utils the jonasAdmin utils
     * @throws Exception if an error occurs
     */
    private void testConnectionFactories(WebTable table, WebConversation wc, JonasAdminUtils utils) throws Exception {
        WebResponse wr;
        WebLink link;
        WebTable tabTable;
        TableCell cell;
        int selectedTab;

        cell = table.getTableCell(1,0);
        // Name     eis/ErsatzEIS
        assertEquals("It is not the expeted name of Connection Factory", "eis/ErsatzEIS", cell.getText());

        // Go to 'eis/ErsatzEIS'
        link = cell.getLinks()[0];
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 5;

        // Verify there are 6 tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_CF, selectedTab, "Problem in 'Connection Factory' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
        assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                tabTable.getTableCell(0, 2).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_RA) != -1);
        assertTrue("The link " + URL_JONASADMIN_CONNECTION_FACTORIES + " is not found in the third tab. ",
                tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONNECTION_FACTORIES));
        assertTrue("The link " + URL_JONASADMIN_ACTIVATION_SPECS + " is not found in the fourth tab. ",
                tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_ACTIVATION_SPECS));
        assertTrue("The link " + URL_JONASADMIN_STATISTICS_CF + " is not found in the sixth tab. ",
                tabTable.getTableCell(0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_STATISTICS_CF));

        // Verify there are 10 tables
        try {
            // Try to get the last table with 'Apply' button.
            table = utils.getTable(wr, 9);
        } catch (ArrayIndexOutOfBoundsException e) {
            fail("The number of tables is too little in the content frame of the connection factory page. ");
        }
        try {
            // Try to get the eleventh table that doesn't exist.
            table = utils.getTable(wr, 10);
            fail("The number of tables is too big in the content frame of the connection factory page. ");
        } catch (ArrayIndexOutOfBoundsException e) {
            // ok
        }

        // Go to 'Statistics'
        link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_STATISTICS_CF);
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 6;

        // Verify there are 6 tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_CF, selectedTab, "Problem in CF 'Statistics' tab. ");
        // - links
        assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
        assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                tabTable.getTableCell(0, 2).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_RA) != -1);
        assertTrue("The link " + URL_JONASADMIN_CONNECTION_FACTORIES + " is not found in the third tab. ",
                tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONNECTION_FACTORIES));
        assertTrue("The link " + URL_JONASADMIN_ACTIVATION_SPECS + " is not found in the fourth tab. ",
                tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_ACTIVATION_SPECS));
        assertTrue("The link " + URL_JONASADMIN_RA_CF + " is not found in the fifth tab. ",
                tabTable.getTableCell(0, 8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RA_CF));

        // Verify there are 13 tables
        try {
            // Try to get the last table with 'Apply' button.
            table = utils.getTable(wr, 12);
        } catch (ArrayIndexOutOfBoundsException e) {
            fail("The number of tables is too little in the content frame of the CF statistic page. ");
        }
        try {
            // Try to get the fourteenth table that doesn't exist.
            table = utils.getTable(wr, 13);
            fail("The number of tables is too big in the content frame of the CF statistic page. ");
        } catch (ArrayIndexOutOfBoundsException e) {
            // ok
        }

    }

    /**
     * Test Activation Specs
     * @param table the 'ActivationSpecs' table
     * @param wc the jonasAdmin connection
     * @param utils the jonasAdmin utils
     * @throws Exception if an error occurs
     */
    private void testActivationSpecs(WebTable table, WebConversation wc, JonasAdminUtils utils) throws Exception {
        WebResponse wr;
        WebLink link;
        WebTable tabTable;
        TableCell cell;
        int selectedTab;

        cell = table.getTableCell(1,0);
        // Name     eis/ErsatzAS
        assertEquals("It is not the expeted name of ActivationSpec", "eis/ErsatzAS", cell.getText());

        // Go to 'eis/ErsatzAS'
        link = cell.getLinks()[0];
        link.click();
        wr = wc.getFrameContents(FRAME_CONTENT);
        selectedTab = 5;

        // Verify there are 5 tabs
        tabTable = utils.getTabTable(wr);
        testTabs(tabTable, NUMBER_OF_TABS_AS, selectedTab, "Problem in 'ActivationSpecs' tab.");
        // - links
        assertTrue("The link " + URL_JONASADMIN_RESOURCE + " is not found in the first tab. ",
                tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_RESOURCE));
        assertTrue("The link " + URL_JONASADMIN_RA + " is not found in the second tab. ",
                tabTable.getTableCell(0, 2).getLinks()[0].getURLString().indexOf(URL_JONASADMIN_RA) != -1);
        assertTrue("The link " + URL_JONASADMIN_CONNECTION_FACTORIES + " is not found in the third tab. ",
                tabTable.getTableCell(0, 4).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONNECTION_FACTORIES));
        assertTrue("The link " + URL_JONASADMIN_ACTIVATION_SPECS + " is not found in the fourth tab. ",
                tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_ACTIVATION_SPECS));

        // Verify there are 5 tables
        try {
            // Try to get the last table with required properties.
            table = utils.getTable(wr, 4);
        } catch (ArrayIndexOutOfBoundsException e) {
            fail("The number of tables is too little in the content frame of the activation spec page. ");
        }
        try {
            // Try to get the eleventh table that doesn't exist.
            table = utils.getTable(wr, 5);
            fail("The number of tables is too big in the content frame of the activation spec page. ");
        } catch (ArrayIndexOutOfBoundsException e) {
            // ok
        }
    }

    /**
     * Use Alarm Demo for creating connections
     * @throws Exception if an error occurs
     */
    private void createConnections() throws Exception {
        WebConversation wc2 = new WebConversation();
        String urlAlarm = prefixUrl + "/alarm/secured/list.jsp";
        // Start the alarm demo
        wc2.setAuthorization("jonas", "jonas");
        WebResponse wr = wc2.getResponse(urlAlarm);
    }

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