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

import junit.framework.*;
import java.io.BufferedReader; 
import java.io.FileInputStream;
import java.io.InputStreamReader; 
import java.io.IOException;
import java.lang.String;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.objectweb.jonas.jtests.beans.jca15.RuntimeCA;
import org.objectweb.jonas.jtests.beans.jca15.RuntimeCAHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * Tests Jonas Connector Architecture Basic error logging and tracing and component-managed sign-on.
 */

public class F_runtimeTest extends JTestCase {

    static Context ctx = null;

    // Lookup bean home

    protected static String BEAN_HOME = "RuntimeCAHome";
    protected static RuntimeCAHome home = null;
    private static final String RAR_JNDI_NAME = "eis/ErsatzNolog";
    static RuntimeCA bean = null;
    final private int CLOSE_HANDLE = 0;
    final private int CLOSE_PHYSICAL = 1;
    int CloseType = 0;

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

    protected void setUp() {
        super.setUp();

    // Get InitialContext

        try {

        // get JNDI initial context

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

            if (home == null) {
                useBeans("jca15", false);  // does "jonas admin -j jca15.jar"
            }

        // Connecting to runtimeHome thru JNDI

            if (home == null) {
                home = (RuntimeCAHome)PortableRemoteObject.narrow(
                                 ctx.lookup(BEAN_HOME),
                                 RuntimeCAHome.class);
                bean = home.create();
            }

            assertTrue (5 == 5);

        } catch (Exception e) {
            fail("Cannot do setUp: " +e);
        }
    }

    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 ****************************************************
   /**
    * Basic error logging and tracing negative test.
    *
    * An application server is required to use the following interfaces (supported 
    * by the resource adapter) to provide basic error logging and tracing for its 
    * configured set of resource adapters.
    * <pre>
    *   • ManagedConnectionFactory.set/getLogWriter
    *   • ManagedConnection.set/getLogWriter
    * </pre>
    *
    *  This test 
    *  includes in <code>jonas-ra.xml</code> file the log-enabled info as 
    *  shown below.  The ErsatzNolog.rar
    *  is used. This test looks up "ErsatzNolog" in the JNDI name space. The F_runtimeTest is 
    *  deployed with log-enabled set to false.
    * <p><pre>
    *    &lt;log-enabled>false&lt;/log-enabled&gt;
    *    &lt;log-topic&gt;&lt;/log-topic&gt;
    *   </pre>
    */
    public void testPrintWriterNull() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        int isNull = 0;
        int isNotNull = 1;
        int isError = 2;
        startUp("testPrintWriterNull");
        int x = bean.getMCF_Pwriter();
        assertEquals(isNull, x);
        x = bean.getMC_Pwriter();
        assertEquals(isNull, x);
    }

    /**
     *  Component managed sign-on.
     *
     *  The application server relies on the resource adapter to manage EIS 
     *  sign-on. Username and password are passed during connection.
     *  
     *  This test verifies the username and password were passed.
     */
    public void testSecurityContextApplication() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        bean.setResAuth("Application");
        startUp("testSecurityContextApplication");
        //bean.setMatchNull(true);
        String pw = bean.getSecurityPassword();
        String u = bean.getSecurityUserName();
        String realResAuth = bean.getResAuth();
        if (realResAuth.equals("Application")) {
            assertEquals("__Jtest_Pass_word__", pw);
            assertEquals("Ersatz_User_Name", u);
        } else {
            assertFalse(pw.equals("__Jtest_Pass_word__"));
        }
    
    }
// end test list ****************************************************

    public static Test suite() {
        return new TestSuite(F_runtimeTest.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_runtimeTest(testtorun));
        }
    }
}
