/**
 * 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 java.util.Iterator;

import javax.management.MBeanServerConnection;
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.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 ejb container in jonasAdmin
 * @author Paul Kemler
 *
 */
public class F_JonasAdminEjbContainer extends JonasAdminTestCase {

    /**
     * URL of Ejb container
     */
    private static final String URL_JONASADMIN_EJB_CONTAINER = "ListContainers.do";

    /**
     * URL of the container tab
     */
    private static final String URL_JONASADMIN_CONTAINER = "/ViewContainer.do";

    /**
     * URL of the container jsp
     */
    private static final String URL_JONASADMIN_CONTAINER_JSP = "/jonasAdmin/service/container.jsp";

    /**
     * URL of the statistic tab
     */
    private static final String URL_JONASADMIN_STATISTICS = "/EditContainerStatistic.do";

    /**
     * URL of the ejb container's' statistics tab
     */
    private static final String URL_JONASADMIN_CONTAINERS_STATISTICS = "EditEjbContainersStatistic.do";

    /**
     * URL of the view ejb container tab
     */
    private static final String URL_JONASADMIN_VIEW_EJB = "/ViewContainerEjbs.do";

    /**
     * URL of the mdb tab
     */
    private static final String URL_JONASADMIN_MDB = "/jonasAdmin/service/ejbMessage.jsp";

    /**
     * URL of the entity bean tab
     */
    private static final String URL_JONASADMIN_EB = "/jonasAdmin/service/ejbEntity.jsp";

    /**
     * URL of the view ejb container jsp
     */
    private static final String URL_JONASADMIN_VIEW_EJB_JSP = "/jonasAdmin/service/containerEjbs.jsp";

    /**
     * URL of the ejb dependency jsp
     */
    private static final String URL_JONASADMIN_DEPENDENCIES = "/jonasAdmin/service/ejbDependency.jsp";

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

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

    /**
     * number of tabs when you are in the jar page
     */
    private static final int NUMBER_OF_TABS_FOR_JAR = 4;

    /**
     * number of tabs when you are in the MDB page
     */
    private static final int NUMBER_OF_TABS_FOR_MDB = 6;

    /**
     * number of tabs when you are in the EntityBean page when using dbm
     */
    private static final int NUMBER_OF_TABS_FOR_ENTITY_DBM = 6;
    /**
     * number of tabs when you are in the EntityBean page when using the RAR
     * (there is no Dependency tab in this case)
     */
    private static final int NUMBER_OF_TABS_FOR_ENTITY_RAR = 5;

    /**
     * index of the Mdb tab
     */
    private static final int MDB_TAB_INDEX = 5;

