/*
 * 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.WebForm;
import com.meterware.httpunit.WebImage;
import com.meterware.httpunit.WebResponse;


/**
 * Test the modification of the number of measures
 * @author kemlerp
 *
 */
public class F_JonasAdminNumberOfMeasures extends JonasAdminTestCase {

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

    /**
     * CONSTANT which is used to calculate the width of the Memory image
     */
    private static final int CONSTANT = 190;

    /**
     *
     */
    private static final String INITIAL_NB_MEASURES = "120";

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

    /**
     * Get a new TestSuite for this class
     * @return a new TestSuite for this class
     */
    public static TestSuite suite() {
        return new TestSuite(F_JonasAdminNumberOfMeasures.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);
                }
            }
        }
    }

    /**
     * Setup need for these tests
     * jonasAdmin is required
     * @throws Exception if it fails
     */
    public void tearDown() throws Exception {

        super.tearDown();

        WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
        WebForm[] webForms = wr.getForms();
        WebForm webForm = webForms[0];

        webForm.setParameter("numberOfMeasures", INITIAL_NB_MEASURES);
        wr = webForm.submit();
    }

    /**
     * Test image width with a good value
     * @throws Exception if error occurs
     */
    public void testValidNbMeasures() throws Exception {

        String nbMeasures = "300";
        WebResponse wr;

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

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);

        // Verify image width
        WebImage imageMemory = wr.getImageWithName("memory");
        assertEquals("the width of the Memory image is wrong. ", "" + calculateWidth(nbMeasures), imageMemory.getAttribute("width"));

    }

    /**
     * Test image width with a number of measures is not an integer
     * @throws Exception if error occurs
     */
    public void testNotIntegerNbMeasures() throws Exception {

        String nbMeasures = "100.2";
        WebResponse wr;

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

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);

        // Verify no image
        assertNull("there is the Memory image. ", wr.getImageWithName("memory"));

        // Verify error message
        assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
    }

    /**
     * Test image width with a number of measures is 1
     * @throws Exception if error occurs
     */
    public void testNbMeasuresLessThanOne() throws Exception {

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


        String nbMeasures = "1";
        WebResponse wr;

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);
        // Verify no image
        assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
        // Verify error message
        assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);


        nbMeasures = "0";

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);
        // Verify no image
        assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
        // Verify error message
        assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);


        nbMeasures = "-1";

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);
        // Verify no image
        assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
        // Verify error message
        assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);


        nbMeasures = "-99999";

        // Modify number of measures
        wr = modifyNbMeasures(nbMeasures);
        // Verify no image
        assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
        // Verify error message
        assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
    }

    /**
     * Calculate the width of Memory image
     * @param nbMeasures number of measures
     * @return width
     */
    public int calculateWidth(String nbMeasures) {
        return Integer.parseInt(nbMeasures) * 2 + CONSTANT;
    }

    /**
     * Modify number of measures
     * @param nbMeasures number of measures
     * @return response
     * @throws Exception if error occurs
     */
    public WebResponse modifyNbMeasures(String nbMeasures) throws Exception {

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

        webForm.setParameter("numberOfMeasures", nbMeasures);
        return wr = webForm.submit();
    }
}
