/*
 * 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 java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.TransactionRequiredException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.transacted.Simple;
import org.objectweb.jonas.jtests.util.JTestCase;


/**
 * Transactional attribute test cases common for all type of beans (Entity/Session)
 *
 * @author Ph.Coq, Ph.Durieux
 *
 */

public abstract class A_TxAttribute  extends JTestCase {

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

    // This method depends on the home used to get it.
    public abstract Simple getSimple(int i) throws Exception;



    /**
     * Test of NotSupported attribute
     * A business method with NotSupported is called outside TX
     * this method returns if the thread is associated to a transaction
     * 
     * the expected value is  false
     */
    public void testNotSupported() throws Exception {

	Simple s = getSimple(10);
	assertEquals(s.opwith_notsupported(), false);
	s.remove();
    }

    /**
     * Test of RequiresNew attribute
     * A business method with RequiresNew is called outside TX
     * this method returns if the thread is associated to a transaction
     *
     * the expected value is  true
     */
    public void testRequiresNew() throws Exception {

	Simple s = getSimple(11);
	assertEquals(s.opwith_requires_new(), true);
	s.remove();
    }

    /**
     * Test of Required attribute
     * A business method with Required is called outside TX
     * this method returns if the thread is associated to a transaction
     *
     * the expected value is  true
     */
    public void testRequired() throws Exception {

	Simple s = getSimple(12);
	assertEquals(s.opwith_required(), true);
	s.remove();
    }

    /**
     * Test of Mandatory attribute
     * A business method with Mandatory is called outside TX
     * this method returns if the thread is associated to a transaction
     *
     * A javax.transaction.TransactionRequiredException must be received
     */

    public void testMandatory() throws Exception {

	Simple s = getSimple(13);
	try {
	    s.opwith_mandatory();
	    fail("mandatory: should raise exception");
	} catch (javax.transaction.TransactionRequiredException e) {
	} catch (RemoteException e) {
	    assertTrue(e.detail instanceof TransactionRequiredException);
	}
	s.remove();
    }

    /**
     * Test of Never  attribute
     * A business method with Never is called outside TX
     * this method returns if the thread is associated to a transaction
     * 
     * the expected value is  false
     */
    public void testNever() throws Exception {

	Simple s = getSimple(14);
	assertEquals(s.opwith_never(), false);
	s.remove();
    }


    /**
     * Test of Supports  attribute
     * A business method with Supports is called outside TX
     * this method returns if the thread is associated to a transaction
     * 
     * the expected value is  false
     */
    public void testSupports() throws Exception {

	Simple s = getSimple(15);
	assertEquals(s.opwith_supports(), false);
	s.remove();
    }

    /**
     * Test of NotSupported attribute
     * A business method with NotSupported is called inside  TX
     * this method returns if the thread is associated to a transaction
     * 
     * the expected value is  false
     */
    public void testNotSupportedTx() throws Exception {

	Simple s = getSimple(20);
	utx.begin();
	try {
	    assertEquals(s.opwith_notsupported(), false);
	} finally {
	    utx.rollback();
            s.remove();
	}
    }

    /**
     * Test of RequiresNew attribute
     * A business method with RequiresNew is called inside TX
     * this method returns if the thread is associated to a transaction
     *
     * the expected value is  true
     */
    public void testRequiresNewTx() throws Exception {
	Simple s = getSimple(21);
	utx.begin();
	try {
	    assertEquals(s.opwith_requires_new(), true);
	} finally {
	    utx.rollback();
            s.remove();
	}
    }

    /**
     * Test of Required attribute
     * A business method with Required is called inside  TX
     * this method returns if the thread is associated to a transaction
     *
     * the expected value is  true
     */
    public void testRequiredTx() throws Exception {

	Simple s = getSimple(22);
	utx.begin();
	try {
	    assertEquals(s.opwith_required(), true);
	} finally {
	    utx.rollback(); 
            s.remove();
	}

    }

    /**
     * Test of Mandatory  attribute
     * A business method with Mandatory is called inside  TX
     * this method returns if the thread is associated to a transaction
     *
     * the expected value is  true
     */
    public void testMandatoryTx() throws Exception {

	Simple s = getSimple(23);
	utx.begin();
	try {
	    assertEquals(s.opwith_mandatory(), true);
	} finally {
	    utx.rollback();
            s.remove();
	}
    }




    /**
     * Test of Never attribute
     * A business method with Mandatory is called inside TX
     * this method returns if the thread is associated to a transaction
     *
     * A java.rmi.RemoteException must be received
     */
    public void testNeverTx() throws Exception {

	Simple s = getSimple(24);
	utx.begin();
	try {
	    s.opwith_never();
	    fail("never: should raise exception");
	} catch (RemoteException e) {
	} finally {
	    utx.rollback();
            s.remove();
	}
    }

    /**
     * Test of Supports  attribute
     * A business method with Supports is called inside TX
     * this method returns if the thread is associated to a transaction
     * 
     * the expected value is  true
     */
    public void testSupportsTx() throws Exception {

	Simple s = getSimple(25);
        utx.begin();
        try {
            assertEquals(s.opwith_supports(), true);
        } finally {
	    utx.rollback();
            s.remove();
	}
	
    }

    /**
     * Test the sequence of several calls to methods
     * with different transactional contexts
     */
    public void testNoTx() throws Exception {

	Simple s = getSimple(1);
	assertEquals(s.opwith_notsupported(), false);
	assertEquals(s.opwith_requires_new(), true);
	assertEquals(s.opwith_required(), true);
	assertEquals(s.opwith_never(), false);
	assertEquals(s.opwith_supports(), false);
	s.remove();
    }

 

}
