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

import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.transacted.Simple;
import org.objectweb.jonas.jtests.beans.transacted.SimpleEHome;

/**
 * TxAttribute tests on on entity bean
 */
public class F_TxAttributeEB extends A_TxAttributeEntity {

    private static String BEAN_HOME = "transactedSimpleEBHome";
    protected static SimpleEHome home = null;

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


    // --------------------------------------------------------------------
    // tests on finder methods
    // These tests are specific to BMP because there is no ejb<Find> methods
    // in CMP.
    // ---------------------------------------------------------------------

    /**
     * Test NotSupported transactional attributes on finder method
     * the finder method is called outside TX
     */
    public void testFinderNotSupported() throws Exception {

        getHome().finder_notsupported();
    }

    /**
     * Test NotSupported  transactional attributes on finder method
     * the finder method is called inside TX
     */
    public void testFinderNotSupportedTx() throws Exception {

        utx.begin();
        try {
            getHome().finder_notsupported();
        } finally {
            utx.rollback();
        }
    }


    /**
     * Test required transactional attributes on finder method
     * the finder method is called outside TX
     */
    public void testFinderRequired() throws Exception {

        getHome().finder_required();
    }

    /**
     * Test Required  transactional attributes on finder method
     * the finder method is called inside TX
     */
    public void testFinderRequiredTx() throws Exception {

        utx.begin();
        try {
            getHome().finder_required();
        } finally {
            utx.rollback();
        }
    }

    /**
     * Test supports  transactional attributes on finder method
     * the finder method is called outside TX
     * the finder method is called with false that mean that the method must not be associated 
     *                to a transaction
     */
    public void testFinderSupports() throws Exception {

        getHome().finder_supports(false);
    }


    /**
     * Test supports  transactional attributes on finder method
     * the finder method is called inside TX
     * the finder method is called with true  that mean that the method must be associated 
     *                to a transaction
     */
    public void testFinderSupportsTx() throws Exception {
        utx.begin();
        try {
            getHome().finder_supports(true);
        } finally {
            utx.rollback();
        }
    }

    /**
     * Test requiresNew  transactional attributes on finder method
     * the finder method is called outside TX
     */
    public void testFinderRequiresNew() throws Exception {

        getHome().finder_requiresnew();
    }

    /**
     * Test RequiresNew  transactional attributes on finder method
     * the finder method is called inside TX
     */
    public void testFinderRequiresNewTx() throws Exception {

        utx.begin();
        try {
            getHome().finder_requiresnew();
        } finally {
            utx.rollback();
        }
    }


    /**
     * Test Mandatory  transactional attributes on finder method
     * the finder method is called outside TX
     * TransactionRequiredException must be received
     */
    public void testFinderMandatory() throws Exception {
        try {
            getHome().finder_mandatory();
            fail("mandatory: should raise exception");
        } catch (javax.transaction.TransactionRequiredException e) {
        } catch (java.rmi.RemoteException e) {
            assertTrue(e.detail instanceof javax.transaction.TransactionRequiredException);
        }
    }

    /**
     * Test Mandatory  transactional attributes on finder method
     * the finder method is called inside TX
     */
    public void testFinderMandatoryTx() throws Exception {

        utx.begin();
        try {
            getHome().finder_mandatory();
        } finally {
            utx.rollback();
        }
    }

    /**
     * Test Never  transactional attributes on finder method
     * the finder method is called outside TX
     */
    public void testFinderNever() throws Exception {

        getHome().finder_never();
    }

    /**
     * Test Never  transactional attributes on finder method
     * the finder method is called inside TX
     */
    public void testFinderNeverTx() throws Exception {
        utx.begin();
        try {
            getHome().finder_never();
            fail("never: should raise exception");
        } catch (java.rmi.RemoteException e) {
        } finally {
            utx.rollback();
        }
    }


    // -----------------------------------------------------------------
    // Methods for Suite handling
    // -----------------------------------------------------------------

    protected SimpleEHome getHome() {
        if (home == null) {
            try {
                home = (SimpleEHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME), SimpleEHome.class);
            } catch (NamingException e) {
                fail("Cannot get bean home");
            }
        }
        return home;
    }


    /**
     * Create a bean and return it.
     */
    public Simple getSimple(int i) throws Exception {
        return getHome().create(i);
    }

    /**
     * This suite is all BMP test cases
     */
    public static Test suite() {
        return new TestSuite(F_TxAttributeEB.class);
    }

    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_TxAttributeEB(testtorun));
        }
    }
}