    /**
     * Name of Mdb tab
     */
    private static final String TDB_TAB_TEXT = "Mdb";
    /**
     * Constructor with a specified name
     * @param s name
     */
    public F_JonasAdminEjbContainer(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_JonasAdminEjbContainer(testtorun));
        }
    }

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_JonasAdminEjbContainer.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 ejb container with alarm.ear
     * @throws Exception if error occurs
     *
     */
    public void testEjbContainer() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "alarm";
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add alarm.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to alarm.jar page
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

            // Verify infos
            table = utils.getTable(wr, 0);
            cell = table.getTableCell(0,2);
            // Name
            assertEquals("It is not the name of the jar. ", ear, cell.getText());
            // Path
            cell = table.getTableCell(1, 2);
            assertTrue("It is not the path of the jar file. ", cell.getText().endsWith(File.separator + "alarm.jar"));

            // Verify tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JAR, selectedTab, "Problem in 'alarm.jar' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            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_VIEW_EJB + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB));


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

            // Verify stats
            table = utils.getTable(wr, 1);

            int alarmBMP = Integer.parseInt(table.getTableCell(1, 2).getText());
            int alarmCMP = Integer.parseInt(table.getTableCell(2, 2).getText());
            int alarmStateful = Integer.parseInt(table.getTableCell(3, 2).getText());
            int alarmStateless = Integer.parseInt(table.getTableCell(4, 2).getText());
            int alarmMDB = Integer.parseInt(table.getTableCell(5, 2).getText());
            int alarmTotal = Integer.parseInt(table.getTableCell(0, 2).getText());

            assertEquals("It is not the number of Entity with Bean-Managed Persistence in alarm.jar. ", 0, alarmBMP);
            assertEquals("It is not the number of Entity with Container-Managed Persistence in alarm.jar. ", 1, alarmCMP);
            assertEquals("It is not the number of Stateful Session Bean in alarm.jar. ", 1, alarmStateful);
            assertEquals("It is not the number of Stateless Session Bean in alarm.jar. ", 0, alarmStateless);
            assertEquals("It is not the number of Message Driven Bean in alarm.jar. ", 1, alarmMDB);
            assertEquals("It is not the number of EJBs in alarm.jar. ", 3, alarmTotal);

            // Verify tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JAR, selectedTab, "Problem in 'statistic' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            assertTrue("The link " + URL_JONASADMIN_CONTAINER_JSP + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER_JSP));
            assertTrue("The link " + URL_JONASADMIN_VIEW_EJB_JSP + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB_JSP));


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

            // Verify infos
            table = utils.getTable(wr, 0);
            // Name
            cell = table.getTableCell(1, 0);
            assertEquals("It is not the name of the EJB. ", "AlarmListener", cell.getText());
            cell = table.getTableCell(2, 0);
            assertEquals("It is not the name of the EJB. ", "AlarmRecord", cell.getText());
            cell = table.getTableCell(3, 0);
            assertEquals("It is not the name of the EJB. ", "View", cell.getText());
            // Type
            cell = table.getTableCell(1, 2);
            assertEquals("It is not the type of the AlarmListener EJB. ", "MessageDrivenBean", cell.getText());
            cell = table.getTableCell(2, 2);
            assertEquals("It is not the type of the AlarmRecord EJB. ", "EntityBean", cell.getText());
            cell = table.getTableCell(3, 2);
            assertEquals("It is not the type of the View EJB. ", "StatefulSessionBean", cell.getText());

            // Verify tabs
            tabTable = utils.getTabTable(wr);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_JAR, selectedTab, "Problem in 'EJBs' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            assertTrue("The link " + URL_JONASADMIN_CONTAINER + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER));
            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 the message driven bean management
    * @throws Exception if error occurs
    */
    public void testMessageDrivenBean() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "alarm";
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add alarm.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to alarm.jar page
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

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

            table = utils.getTable(wr, 0);

            // Get links
            int nbLink = table.getRowCount() -1;
            WebLink[] links = new WebLink[nbLink];
            WebResponse[] responses = new WebResponse[nbLink];
            for (int i = 0; i < nbLink; i++) {
                links[i] = table.getTableCell(i + 1, 0).getLinks()[0];
            }

            /*
             * Verify the MessageDrivenBean: AlarmListener
             */
            links[0].click();
            responses[0] = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 5;
            table = utils.getTable(responses[0], 1);
            // Name AlarmListener
            assertEquals("It is not the name of the MessageDrivenBean. ", "AlarmListener", table.getTableCell(0, 2).getText());
            // File .../alarm.jar
            assertTrue("It is not the file of the MessageDrivenBean. ", table.getTableCell(1, 2).getText().endsWith("alarm.jar"));

            // Naming and Classes
            table = utils.getTable(responses[0], 4);
            // EJB class org.objectweb.alarm.beans.AlarmListenerBean
            assertEquals("It is not the EJB class of the MessageDrivenBean. ", "org.objectweb.alarm.beans.AlarmListenerBean", table.getTableCell(0, 2).getText());
            // EJB JNDI Name AlarmListenerHome
            assertEquals("It is not the EJB JNDI Name of the MessageDrivenBean. ", "AlarmListenerHome", table.getTableCell(1, 2).getText());
            // EJB HomeClass
            assertEquals("It is not the EJB HomeClass of the MessageDrivenBean. ", "", table.getTableCell(2, 2).getText());
            // EJB RemoteClass
            assertEquals("It is not the EJB RemoteClass of the MessageDrivenBean. ", "", table.getTableCell(3, 2).getText());
            // EJB LocalHomeClass
            assertEquals("It is not the EJB LocalHomeClass of the MessageDrivenBean. ", "", table.getTableCell(4, 2).getText());
            // EJB LocalClass
            assertEquals("It is not the EJB LocalClass of the MessageDrivenBean. ", "", table.getTableCell(5, 2).getText());

            // Mdb Configuration
            table = utils.getTable(responses[0], 7);
            // JMS Destination name AlarmTopic
            assertEquals("It is not the JMS Destination name of the MessageDrivenBean. ", "AlarmTopic", table.getTableCell(0, 2).getText());

            // Verify tabs
            tabTable = utils.getTabTable(responses[0]);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_MDB, selectedTab, "Problem in 'AlarmListener' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            assertTrue("The link " + URL_JONASADMIN_CONTAINER + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER));
            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_VIEW_EJB + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB));
            assertTrue("The link " + URL_JONASADMIN_DEPENDENCIES + " is not found in the sixth tab. ",
                    tabTable.getTableCell(0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_DEPENDENCIES));

            // Go to Dependency
            link = responses[0].getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_DEPENDENCIES);
            link.click();
            responses[0] = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 6;

            // JMS Destination
            table = utils.getTable(responses[0], 0);
            assertEquals("The title of table is not 'JMS Destinastion'. ", "JMS Destination", table.getTableCell(0, 0).getText());
            // Topic AlarmTopic
            table = utils.getTable(responses[0], 1);
            assertEquals("The JMS destination is not 'AlarmTopic'. ", "AlarmTopic", table.getTableCell(0, 0).getText());


            // Verify tabs
            tabTable = utils.getTabTable(responses[0]);
            testTabs(tabTable, NUMBER_OF_TABS_FOR_MDB, selectedTab, "Problem in 'Dependency' tab.");
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            assertTrue("The link " + URL_JONASADMIN_CONTAINER + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER));
            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_VIEW_EJB + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB));
            assertTrue("The link " + URL_JONASADMIN_MDB + " is not found in the sixth tab. ",
                    tabTable.getTableCell(0, 8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_MDB));
            }
    }


    /**
    * Test the entity bean management
    * @throws Exception if error occurs
    */
    public void testEntityBean() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "alarm";
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add alarm.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to alarm.jar page
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

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

            table = utils.getTable(wr, 0);

            // Get links
            int nbLink = table.getRowCount() -1;
            WebLink[] links = new WebLink[nbLink];
            WebResponse[] responses = new WebResponse[nbLink];
            for (int i = 0; i < nbLink; i++) {
                links[i] = table.getTableCell(i + 1, 0).getLinks()[0];
            }

            /*
             * Verify the EntityBean: AlarmRecord
             */
            links[1].click();
            responses[1] = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 5;
            // EntityBean
            table = utils.getTable(responses[1], 3);
            assertEquals("The title of table is not 'EntityBean'. ", "EntityBean", table.getTableCell(0, 0).getText());

            table = utils.getTable(responses[1], 4);
            // Name                 AlarmRecord
            assertEquals("It is not the name of the EntityBean. ", "AlarmRecord", table.getTableCell(0, 2).getText());
            // File                 .../alarm.jar
            assertTrue("It is not the file of the EntityBean. ", table.getTableCell(1, 2).getText().endsWith("alarm.jar"));

            // Naming and Classes
            table = utils.getTable(responses[1], 7);
            // EJB class            org.objectweb.alarm.beans.AlarmRecordBean
            assertEquals("It is not the EJB class of the EntityBean. ", "org.objectweb.alarm.beans.AlarmRecordBean", table.getTableCell(0, 2).getText());
            // EJB JNDI Name        alarmrecord
            assertEquals("It is not the EJB JNDI Name of the EntityBean. ", "alarmrecord", table.getTableCell(1, 2).getText());
            // EJB HomeClass        org.objectweb.alarm.beans.AlarmRecordHome
            assertEquals("It is not the EJB HomeClass of the EntityBean. ", "org.objectweb.alarm.beans.AlarmRecordHome", table.getTableCell(2, 2).getText());
            // EJB RemoteClass      org.objectweb.alarm.beans.AlarmRecord
            assertEquals("It is not the EJB RemoteClass of the EntityBean. ", "org.objectweb.alarm.beans.AlarmRecord", table.getTableCell(3, 2).getText());
            // EJB LocalHomeClass
            assertEquals("It is not the EJB LocalHomeClass of the EntityBean. ", "", table.getTableCell(4, 2).getText());
            // EJB LocalClass
            assertEquals("It is not the EJB LocalClass of the EntityBean. ", "", table.getTableCell(5, 2).getText());

            // Entity Configuration
            table = utils.getTable(responses[1], 10);
            // Persistency              Container-Managed   Type of Persistency (CMP/BMP)
            assertEquals("It is not the persistency of the EntityBean. ", "Container-Managed", table.getTableCell(0, 2).getText());
            // Passivation Timeout      0   Value in seconds
            assertEquals("It is not the passivation timeout of the EntityBean. ", "0", table.getTableCell(1, 2).getText());
            // Inactivity Timeout       0   Maximum time of inactivity (in sec.) before freeing objects
            assertEquals("It is not the inactivity timeout of the EntityBean. ", "0", table.getTableCell(2, 2).getText());
            // Lock Policy              container-serialized    one among CS/CRC/CRU/DB
            assertEquals("It is not the lock policy of the EntityBean. ", "container-serialized", table.getTableCell(3, 2).getText());
            // Shared                   false   True if bean accessed outside this ejb server.
            assertEquals("It is not the shared value of the EntityBean. ", "false", table.getTableCell(4, 2).getText());
            // Prefetch                 false   True if prefetch optimization wanted.
            assertEquals("It is not the prefetch value of the EntityBean. ", "false", table.getTableCell(5, 2).getText());
            // HardLimit                false   True if hard limit optimization wanted.
            assertEquals("It is not the hard limit value of the EntityBean. ", "false", table.getTableCell(6, 2).getText());
            // Initial Pool size        0   Nb of instances preallocated
            assertEquals("It is not the initial pool size of the EntityBean. ", "0", table.getTableCell(7, 2).getText());
            // Max number of instances  20  Max nb of instances in memory
            assertEquals("It is not the max number of instances of the EntityBean. ", "20", table.getTableCell(8, 2).getText());

            // Memory Management
            int currentPoolSize;
            int usedInTX;
            int usedOutsideTX;
            int unusedButReady;
            int markedRemoved;
            int currentNbInstances;
            int pkNumber;
            int passivated;
            int total;
            // Create instances
            createInstances();
            // Synchronize
            link = responses[1].getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SYNCHRONIZE);
            link.click();
            responses[1] = wc.getFrameContents(FRAME_CONTENT);
            table = utils.getTable(responses[1], 13);
            currentPoolSize = Integer.parseInt(table.getTableCell(0, 2).getText());
            usedInTX = Integer.parseInt(table.getTableCell(1, 2).getText());
            usedOutsideTX = Integer.parseInt(table.getTableCell(2, 2).getText());
            unusedButReady = Integer.parseInt(table.getTableCell(3, 2).getText());
            markedRemoved = Integer.parseInt(table.getTableCell(4, 2).getText());
            currentNbInstances = Integer.parseInt(table.getTableCell(5, 2).getText());
            pkNumber = Integer.parseInt(table.getTableCell(6, 2).getText());
            passivated = Integer.parseInt(table.getTableCell(7, 2).getText());
            total = currentPoolSize + usedInTX + usedOutsideTX + unusedButReady + markedRemoved;
            // Current number of instances
            assertEquals("The current number of instances doesn't correspond to the sum of instances. ", total, currentNbInstances);
            assertEquals("It is not the current number of instances. ", getAlarmRecordCurrentNbInstances(), currentNbInstances);
            assertEquals("The number of passivated instances is not '" + getAlarmRecordPassivatedInstances() + "' . ", getAlarmRecordPassivatedInstances(), passivated);
            assertEquals("The number of PK is not '" + getAlarmRecordPkNumber() + "' . ", getAlarmRecordPkNumber(), pkNumber);
            // Reduce
            link = responses[1].getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_REDUCE);
            link.click();
            // Here we should verify that Reduce instances worked.
            // This test is done currently in testEntityBeanReduceInstances method.

            // Verify tabs
            tabTable = utils.getTabTable(responses[1]);
            if(isDatabaseService()) {
            	testTabs(tabTable, NUMBER_OF_TABS_FOR_ENTITY_DBM, selectedTab, "Problem in 'AlarmRecord' tab.");
                assertTrue("The link " + URL_JONASADMIN_DEPENDENCIES + " is not found in the sixth tab. ",
                        tabTable.getTableCell(0, 10).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_DEPENDENCIES));
            } else {
                testTabs(tabTable, NUMBER_OF_TABS_FOR_ENTITY_RAR, selectedTab, "Problem in 'AlarmRecord' tab.");
            }
            // - links
            assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                    tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
            assertTrue("The link " + URL_JONASADMIN_CONTAINER + " is not found in the second tab. ",
                    tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER));
            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_VIEW_EJB + " is not found in the fourth tab. ",
                    tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB));

            if(isDatabaseService()) {
            	// dbm started, check for dependency
            	link = responses[1].getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_DEPENDENCIES);
                link.click();
                responses[1] = wc.getFrameContents(FRAME_CONTENT);
                selectedTab = 6;

                // Datasource
                table = utils.getTable(responses[1], 0);
                assertEquals("The title of table is not 'Datasource'. ", "Datasource", table.getTableCell(0, 0).getText());
                // jdbc_1       postgres
                table = utils.getTable(responses[1], 1);
                assertEquals("It is not 'jdbc_1'. ", "jdbc_1", table.getTableCell(0, 0).getText());
                // Here check getDataSourceName() maybe no datasource deployed ...
                String datasourceName = getDataSourceName();
                if (datasourceName != null) {
                	assertEquals("The datasource is not '" + getDataSourceName() + "'. ", getDataSourceName(), table.getTableCell(0, 2).getText());
                	link = table.getTableCell(0, 2).getLinks()[0];
                	assertTrue("It is not the correct URL. ", link.getURLString().endsWith("EditDatasource.do?name=" + datasourceName));
                	assertEquals("The link is not correct. ", 200, wc.sendRequest(link.getRequest()).getResponseCode());
                    // Verify tabs
                    tabTable = utils.getTabTable(responses[1]);
                    testTabs(tabTable, NUMBER_OF_TABS_FOR_MDB, selectedTab, "Problem in 'Dependency' tab.");
                    // - links
                    assertTrue("The link " + URL_JONASADMIN_EJB_CONTAINER + " is not found in the first tab. ",
                            tabTable.getTableCell(0, 0).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EJB_CONTAINER));
                    assertTrue("The link " + URL_JONASADMIN_CONTAINER + " is not found in the second tab. ",
                            tabTable.getTableCell(0, 2).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_CONTAINER));
                    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_VIEW_EJB + " is not found in the fourth tab. ",
                            tabTable.getTableCell(0, 6).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_VIEW_EJB));
                    assertTrue("The link " + URL_JONASADMIN_EB + " is not found in the fifth tab. ",
                            tabTable.getTableCell(0, 8).getLinks()[0].getURLString().endsWith(URL_JONASADMIN_EB));
                }
            }
        }
    }

    /**
     * Test the entity bean management - reduce instances function
     * @throws Exception if error occurs
     */
     public void testEntityBeanReduceInstances() throws Exception {

         WebResponse wr;
         WebLink link;
         WebTable table;
         WebTable tabTable;
         TableCell selectedCell;
         TableCell cell;
         String ear = "alarm";
         int selectedTab;
         JonasAdminUtils utils = new JonasAdminUtils();

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

         if (jProp.isEar() && jProp.isEjb()) {
             // Add alarm.ear
             useEar(ear);

             // Go to ejb container
             wr = wc.getFrameContents(FRAME_TREE);
             link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
             link.click();
             wr = wc.getFrameContents(FRAME_CONTENT);

             // Go to alarm.jar page
             link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
             link.click();
             wr = wc.getFrameContents(FRAME_CONTENT);
             selectedTab = 2;

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

             table = utils.getTable(wr, 0);

             // Get links
             int nbLink = table.getRowCount() -1;
             WebLink[] links = new WebLink[nbLink];
             WebResponse[] responses = new WebResponse[nbLink];
             for (int i = 0; i < nbLink; i++) {
                 links[i] = table.getTableCell(i + 1, 0).getLinks()[0];
             }

             // Verify the EntityBean: AlarmRecord
             links[1].click();
             responses[1] = wc.getFrameContents(FRAME_CONTENT);

             selectedTab = 5;
             // EntityBean
             table = utils.getTable(responses[1], 3);
             assertEquals("The title of table is not 'EntityBean'. ", "EntityBean", table.getTableCell(0, 0).getText());

             table = utils.getTable(responses[1], 4);
             // Name                 AlarmRecord
             assertEquals("It is not the name of the EntityBean. ", "AlarmRecord", table.getTableCell(0, 2).getText());
             // File                 .../alarm.jar
             assertTrue("It is not the file of the EntityBean. ", table.getTableCell(1, 2).getText().endsWith("alarm.jar"));

             // Memory Management
             int unusedButReady;
             int currentNbInstances;
             int passivated;
             // Create instances
             createInstances();
             // Synchronize
             link = responses[1].getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SYNCHRONIZE);
             link.click();
             responses[1] = wc.getFrameContents(FRAME_CONTENT);
             table = utils.getTable(responses[1], 13);
             unusedButReady = Integer.parseInt(table.getTableCell(3, 2).getText());
             currentNbInstances = Integer.parseInt(table.getTableCell(5, 2).getText());
             passivated = Integer.parseInt(table.getTableCell(7, 2).getText());
             if (unusedButReady == 0) {
            	 // there is nothing to reduce
            	 return;
             }
             // Reduce
             link = responses[1].getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_REDUCE);
             link.click();
             responses[1] = wc.getFrameContents(FRAME_CONTENT);
             table = utils.getTable(responses[1], 13);
             // Current number of instances is less big than previous number of instances
             int previousNbInstances = currentNbInstances;
             assertTrue("The number of instances doesn't decrease when reduce button is clicked. ", previousNbInstances > currentNbInstances);
             currentNbInstances = Integer.parseInt(table.getTableCell(5, 2).getText());
             passivated = Integer.parseInt(table.getTableCell(7, 2).getText());
             assertTrue("The number of passivated instances is not positif. ", passivated > 0);
         }
     }

    /**
    * Test the stateful session bean management
    * @throws Exception if error occurs
    */
    public void testStatefulSessionBean() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "alarm";
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add alarm.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to alarm.jar page
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

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

            table = utils.getTable(wr, 0);

            // Get links
            int nbLink = table.getRowCount() -1;
            WebLink[] links = new WebLink[nbLink];
            WebResponse[] responses = new WebResponse[nbLink];
            for (int i = 0; i < nbLink; i++) {
                links[i] = table.getTableCell(i + 1, 0).getLinks()[0];
            }

            /*
             * Verify the StatefulSessionBean: View
             */
            links[2].click();
            responses[2] = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 5;
            // StatefulSessionBean
            table = utils.getTable(responses[2], 0);
            assertEquals("The title of table is not 'StatefulSessionBean'. ", "StatefulSessionBean", table.getTableCell(0, 0).getText());

            table = utils.getTable(responses[2], 1);
            // Name                 AlarmListener
            assertEquals("It is not the name of the StatefulSessionBean. ", "View", table.getTableCell(0, 2).getText());
            // File                 .../alarm.jar
            assertTrue("It is not the file of the StatefulSessionBean. ", table.getTableCell(1, 2).getText().endsWith("alarm.jar"));

            // Naming and Classes
            table = utils.getTable(responses[2], 4);
            // EJB class            org.objectweb.alarm.beans.ViewBean
            assertEquals("It is not the EJB class of the StatefulSessionBean. ", "org.objectweb.alarm.beans.ViewBean", table.getTableCell(0, 2).getText());
            // EJB JNDI Name        viewhome
            assertEquals("It is not the EJB JNDI Name of the StatefulSessionBean. ", "viewhome", table.getTableCell(1, 2).getText());
            // EJB HomeClass        org.objectweb.alarm.beans.ViewHome
            assertEquals("It is not the EJB HomeClass of the StatefulSessionBean. ", "org.objectweb.alarm.beans.ViewHome", table.getTableCell(2, 2).getText());
            // EJB RemoteClass      org.objectweb.alarm.beans.View
            assertEquals("It is not the EJB RemoteClass of the StatefulSessionBean. ", "org.objectweb.alarm.beans.View", table.getTableCell(3, 2).getText());
            // EJB LocalHomeClass
            assertEquals("It is not the EJB LocalHomeClass of the StatefulSessionBean. ", "", table.getTableCell(4, 2).getText());
            // EJB LocalClass
            assertEquals("It is not the EJB LocalClass of the StatefulSessionBean. ", "", table.getTableCell(5, 2).getText());

            // Session Configuration
            WebForm form = responses[2].getForms()[0];
            // sessionTimeOut = 0
            form.setParameter("sessionTimeOut", "0");
            responses[2] = form.submit();
            form = responses[2].getForms()[0];
            assertTrue("The value of sessionTimeOut is not 'no limit'/'pas de limite'. ", form.getParameterValue("sessionTimeOut").indexOf("limit") != -1);
            // sessionTimeOut = 10
            form.setParameter("sessionTimeOut", "10");
            responses[2] = form.submit();
            form = responses[2].getForms()[0];
            assertEquals("The value of sessionTimeOut is not '10'. ", "10", form.getParameterValue("sessionTimeOut"));
            // empty
            form.setParameter("sessionTimeOut", "");
            responses[2] = form.submit();
            form = responses[2].getForms()[0];
            table = utils.getTable(responses[2], 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertEquals("The message is not 'Session timeout cannot be empty'. ", "Session timeout cannot be empty", cell.getText());
            // not a valid integer
            form.setParameter("sessionTimeOut", "10.5");
            responses[2] = form.submit();
            form = responses[2].getForms()[0];
            table = utils.getTable(responses[2], 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertEquals("The message is not 'Session timeout not a valid integer !'. ", "Session timeout not a valid integer !", cell.getText());
            // negative value
            form.setParameter("sessionTimeOut", "-1");
            responses[2] = form.submit();
            table = utils.getTable(responses[2], 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertFalse("An error was found but there is no text for a negative value for 'Current session TimeOut'. ", cell.getText().equals(""));
        }
    }


    /**
    * Test the stateless session bean management
    * @throws Exception if error occurs
    */
    public void testStatelessSessionBean() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "olstore";
        int selectedTab;
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add olstore.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Go to olstoreejb.jar page
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, ear);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 2;

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

            table = utils.getTable(wr, 0);

            // Get a link to a StatelessSessionBean
            int rowLastStatelessSB = utils.getRow("StatelessSessionBean", table, 2).intValue();
            if (rowLastStatelessSB == -1) {
                fail("No Stateless Session Bean found for the ear '" + ear + "'. ");
            }
            String sbName = table.getTableCell(rowLastStatelessSB, 0).getText();
            link = table.getTableCell(rowLastStatelessSB, 0).getLinks()[0];
            if (link == null) {
                fail("No link found for the StatelessSessionBean '" + sbName + "'. ");
            }

            /*
             * verify the StatelessSessionBean
             */
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            selectedTab = 5;
            // StatelessSessionBean
            table = utils.getTable(wr, 0);
            assertEquals("The title of table is not 'StatelessSessionBean'. ", "StatelessSessionBean", table.getTableCell(0, 0).getText());

            table = utils.getTable(wr, 1);
            // Name                 UserHelper
            assertEquals("It is not the name of the StatelessSessionBean. ", sbName, table.getTableCell(0, 2).getText());
            // File                 .../olstoreejb.jar
            assertTrue("It is not the file of the StatelessSessionBean. ", table.getTableCell(1, 2).getText().endsWith("olstoreejb.jar"));

            // Naming and Classes
            String ejbClass = getStatelessSBClassName(sbName, "ejbClass");
            if (ejbClass == null) {
                ejbClass = "";
            }
            String jndiName = getStatelessSBClassName(sbName, "jndiName");
            if (jndiName == null) {
                jndiName = "";
            }
            String homeClass = getStatelessSBClassName(sbName, "homeClass");
            if (homeClass == null) {
                homeClass = "";
            }
            String remoteClass = getStatelessSBClassName(sbName, "remoteClass");
            if (remoteClass == null) {
                remoteClass = "";
            }
            String localHomeClass = getStatelessSBClassName(sbName, "localHomeClass");
            if (localHomeClass == null) {
                localHomeClass = "";
            }
            String localClass = getStatelessSBClassName(sbName, "localClass");
            if (localClass == null) {
                localClass = "";
            }
            table = utils.getTable(wr, 4);
            // EJB class            olstore.session.helper.UserHelperSession
            assertEquals("It is not the EJB class of the StatelessSessionBean. ", ejbClass, table.getTableCell(0, 2).getText());
            // EJB JNDI Name        UserHelperLocal
            assertEquals("It is not the EJB JNDI Name of the StatelessSessionBean. ", jndiName, table.getTableCell(1, 2).getText());
            // EJB HomeClass
            assertEquals("It is not the EJB HomeClass of the StatelessSessionBean. ", homeClass, table.getTableCell(2, 2).getText());
            // EJB RemoteClass
            assertEquals("It is not the EJB RemoteClass of the StatelessSessionBean. ", remoteClass, table.getTableCell(3, 2).getText());
            // EJB LocalHomeClass   olstore.session.helper.UserHelperLocalHome
            assertEquals("It is not the EJB LocalHomeClass of the StatelessSessionBean. ", localHomeClass, table.getTableCell(4, 2).getText());
            // EJB LocalClass       olstore.session.helper.UserHelperLocal
            assertEquals("It is not the EJB LocalClass of the StatelessSessionBean. ", localClass, table.getTableCell(5, 2).getText());

            // Instances
            int poolSize = getStatelessSBInstance(sbName, "poolSize");
            int cacheSize = getStatelessSBInstance(sbName, "cacheSize");
            table = utils.getTable(wr, 7);
            //
            assertEquals("It is not the correct Pool size of the StatelessSessionBean. ", "" + poolSize, table.getTableCell(0, 2).getText());
            assertEquals("It is not the correct Cache size of the StatelessSessionBean. ", "" + cacheSize, table.getTableCell(1, 2).getText());

            // Session Configuration
            int minPoolSize = getStatelessSBInstance(sbName, "minPoolSize");
            int maxCacheSize = getStatelessSBInstance(sbName, "maxCacheSize");
            table = utils.getTable(wr, 10);
            assertEquals("It is not the correct Min Pool size of the StatelessSessionBean. ", "" + minPoolSize, table.getTableCell(0, 2).getText());
            assertEquals("It is not the correct Max Cache size of the StatelessSessionBean. ", "" + maxCacheSize, table.getTableCell(1, 2).getText());

            WebForm form = wr.getForms()[0];
            // sessionTimeOut = 0
            form.setParameter("sessionTimeOut", "0");
            wr = form.submit();
            form = wr.getForms()[0];
            assertTrue("The value of sessionTimeOut is not 'no limit'/'pas de limite'. ", form.getParameterValue("sessionTimeOut").indexOf("limit") != -1);
            // sessionTimeOut = 10
            form.setParameter("sessionTimeOut", "10");
            wr = form.submit();
            form = wr.getForms()[0];
            assertEquals("The value of sessionTimeOut is not '10'. ", "10", form.getParameterValue("sessionTimeOut"));
            // empty
            form.setParameter("sessionTimeOut", "");
            wr = form.submit();
            form = wr.getForms()[0];
            table = utils.getTable(wr, 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertEquals("The message is not 'Session timeout cannot be empty'. ", "Session timeout cannot be empty", cell.getText());
            // not a valid integer
            form.setParameter("sessionTimeOut", "10.5");
            wr = form.submit();
            form = wr.getForms()[0];
            table = utils.getTable(wr, 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertEquals("The message is not 'Session timeout not a valid integer !'. ", "Session timeout not a valid integer !", cell.getText());
            // negative value
            form.setParameter("sessionTimeOut", "-1");
            wr = form.submit();
            table = utils.getTable(wr, 0);
            cell = table.getTableCell(0, 0);
            assertEquals("There is not an error. ", "errors", cell.getAttribute("class"));
            assertFalse("An error was found but there is no text for a negative value for 'Current session TimeOut'. ", cell.getText().equals(""));
        }


    }

    /**
     * Test statistic
     * @throws Exception if error occurs
     *
     */
    public void testStatistics() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;
        WebTable tabTable;
        TableCell selectedCell;
        TableCell cell;
        String ear = "alarm";
        JonasAdminUtils utils = new JonasAdminUtils();

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

        if (jProp.isEar() && jProp.isEjb()) {
            // Add alarm.ear
            useEar(ear);

            // Go to ejb container
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_EJB_CONTAINER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Get jar Links
            table = utils.getTable(wr, 0);
            int nbLink = table.getRowCount() -1;
            WebLink[] links = new WebLink[nbLink];
            WebResponse[] responses = new WebResponse[nbLink];
            WebTable tableStat;
            int[] jarBMP = new int[nbLink];
            int[] jarCMP = new int[nbLink];
            int[] jarStateful = new int[nbLink];
            int[] jarStateless = new int[nbLink];
            int[] jarMDB = new int[nbLink];
            int[] jarTotal = new int[nbLink];

            int totalBMP = 0;
            int totalCMP = 0;
            int totalStateful = 0;
            int totalStateless = 0;
            int totalMDB = 0;
            int totalTotal = 0;

            int allBMP;
            int allCMP;
            int allStateful;
            int allStateless;
            int allMDB;
            int allTotal;

            // Go to statistics
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_CONTAINERS_STATISTICS);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            tableStat = utils.getTable(wr, 1);
            allBMP = Integer.parseInt(tableStat.getTableCell(1, 2).getText());
            allCMP = Integer.parseInt(tableStat.getTableCell(2, 2).getText());
            allStateful = Integer.parseInt(tableStat.getTableCell(3, 2).getText());
            allStateless = Integer.parseInt(tableStat.getTableCell(4, 2).getText());
            allMDB = Integer.parseInt(tableStat.getTableCell(5, 2).getText());
            allTotal = Integer.parseInt(tableStat.getTableCell(0, 2).getText());
            int total = allBMP + allCMP + allStateful + allStateless + allMDB;
            assertEquals("It is not the number of EJBs in all EJB containers. ", total, allTotal);

            for (int i = 0; i < nbLink; i++) {
                links[i] = table.getTableCell(i + 1, 0).getLinks()[0];
                links[i].click();
                responses[i] = wc.getFrameContents(FRAME_CONTENT);

                // Go to the statistic tab
                link = responses[i].getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_STATISTICS);
                link.click();
                wr = wc.getFrameContents(FRAME_CONTENT);
                // Verify stats
                tableStat = utils.getTable(wr, 1);

                jarBMP[i] = Integer.parseInt(tableStat.getTableCell(1, 2).getText());
                jarCMP[i] = Integer.parseInt(tableStat.getTableCell(2, 2).getText());
                jarStateful[i] = Integer.parseInt(tableStat.getTableCell(3, 2).getText());
                jarStateless[i] = Integer.parseInt(tableStat.getTableCell(4, 2).getText());
                jarMDB[i] = Integer.parseInt(tableStat.getTableCell(5, 2).getText());
                jarTotal[i] = Integer.parseInt(tableStat.getTableCell(0, 2).getText());
                total = jarBMP[i] + jarCMP[i] + jarStateful[i] +jarStateless[i] + jarMDB[i];
                assertEquals("It is not the number of EJBs in " + links[i].getText(), total, jarTotal[i]);

                tableStat = utils.getTable(wr, 4);
                int allTmpBMP = Integer.parseInt(tableStat.getTableCell(1, 2).getText());
                int allTmpCMP = Integer.parseInt(tableStat.getTableCell(2, 2).getText());
                int allTmpStateful = Integer.parseInt(tableStat.getTableCell(3, 2).getText());
                int allTmpStateless = Integer.parseInt(tableStat.getTableCell(4, 2).getText());
                int allTmpMDB = Integer.parseInt(tableStat.getTableCell(5, 2).getText());
                int allTmpTotal = Integer.parseInt(tableStat.getTableCell(0, 2).getText());

                assertEquals("It is not the number of BMP in all EJB containers for the jar " + links[i].getText() + ". ", allBMP, allTmpBMP);
                assertEquals("It is not the number of CMP in all EJB containers for the jar " + links[i].getText() + ". ", allCMP, allTmpCMP);
                assertEquals("It is not the number of Stateful SB in all EJB containers for the jar " + links[i].getText() + ". ", allStateful, allTmpStateful);
                assertEquals("It is not the number of Stateless SB in all EJB containers for the jar " + links[i].getText() + ". ", allStateless, allTmpStateless);
                assertEquals("It is not the number of MDB in all EJB containers for the jar " + links[i].getText() + ". ", allMDB, allTmpMDB);
                assertEquals("It is not the number of all EJBs in all EJB containers for the jar " + links[i].getText() + ". ", allTotal, allTmpTotal);

                totalBMP += jarBMP[i];
                totalCMP += jarCMP[i];
                totalStateful += jarStateful[i];
                totalStateless += jarStateless[i];
                totalMDB += jarMDB[i];
                totalTotal += jarTotal[i];
            }

            assertEquals("It is not the number of all BMPs. ", totalBMP, allBMP);
            assertEquals("It is not the number of all CMPs. ", totalCMP, allCMP);
            assertEquals("It is not the number of all stateful SBs. ", totalStateful, allStateful);
            assertEquals("It is not the number of all stateless SBs. ", totalStateless, allStateless);
            assertEquals("It is not the number of all MDBs. ", totalMDB, allMDB);
            assertEquals("It is not the number of all EJBs. ", totalTotal, allTotal);
        }
    }

    /**
     * Use Alarm Demo for creating instances
     * @throws Exception if an error occurs
     */
    private void createInstances() 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);
    }

    /**
     * Get the current number of instances of Alarm Record
     * @return the current number of instances
     * @throws Exception if an error occurs
     */
    private int getAlarmRecordCurrentNbInstances() throws Exception {
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=EntityBean,name=AlarmRecord,EJBModule=alarm,J2EEApplication=alarm,J2EEServer=jonas");

        return ((Integer) server.getAttribute(on, "cacheSize")).intValue();
    }

    /**
     * Get the current number of instances of Alarm Record
     * @return the current number of instances
     * @throws Exception if an error occurs
     */
    private int getAlarmRecordPassivatedInstances() throws Exception {
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=EntityBean,name=AlarmRecord,EJBModule=alarm,J2EEApplication=alarm,J2EEServer=jonas");

        Integer[] nbInstanceList = (Integer[]) server.getAttribute(on, "entityCounters");
        return ((Integer) nbInstanceList[3]).intValue();
    }

    /**
     * Get the pk number for Alarm Record
     * @return the nmber of pk in memory
     * @throws Exception if an error occurs
     */
    private int getAlarmRecordPkNumber() throws Exception {
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=EntityBean,name=AlarmRecord,EJBModule=alarm,J2EEApplication=alarm,J2EEServer=jonas");

        Integer[] nbInstanceList = (Integer[]) server.getAttribute(on, "entityCounters");
        return ((Integer) nbInstanceList[5]).intValue();
    }

    /**
     * Get the name of the datasource
     * @return name of the datasource
     * @throws Exception if an error occurs
     */
    private String getDataSourceName() throws Exception {
    	String name = null;
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=JDBCDataSource,*");
        Iterator it = server.queryNames(on, null).iterator();
        if (it.hasNext()) {
        	ObjectName jdbcDataSourceON = (ObjectName) it.next();
        	name = (String) server.getAttribute(jdbcDataSourceON, "name");
        }
        return name;
    }

    /**
     * Get the class name of a StatelessSessionBean
     * @param sbName name of the StatelessSessionBean
     * @param attributName {"ejbClass", "jndiName", "homeClass", "remoteClass", "localHomeClass", "localClass"}
     * @return name of the class
     * @throws Exception if an error occurs
     */
    private String getStatelessSBClassName(String sbName, String attributName) throws Exception {
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=StatelessSessionBean,name=" + sbName + ",EJBModule=olstoreejb,J2EEApplication=olstore,J2EEServer=jonas");
        String name = (String) server.getAttribute(on, attributName);
        return name;
    }

    /**
     * Get number of instances of a StatelessSessionBean
     * @param sbName name of the StatelessSessionBean
     * @param attributName {"nbInstances", "minPoolSize", "maxPoolSize"}
     * @return name of the class
     * @throws Exception if an error occurs
     */
    private int getStatelessSBInstance(String sbName, String attributName) throws Exception {
        MBeanServerConnection server = getMBeanServer();
        ObjectName on = ObjectName.getInstance("jonas:j2eeType=StatelessSessionBean,name=" + sbName + ",EJBModule=olstoreejb,J2EEApplication=olstore,J2EEServer=jonas");
        Integer nb = (Integer) server.getAttribute(on, attributName);
        return nb.intValue();
    }

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

    public static int getNumberOfTabsForMdb() {
    	return NUMBER_OF_TABS_FOR_MDB;
    }

    public static int getMdbTabIndex() {
    	return MDB_TAB_INDEX;
    }

    public static String getMdbTabText() {
    	return TDB_TAB_TEXT;
    }
}
