/*
 * 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.TransactedCA;
import org.objectweb.jonas.jtests.beans.jca15.TransactedCAHome;
import org.objectweb.jonas.jtests.util.JTestCase;
import org.objectweb.jonas.jtests.clients.jca15.F_securityTest;

/**
 * Tests Jonas Connector Architecture Local Transaction.
 * F_transactionTests tests Jonas CA Local Transaction
 */

public class F_transactionTest extends JTestCase {

    static Context ctx = null;

    // Lookup bean home

    protected static String BEAN_HOME = "TransactedCAHome";
    protected static TransactedCAHome home = null;
    private static final String RAR_JNDI_NAME = "eis/ErsatzLoTransaction";
    final public int CLOSE_HANDLE = 0;
    final public int CLOSE_PHYSICAL = 1;
    public int CloseType = 0;
    static TransactedCA bean = null;
    public String UseBeans="jca15";
    
    public F_transactionTest(String name) {
        super(name);
    }

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

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

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

	    } catch (Exception e) {
	        fail("Cannot do setUp: " +e);
        }
    }
    private void getBean() throws Exception {
        // Connecting to TransactedCAHome thru JNDI

        if (home == null) {
	        home = (TransactedCAHome)PortableRemoteObject.narrow(
							    ctx.lookup(BEAN_HOME),
							    TransactedCAHome.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 ****************************************************

    /**
    Test Local Transaction with component-managed demarcation.
    
    The application component uses a transaction demarcation API specific 
    to an EIS. The resource adapter needs to notify the application server of 
    the events (transaction begin, commit, and rollback) related to the local 
    transaction. An application server uses this part of the contract.

     */
    public void testTransaction1() {
        CloseType=CLOSE_PHYSICAL;
        startUp("testTransaction1");
        try {
            bean.beginLoTransaction();
            assertTrue (6 == 6);
            bean.closeUp(CloseType);
            bean.commitLoTransaction();
            assertTrue (6 == 6);
        } catch (Exception e) {
            assertTrue(6==7);
        }
    }

    public void testTransaction2() {
        CloseType=CLOSE_PHYSICAL;
        startUp("testTransaction2");
        try {
            bean.closeUp(CloseType);
            bean.commitLoTransaction();
            assertTrue (6 == 7);
        } catch (Exception e) {
            assertTrue(6==6); // should get here
        }
    }

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