/*
 * 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.connectorCA;
import org.objectweb.jonas.jtests.beans.j2eeca.connectorCAHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * Tests JOnAS Connector Architecture setter methods
 */

public class F_connectorTest extends JTestCase {

    private static UserTransaction utx = null;

    static Context ctx = null;

    // Lookup bean home

    protected static String BEAN_HOME = "connectorCAHome";
    static connectorCAHome home = null;
    static connectorCA bean = null;
    static connectorCA bean2 = null;
    private static final String RAR_JNDI_NAME = "FictionalEIS";
    final private int CLOSE_HANDLE = 0;
    final private int CLOSE_PHYSICAL = 1;
    int CloseType = 0;
    
    public F_connectorTest(String name) {
        super(name);
    }

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

    // Get InitialContext

        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("j2eeca", false);  // does jonas admin -j connector.jar
            }

            // Connecting to connectorCAHome thru JNDI

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

        } catch (Exception e) {
            fail("Cannot lookup UserTransaction in setUp: " +e);
        }
    }
    /**
     * 
     * Common tearDown method, used for every test.
     * Verify some Application Server tests by reviewing the log file.
     * 
     * The application server also calls ManagedConnection.destroy when it 
     * receives a connection error event notification that signals a fatal error 
     * on the physical connection. When method closeUp(int x) is called by
     * tearDown with x>0, a connection error event notification is sent to
     * the application server. Look for <code>ManagedConnection.destroy</code> entry.
     *
     */
    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 ****************************************************
    /**
     *
     * The application server calls setter methods on the ManagedConnectionFactory 
     * instance to set various configuration properties on this instance.     
     *
     */
    public void testSetterMethods() throws Exception {
        // the jonas-ra.xml file contains these properties. The Application Server
        // calls setter methods on the ManagedConnectionFactory instance
        // ServerName = 111.222.333.444     PortNumber= ak41
        // protocol   = LINE                DUserName = defaultUserName
        // DPassword  = defaultPassword
        CloseType=CLOSE_PHYSICAL;
        startUp("testSetterMethods");
        String cp = bean.getServerName();
        assertEquals("111.222.333.444", cp);
        cp = bean.getProtocolProperty();
        assertEquals("LINE", cp);
    }
    /**
     * An application server is required to provide an implementation of the
     * javax.resource.spi.ConnectionManager interface.
     *
     */
    public void testCreatedCMInstance() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        startUp("testCreatedCMInstance");
        assertTrue(bean.getCMInstance());
    }
    /**
     *  Verify connection is useful by accessing ManagedConnectionMetaData instance
     */
    public void testConnectionProduct() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        startUp("testConnectionProduct");
        String p = bean.getConnectionProduct();
        assertEquals("Fictional EIS", p);
    }
    /**
     *
     * An application server is required to implement the javax.resource.spi.-
     * ConnectionEventListener interface and to register ConnectionEventListener 
     * with resource adapter to get connection-related event notifications.
     *
     * Count (at least) one listener
     */
    public void testRegisteredListener() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        startUp("testRegisteredListener");
        int b = bean.cntListeners();
        assertTrue(b>0);
    }
    /**
     *  Verify connection was made
     */
    public void testConnectionBasics() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        startUp("testConnectionBasics");
        assertTrue(bean.isConnectionSpec());
        assertTrue(bean.isConnection());
        assertTrue(bean.isInteraction());
    }
   /**
    * Basic error logging and tracing.
    *
    * 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 JtestResourceAdapter.rar
    *  is used. This test looks up "FictionalEIS" in the JNDI name space.
    *  The F_connectorTest is 
    *  deployed with log-enabled set to true.
    * <p>
    * <pre>
    *     &lt;log-enabled&gt;true&lt;/log-enabled&gt;
    *     &lt;log-topic&gt;org.objectweb.jtests.resourceAdapter&lt;/log-topic&gt;
    *    </pre>
    */
    public void testPrintWriter() throws Exception {
        CloseType=CLOSE_PHYSICAL;
        int isNull = 0;
        int isNotNull = 1;
        int isError = 2;
        startUp("testPrintWriter");
        int x = bean.getMCF_Pwriter();
        assertEquals(isNotNull, x);
        x = bean.getMC_Pwriter();
        assertEquals(isNotNull, x);
    }
// end test list ****************************************************

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