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

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.WebLink;
import com.meterware.httpunit.WebResponse;

/**
 * Class to test jonasAdmin log out
 * @author kemlerp
 *
 */
public class F_JonasAdminLogout  extends JonasAdminTestCase {

    /**
     * URL of log out
     */
    private static final String URL_JONASADMIN_LOGOUT = "logOut.do";

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

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

    /**
     *
     * @throws Exception if error occurs
     *
     */
    public void testLogout() throws Exception {

        WebResponse wr;
        WebResponse topFrame;
        WebLink link;

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

        // Get the frame "top"
        topFrame = wc.getFrameContents(FRAME_TOP);

        link = topFrame.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_LOGOUT);
        wr = link.click();

        assertTrue("It is not the login page. ", wr.getText().indexOf("login.jsp") != -1);
        try {
            wc.getFrameContents(FRAME_TREE);
            fail("There is tree frame. ");
        } catch (Exception e){
            // It is OK
        }


        // Go to Welcome.do
        wc.getResponse(getAbsoluteUrl(URL_JONASADMIN + "/Welcome.do"));

        // login to jonas admin
        try {
            JonasAdminAuth.doValidAuth(wc, url);
        } catch (Exception e) {
            fail("authentification failed :  " + e);
        }

        try {
            assertNotNull("There is not tree frame. ", wc.getFrameContents(FRAME_TREE));
        } catch (Exception e) {
            fail("There is not tree frame. ");
        }
    }

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