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

import java.io.File;

import junit.framework.TestSuite;

import org.objectweb.jonas.jonasadmin.test.catalina.ConnectorUtils;
import org.objectweb.jonas.jonasadmin.test.logging.LoggerUtils;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminFiles;
import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;

import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;


/**
 * Class for testing Save All in jonasAdmin
 * @author Paul Kemler
 *
 */
public class F_JonasAdminSaveAll extends JonasAdminTestCase {

    /**
     * URL of the Server Management
     */
    private static final String URL_JONASADMIN_SERVER = "EditJonasServer.do";

    /**
     * URL of the SERVLET SERVER
     */
    private static final String URL_JONASADMIN_SERVLET_SERVER = "EditServletServer.do";

    /**
     * URL of the SAVE ALL button
     */
    private static final String URL_JONASADMIN_SAVE_ALL = "SaveCatalina.do";

    /**
     * URL of welcome
     */
    private static final String URL_WELCOME = "/jonasAdmin/Welcome.do";

    /**
     * Time of beginning testAllLinks
     */
    private String beginTime;

    /**
     * Time of ending testAllLinks
     */
    private String endTime;

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

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

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

        // Get time
        beginTime = getTime();

        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 Servlet server 'Save all'
     * @throws Exception if error occurs
     *
     */
    public void testSaveAll() throws Exception {

        WebResponse wr;
        WebLink link;
        WebTable table;

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

        if (jProp.isCatalina()) {
            // Go to jonas server management
            wr = wc.getFrameContents(FRAME_TREE);
            link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);
            // Go to servlet server
            link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVLET_SERVER);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Create new connector
            String port = ConnectorUtils.createNewConnector(wc, "http");
            // Create new logger
            LoggerUtils.createNewEngineLogger(wc);

            // Save all
            link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SAVE_ALL);
            link.click();
            wr = wc.getFrameContents(FRAME_CONTENT);

            // Remove created connector
            ConnectorUtils.deleteConnector(wc, port);
            // Remove logger
            LoggerUtils.deleteLogger(wc, null);

            // Get server.xml and test file.
            XMLTest xmlTest = new XMLTest("");
            File serverXml = JonasAdminFiles.getServerXmlFile();
            xmlTest.testServerXml(serverXml, port);

        }
    }

    /**
     * Tear Down
     * cleanUp action
     */
    public void tearDown() throws Exception {
        super.tearDown();
        // Get time
        endTime = getTime();

        // Recover last server.xml configuration
        JonasAdminFiles.recoverServerConf(beginTime, endTime);
    }
}
