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

import junit.framework.TestSuite;

import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;

import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;


/**
 * Define a class to test the JOnAS Admin console
 * Test monitoring activation
 * @author kemlerp
 *
 */
public class F_JonasAdminActivatedMonitoring extends JonasAdminTestCase {

    /**
     * URL of the deployment of EARs
     */
    private static final String URL_JONASADMIN_MONITORING = "/jonasAdmin/EditMonitoring.do";


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

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_JonasAdminActivatedMonitoring.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 activation monitoring
     * @throws Exception if error occurs
     */
    public void testMonitoringActivation() throws Exception {

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

        // Go to Monitoring
        WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MONITORING));
        WebForm[] webForms = wr.getForms();
        WebForm webForm = webForms[0];

        // deactivate monitoring
        webForm.setCheckbox("activated", "on", false);

        wr = webForm.submit();
        assertEquals("there is more one tab. ", -1, wr.getText().indexOf("class=\"tab\""));

        // Go to another page
        wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN));
        wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MONITORING));
        assertEquals("there is more one tab. ", -1, wr.getText().indexOf("class=\"tab\""));


        // reactivate monitoring
        webForms = wr.getForms();
        webForm = webForms[0];
        webForm.setCheckbox("activated", "on", true);

        wr = webForm.submit();
        assertFalse("there is not at least two tabs. ", wr.getText().indexOf("class=\"tab\"") == -1);

        // verify link tab
        TableCell cell = wr.getTables()[0].getTableCell(0, 0).getTables()[0].getTableCell(0, 2);
        String attribut;
        WebLink[] links;

        attribut = cell.getAttribute("class");
        assertEquals("the tab is not in the wanted cell. ", "tab", attribut);
        links = cell.getLinks();
        assertNotNull("there is no link", links);
        assertEquals("link is not \"/jonasAdmin/EditMemory.do\"", "/jonasAdmin/EditMemory.do", links[0].getAttribute("href"));

        // Go to another page
        wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN));
        wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MONITORING));
        assertFalse("there is not at least two tabs. ", wr.getText().indexOf("class=\"tab\"") == -1);
    }

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