/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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 any 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.jtests.clients.j2eeca;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.j2eeca.securedCA;
import org.objectweb.jonas.jtests.beans.j2eeca.securedCAHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * Tests Jonas Connector Architecture container-managed sign-on.
 */

public class F_securityTest extends JTestCase {

    private static UserTransaction utx = null;

    static Context ctx = null;

    // Lookup bean home

    protected static String BEAN_HOME = "securedCAHome";
    protected static securedCAHome home = null;
    private static final String RAR_JNDI_NAME = "FictionalSecurity";
    static securedCA bean = null;
    final public int CLOSE_HANDLE = 0;
    final public int CLOSE_PHYSICAL = 1;
    public int CloseType = 0;
    public String UseBeans="j2eeca";

    public F_securityTest(String name) {
        super(name);
    }

    protected void setUp() {
        super.setUp();
        try {
    	    // get JNDI initial context

            if (ctx == null) {
	            ctx = new InitialContext();
            }

	        // We want to start transactions from client: get UserTransaction

            if (utx == null) {
	            utx = (UserTransaction) ctx.lookup("javax.transaction.UserTransaction");
            }

            if (home == null) {
    	        useBeans(UseBeans, false);
            }
            getBean();
            assertTrue (5 == 5);

	    } catch (Exception e) {
	        fail("Cannot lookup UserTransaction in setUp: " +e);
        }
    }
    private void getBean() throws Exception {
        // Connecting to securedCAHome thru JNDI

        if (home == null) {
	        home = (securedCAHome)PortableRemoteObject.narrow(
							    ctx.lookup(BEAN_HOME),
							    securedCAHome.class);
            bean = home.create();
        }
    }
    protected void tearDown() throws Exception {
        bean.closeUp(CloseType);
    }
    protected void startUp(String testName) {
	    try {
	        bean.method1(RAR_JNDI_NAME, testName);
	    } catch (Exception ee) {
	        ee.printStackTrace();
	        System.exit(2);
	    }
    }

// test list ****************************************************
    /**
     *  Container managed sign-on.
     *
     *  The application server provides the required security information for 
     *  the resource principal through its configured security policies and mechanisms.
     *
     *  When this class is deployed, the <code>secured.xml</code> file contains:
     *  <p>
     *  <code>&lt;res-auth&gt;Container&lt;/res-auth&gt;</code>
     *
     */
    public void testSecurityContextContainer() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        bean.setResAuth("Container");
        startUp("testSecurityContextContainer");
        //bean.setMatchNull(true);
        String pw = bean.getSecurityPassword();
        String u = bean.getSecurityUserName();
        String realResAuth = bean.getResAuth();
        if (realResAuth.equals("Container")) {
            assertEquals("defaultPassword", pw);
            assertEquals("defaultUserName", u);
        } else {
            assertFalse(pw.equals("defaultPassword"));
        }
    }
// end test list ****************************************************

    public static Test suite() {
        return new TestSuite(F_securityTest.class);
    }

    public static void main (String args[]) {

        String testtorun = null;

        // Get args

        for (int argn = 0; argn < args.length; argn++) {

            String s_arg = args[argn];
            Integer i_arg;

            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }

        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_securityTest(testtorun));
        }
    }
}
