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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.relation.mnb.AHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mnb.ARemote;
import org.objectweb.jonas.jtests.beans.relation.mnb.BHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mnb.BRemote;

/**
 * For testing many-to-many bidirectional relationships
 * @author Ph Durieux
 **/
public abstract class A_mnb extends A_Cmp2Util {

    public abstract AHomeRemote getAHome();
    public abstract BHomeRemote getBHome();

    static Hashtable tbRelationA2B = new Hashtable();


    static {
        tbRelationA2B.put("a0", new String[]{});
        tbRelationA2B.put("a1", new String[]{"b1", "b2"});
        tbRelationA2B.put("a2", new String[]{"b1", "b2", "b3"});
        tbRelationA2B.put("a3", new String[]{"b2", "b3", "b4"});
        // Translate the String[] to a Collection of String
        for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
            String aname = (String) (ea.nextElement());
            String[] tb = (String[]) tbRelationA2B.get(aname);
            ArrayList col = new ArrayList(tb.length);
            for (int i = 0; i < tb.length; i++) {
                col.add(tb[i]);
            }
            tbRelationA2B.put(aname, col);
        }
    }

    static Hashtable tbRelationB2A = new Hashtable();

    static {
        tbRelationB2A.put("b0", new String[]{});
        tbRelationB2A.put("b1", new String[]{"a1", "a2"});
        tbRelationB2A.put("b2", new String[]{"a1", "a2", "a3"});
        tbRelationB2A.put("b3", new String[]{"a2", "a3"});
        tbRelationB2A.put("b4", new String[]{"a3"});
        // Translate the String[] to a Collection of String
        for (Enumeration ea1 = tbRelationB2A.keys(); ea1.hasMoreElements();) {
            String aname1 = (String) (ea1.nextElement());
            String[] tb1 = (String[]) tbRelationB2A.get(aname1);
            ArrayList col1 = new ArrayList(tb1.length);
            for (int j = 0; j < tb1.length; j++) {
                col1.add(tb1[j]);
            }
            tbRelationB2A.put(aname1, col1);
        }
    }

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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        boolean ok = false;
        int nbtry = 0;
        while (!ok && nbtry < 3) {
            if (!isInit) {
                // load bean if not loaded yet
                useBeans("mnb", false);
                // check if tables have been initialized
                try {
                    getAHome().findByPrimaryKey("a0");
                } catch (Exception e) {
                    try {
                        // IMPORTANT: comments begin/commit to highlight a bug
                        utx.begin();
                        getAHome().create("a0");
                        getAHome().create("a1");
                        getAHome().create("a2");
                        getAHome().create("a3");
                        getBHome().create("b0");
                        getBHome().create("b1");
                        getBHome().create("b2");
                        getBHome().create("b3");
                        getBHome().create("b4");
                        ARemote a0 = getAHome().findByPrimaryKey("a0");
                        ARemote a1 = getAHome().findByPrimaryKey("a1");
                        ARemote a2 = getAHome().findByPrimaryKey("a2");
                        ARemote a3 = getAHome().findByPrimaryKey("a3");
                        Collection  rel_a0 = (Collection) (tbRelationA2B.get("a0"));
                        a0.assignB(rel_a0);
                        Collection rel_a1 = (Collection) (tbRelationA2B.get("a1"));
                        a1.assignB(rel_a1);
                        Collection rel_a2 = (Collection) (tbRelationA2B.get("a2"));
                        a2.assignB(rel_a2);
                        Collection  rel_a3 = (Collection) (tbRelationA2B.get("a3"));
                        a3.assignB(rel_a3);
                    } catch (Exception i) {
                        fail("InitialState creation problem: "+i);
                    } finally {
                        try {
                            utx.commit();
                        } catch (Exception ii) {
                        }
                    }
                }
                isInit = true;
            }
            // Check that all is OK. Sometimes, a test has failed and has corrupted
            // the bean state in the database. We must unload and reload the bean then.
            nbtry++;
            try {
                if (initStateOK()) {
                    ok = true;
                }
            } catch (Exception e) {
            }
            if (!ok) {
                debug("F_Relation_mnbEC2 setUp: unload bean and retry");
                isInit = false;
                unloadBeans("mnb");
            }
        }
    }

    /*
     * Check that we are in the same state as after the tables creation for thoses beans A and B
     * (ie if it is the initial state)
     */
    boolean initStateOK() throws Exception {
        boolean isOk = true;
        msgerror = new StringBuffer();
        // Check relations A to B
        for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) {
            String aname = (String) (ea.nextElement());
            ARemote a = getAHome().findByPrimaryKey(aname);
            Collection colActual = a.retrieveB();
            ArrayList colExpected = (ArrayList) (tbRelationA2B.get(aname));
            if (!isCollectionEqual(colExpected, colActual)) {
                isOk = false;
                msgerror = msgerror.append("Wrong relation for " + aname
                                           + " (expected:" + colExpected
                                           + ", found:" + colActual + ")");
            }
        }
        // Check Relations B to A
        for (Enumeration ea1 = tbRelationB2A.keys(); ea1.hasMoreElements();) {
            String aname1 = (String) (ea1.nextElement());
            BRemote b = getBHome().findByPrimaryKey(aname1);
            Collection colActual1 = b.retrieveA();
            ArrayList colExpected1 = (ArrayList) (tbRelationB2A.get(aname1));
            if (!isCollectionEqual(colExpected1, colActual1)) {
                isOk = false;
                msgerror = msgerror.append("Wrong relation for " + aname1
                                           + " (expected:" + colExpected1
                                           + ", found:" + colActual1 + ")");
            }
        }
        return isOk;
    }

    /**
     * Check that the bean 'a0' has no relation.
     */
    public void _testBasicGetEmpty(int tx) throws Exception {
        Collection c;
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a0");
        if (tx == TX_CONT) {
            c = a.retrieveBInNewTx();
        } else {
            c = a.retrieveB();
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        checkIsInitialState();
    }

    public void testBasicGetEmptyTxNo() throws Exception {
        _testBasicGetEmpty(TX_NO);
    }

    public void testBasicGetEmptyTxCall() throws Exception {
        _testBasicGetEmpty(TX_CALL);
    }

    public void testBasicGetEmptyTxCont() throws Exception {
        _testBasicGetEmpty(TX_CONT);
    }

    public void testBasicGetEmptyTxRb() throws Exception {
        _testBasicGetEmpty(TX_RB);
    }

    /**
     * Check the new relation a0-b0
     */
    public void _testBasicSetEmpty(int tx) throws Exception {
        ArrayList c = new ArrayList(1);
        c.add("b0");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a0");
        if (tx == TX_CONT) {
            a.assignBInNewTx(c);
        } else {
            a.assignB(c);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection c_value = a.retrieveB();
        if (tx != TX_RB) {
            assertTrue("Wrong relations a0 (required:" + c + ", found:" + c_value
                       + ")", isCollectionEqual(c_value, c));
        } else {
            assertTrue("Wrong relation a0->b0 : ", c_value.isEmpty());
        }
        // undo
        if (tx != TX_RB) {
            a.assignB(new ArrayList());
        }
        checkIsInitialState();
    }

    public void testBasicSetEmptyTxNo() throws Exception {
        _testBasicSetEmpty(TX_NO);
    }

    public void testBasicSetEmptyTxCall() throws Exception {
        _testBasicSetEmpty(TX_CALL);
    }

    public void testBasicSetEmptyTxCont() throws Exception {
        _testBasicSetEmpty(TX_CONT);
    }

    public void testBasicSetEmptyTxRb() throws Exception {
        _testBasicSetEmpty(TX_RB);
    }

    public void testPhil() throws Exception {
        utx.begin();
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        a2.clearB();
        utx.rollback();
        //checkIsInitialState();
    }

    /**
     * Check clear function
     */
    public void _testBasicClear(int tx) throws Exception {
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        if (tx == TX_CONT) {
            a2.clearBInNewTx();
        } else {
            a2.clearB();
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection a2_value = a2.retrieveB();
        BRemote b1 = getBHome().findByPrimaryKey("b1");
        BRemote b2 = getBHome().findByPrimaryKey("b2");
        BRemote b3 = getBHome().findByPrimaryKey("b3");
        ArrayList collb1 = new ArrayList(1);
        collb1.add("a1");
        ArrayList collb2 = new ArrayList(2);
        collb2.add("a1");
        collb2.add("a3");
        ArrayList collb3 = new ArrayList(1);
        collb3.add("a3");
        Collection col=null;
        if (tx != TX_RB) {
            assertTrue("Wrong relations a2 (required: Empty  "+", found:" + a2_value
                       + ")", a2_value.isEmpty());
            col = getBHome().findByPrimaryKey("b1").retrieveA();
            assertTrue("Wrong collection returned by b1.getA(): expected:" + collb1
                       + " found: " + col, isCollectionEqual(collb1, col));
            col = getBHome().findByPrimaryKey("b2").retrieveA();
            assertTrue("Wrong collection returned by b2.getA(): expected:" + collb2
                       + " found: " + col, isCollectionEqual(collb2, col));
            col = getBHome().findByPrimaryKey("b3").retrieveA();
            assertTrue("Wrong collection returned by b3.getA(): expected:" + collb3
                       + " found: " + col, isCollectionEqual(collb3, col));
            // undo
            ArrayList a2undo = new ArrayList(3);
            a2undo.add("b1");
            a2undo.add("b2");
            a2undo.add("b3");
            a2.assignB(a2undo);
        }
        checkIsInitialState();
    }

    public void testBasicClearTxNo() throws Exception {
        _testBasicClear(TX_NO);
    }

    public void testBasicClearTxCall() throws Exception {
        _testBasicClear(TX_CALL);
    }

    public void testBasicClearTxCont() throws Exception {
        _testBasicClear(TX_CONT);
    }

    public void testBasicClearTxRb() throws Exception {
        _testBasicClear(TX_RB);
    }

    /**
     * Remove an element in a relation.
     */
    public void _testCohRemoveInRel(int tx) throws Exception {
        String bRemoved = "b4";
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a3");
        if (tx == TX_CONT) {
            a.removeFromBInNewTx(bRemoved);
        } else {
            a.removeFromB(bRemoved);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        BRemote b4 = getBHome().findByPrimaryKey("b4");
        Collection cb4 = b4.retrieveA();
        if (tx != TX_RB) {
            assertEquals("Wrong relations size for a3: ", 2, ca.size());
            assertEquals("Wrong relations size for b4: ", 0, cb4.size());
        } else {
            assertEquals("Wrong relations size for a3: ", 3, ca.size());
            assertEquals("Wrong relations size for b4: ", 1, cb4.size());
        }
        // undo
        if (tx != TX_RB) {
            a.addInB(bRemoved);
        }
        // check to initial state
        checkIsInitialState();

    }

    public void testCohRemoveInRelTxNo() throws Exception {
        _testCohRemoveInRel(TX_NO);
    }

    public void testCohRemoveInRelTxCall() throws Exception {
        _testCohRemoveInRel(TX_CALL);
    }

    public void testCohRemoveInRelTxCont() throws Exception {
        _testCohRemoveInRel(TX_CONT);
    }

    public void testCohRemoveInRelTxRb() throws Exception {
        _testCohRemoveInRel(TX_RB);
    }

    /**
     * Check the coherence 10.3.7.6: a1.assignB(a3.retrieveB()) =>
     *   a1.retreiveB().contains("b2") &&
     *   a1.retreiveB().contains("b3") &&
     *   a1.retreiveB().contains("b4") &&
     *   a3.retreiveB().contains("b2") &&
     *   a3.retreiveB().contains("b3") &&
     *   a3.retreiveB().contains("b4") &&
     *   b1.retreiveB().contains("a2") &&
     *   b2.retreiveB().contains("a1") &&
     *   b2.retreiveB().contains("a2") &&
     *   b2.retreiveB().contains("a3") &&
     *   b3.retreiveB().contains("b4") &&
     *   b3.retreiveB().contains("a1") &&
     *   b3.retreiveB().contains("a2") &&
     *   b3.retreiveB().contains("a3") &&
     *   b4.retreiveB().contains("a1") &&
     *   b4.retreiveB().contains("a3") &&
     *   b4.retreiveB().contains("a4") &&
     *   b4.retreiveB().contains("a5") &&
     */
    public void _testCoSetFirst(int tx) throws Exception {
        String c = null;
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection c1 = a1.retrieveB();
        if (tx == TX_CONT) {
            a1.assignBInNewTx(a3.retrieveB());
        } else {
            a1.assignB(a3.retrieveB());
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }

        // checking
        if (tx == TX_RB) {
            checkIsInitialState();
            return;
        }

        ArrayList al = new ArrayList(3);
        Collection col = null;
        al.add("b2");
        al.add("b3");
        al.add("b4");
        col = a1.retrieveB();
        assertTrue("Wrong collection returned by a1.getB(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));
        col = a3.retrieveB();
        assertTrue("Wrong collection returned by a3.getB(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));

        al = new ArrayList(1);
        al.add("a2");
        col = getBHome().findByPrimaryKey("b1").retrieveA();
        assertTrue("Wrong collection returned by b1.getA(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));

        al = new ArrayList(3);
        al.add("a1");
        al.add("a2");
        al.add("a3");
        col = getBHome().findByPrimaryKey("b2").retrieveA();
        assertTrue("Wrong collection returned by b2.getA(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));

        al = new ArrayList(2);
        al.add("a1");
        al.add("a2");
        al.add("a3");
        col = getBHome().findByPrimaryKey("b3").retrieveA();
        assertTrue("Wrong collection returned by b3.getA(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));

        al = new ArrayList(2);
        al.add("a1");
        al.add("a3");
        col = getBHome().findByPrimaryKey("b4").retrieveA();
        assertTrue("Wrong collection returned by b4.getA(): expected:" + al
                   + " found: " + col, isCollectionEqual(al, col));

        a1.assignB(null);
        al = new ArrayList(2);
        al.add("b1");
        al.add("b2");
        a1.assignBInNewTx(al);
        checkIsInitialState();

    }

    public void testCoSetFirstTxNo() throws Exception {
        _testCoSetFirst(TX_NO);
    }

    public void testCoSetFirstTxCall() throws Exception {
        _testCoSetFirst(TX_CALL);
    }

    public void testCoSetFirstTxCont() throws Exception {
        _testCoSetFirst(TX_CONT);
    }

    public void testCoSetFirstTxRb() throws Exception {
        _testCoSetFirst(TX_RB);
    }

    /**
     * Check the coherence 10.3.7.6 : a1.getB().add(b3) =>
     *   a1.retreiveB().contains("b1") &&
     *   a1.retreiveB().contains("b2") &&
     *   a1.retreiveB().contains("b3") &&
     *   b3.retreiveB().contains("a1") &&
     *   b3.retreiveB().contains("a2") &&
     *   b3.retreiveB().contains("a3") &&
     *   b3.retreiveB().contains("a4") &&
     */
    public void _testCoSetSecond(int tx) throws Exception {
        String c = null;

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a1 = getAHome().findByPrimaryKey("a1");

        if (tx == TX_CONT) {
            a1.addInBInNewTx("b3");
        } else {
            a1.addInB("b3");
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        if (tx != TX_RB) {
            ArrayList al = new ArrayList(3);
            Collection col = null;
            al.add("b1");
            al.add("b2");
            al.add("b3");
            col = a1.retrieveB();
            assertTrue("Wrong collection returned by a1.getB(): expected:" + al
                       + " found: " + col, isCollectionEqual(al, col));

            al = new ArrayList(3);
            al.add("a1");
            al.add("a2");
            al.add("a3");

            col = getBHome().findByPrimaryKey("b3").retrieveA();
            assertTrue("Wrong collection returned by b3.getA(): expected:" + al
                       + " found: " + col, isCollectionEqual(al, col));

            // undo
            a1.removeFromB("b3");
        }
        checkIsInitialState();
    }

    public void testCoSetSecondTxNo() throws Exception {
        _testCoSetSecond(TX_NO);
    }

    public void testCoSetSecondTxCall() throws Exception {
        _testCoSetSecond(TX_CALL);
    }

    public void testCoSetSecondTxCont() throws Exception {
        _testCoSetSecond(TX_CONT);
    }

    public void testCoSetSecondTxRb() throws Exception {
        _testCoSetSecond(TX_RB);
    }

    /**
     * Check the coherence 10.3.7.6 : a2.getB().remove(b2) =>
     *   a2.retreiveB().contains("b1") &&
     *   a2.retreiveB().contains("b3") &&
     *   b2.retreiveB().contains("a1") &&
     *   b2.retreiveB().contains("a3") &&
     *   !b2.retreiveB().contains("a2")
     */
    public void _testCoSetRemove(int tx) throws Exception {

        if (tx == TX_CONT) {
            // The transaction attribute of the remove method is TX_SUPPORT,
            // so the transaction cannot be initiate by the container
            fail("Transaction cannot be initiate by the container for this test");
        }

        String c = null;

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        BRemote b2 = getBHome().findByPrimaryKey("b2");
        Collection c2 = a2.retrieveB();
        if (tx == TX_CONT) {
            a2.removeFromBInNewTx("b2");
        } else {
            a2.removeFromB("b2");
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        if (tx != TX_RB) {
            ArrayList al = new ArrayList(2);
            Collection col = null;
            al.add("b1");
            al.add("b3");
            col = a2.retrieveB();
            assertTrue("Wrong collection returned by a2.getB(): expected:" + al
                       + " found: " + col, isCollectionEqual(al, col));

            al = new ArrayList(2);
            al.add("a1");
            al.add("a3");
            col = b2.retrieveA();
            assertTrue("Wrong collection returned by b2.getA(): expected:" + al
                       + " found: " + col, isCollectionEqual(al, col));

            // undo
            a2.addInB("b2");
        }
        checkIsInitialState();
    }

    public void testCoSetRemoveTxNo() throws Exception {
        _testCoSetRemove(TX_NO);
    }

    public void testCoSetRemoveTxCall() throws Exception {
        _testCoSetRemove(TX_CALL);
    }

    public void testCoSetRemoveTxRb() throws Exception {
        _testCoSetRemove(TX_RB);
    }

    /**
     * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
     */
    public void _testCohRemoveB(int tx) throws Exception {

        if (tx == TX_CONT) {
            // The transaction attribute of the remove method is TX_SUPPORT,
            // so the transaction cannot be initiate by the container
            fail("Transaction cannot be initiate by the container for this test");
        }

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        getBHome().remove("b2");
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking

        if (tx != TX_RB) {
            ARemote a1 = getAHome().findByPrimaryKey("a1");
            ARemote a2 = getAHome().findByPrimaryKey("a2");
            ARemote a3 = getAHome().findByPrimaryKey("a3");
            Collection idrela1 = a1.retrieveB();
            Collection idrela2 = a2.retrieveB();
            Collection idrela3 = a3.retrieveB();
            ArrayList col_a1 = new ArrayList(1);
            col_a1.add("b1");
            ArrayList col_a2 = new ArrayList(2);
            col_a2.add("b1");
            col_a2.add("b3");
            ArrayList col_a3 = new ArrayList(2);
            col_a3.add("b3");
            col_a3.add("b4");
            assertTrue("Wrong relation a1 : Expected [b1] , found "+idrela1, isCollectionEqual(idrela1,col_a1));
            assertTrue("Wrong relation a2 : Expected [b1, b3] , found "+idrela2 , isCollectionEqual(idrela2,col_a2));
            assertTrue("Wrong relation a3 : Expected [b3, b4] , found "+idrela3, isCollectionEqual(idrela3,col_a3));
            // undo
            BRemote b2 = getBHome().create("b2");
            col_a2 = new ArrayList(3);
            col_a2.add("a1");
            col_a2.add("a2");
            col_a2.add("a3");

            b2.assignAInNewTx(col_a2);
        }
        checkIsInitialState();
    }

    public void testCohRemoveBTxNo() throws Exception {
        _testCohRemoveB(TX_NO);
    }

    public void testCohRemoveBTxCall() throws Exception {
        _testCohRemoveB(TX_CALL);
    }

    public void testCohRemoveBTxRb() throws Exception {
        _testCohRemoveB(TX_RB);
    }

    /**
     * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
     * Same as _testCohRemoveB except that the called remove method is on the bean
     * instead of the home.
     */
    public void _testCohBeanRemoveB(int tx) throws Exception {

        if (tx == TX_CONT) {
            // The transaction attribute of the remove method is TX_SUPPORT,
            // so the transaction cannot be initiate by the container
            fail("Transaction cannot be initiate by the container for this test");
        }

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        BRemote b = getBHome().findByPrimaryKey("b2");
        b.remove();
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking

        if (tx != TX_RB) {
            ARemote a1 = getAHome().findByPrimaryKey("a1");
            ARemote a2 = getAHome().findByPrimaryKey("a2");
            ARemote a3 = getAHome().findByPrimaryKey("a3");
            Collection idrela1 = a1.retrieveB();
            Collection idrela2 = a2.retrieveB();
            Collection idrela3 = a3.retrieveB();
            ArrayList col_a1 = new ArrayList(1);
            col_a1.add("b1");
            ArrayList col_a2 = new ArrayList(2);
            col_a2.add("b1");
            col_a2.add("b3");
            ArrayList col_a3 = new ArrayList(2);
            col_a3.add("b3");
            col_a3.add("b4");
            assertTrue("Wrong relation a1 : Expected [b1] , found "+idrela1, isCollectionEqual(idrela1,col_a1));
            assertTrue("Wrong relation a2 : Expected [b1, b3] , found "+idrela2 , isCollectionEqual(idrela2,col_a2));
            assertTrue("Wrong relation a3 : Expected [b3, b4] , found "+idrela3, isCollectionEqual(idrela3,col_a3));
            // undo
            BRemote b2 = getBHome().create("b2");
            col_a2 = new ArrayList(3);
            col_a2.add("a1");
            col_a2.add("a2");
            col_a2.add("a3");

            b2.assignAInNewTx(col_a2);
        }
        checkIsInitialState();
    }

    public void testCohBeanRemoveBTxNo() throws Exception {
        _testCohBeanRemoveB(TX_NO);
    }

    public void testCohBeanRemoveBTxCall() throws Exception {
        _testCohBeanRemoveB(TX_CALL);
    }

    public void testCohBeanRemoveBTxRb() throws Exception {
        _testCohBeanRemoveB(TX_RB);
    }

    /**
     * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
     */
    public void _testCohRemoveA(int tx) throws Exception {

        if (tx == TX_CONT) {
            // The transaction attribute of the remove method is TX_SUPPORT,
            // so the transaction cannot be initiate by the container
            fail("Transaction cannot be initiate by the container for this test");
        }

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        getAHome().remove("a2");
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking

        if (tx != TX_RB) {
            BRemote b1 = getBHome().findByPrimaryKey("b1");
            BRemote b2 = getBHome().findByPrimaryKey("b2");
            BRemote b3 = getBHome().findByPrimaryKey("b3");
            Collection idrelb1 = b1.retrieveA();
            Collection idrelb2 = b2.retrieveA();
            Collection idrelb3 = b3.retrieveA();
            ArrayList col_b1 = new ArrayList(1);
            col_b1.add("a1");
            ArrayList col_b2 = new ArrayList(2);
            col_b2.add("a1");
            col_b2.add("a3");
            ArrayList col_b3 = new ArrayList(1);
            col_b3.add("a3");
            assertTrue("Wrong relation b1 : Expected [a1] , found "+idrelb1, isCollectionEqual(idrelb1,col_b1));
            assertTrue("Wrong relation b2 : Expected [a1, a3] , found "+idrelb2 , isCollectionEqual(idrelb2,col_b2));
            assertTrue("Wrong relation b3 : Expected [a3] , found "+idrelb3, isCollectionEqual(idrelb3,col_b3));
            // undo
            getAHome().create("a2");
            ARemote a2 = getAHome().findByPrimaryKey("a2");
            ArrayList col_a2 = new ArrayList(3);
            col_a2.add("b1");
            col_a2.add("b2");
            col_a2.add("b3");

            a2.assignB(col_a2);
        }
        checkIsInitialState();
    }

    public void testCohRemoveATxNo() throws Exception {
        _testCohRemoveA(TX_NO);
    }

    public void testCohRemoveATxCall() throws Exception {
        _testCohRemoveA(TX_CALL);
    }

    public void testCohRemoveATxRb() throws Exception {
        _testCohRemoveA(TX_RB);
    }

    /**
     * Test of coherence a2.remove() => b1.retrieveB()=null && b2.retrieveB()=null && b3.retrieveB()=null
     * Same as _testCohRemoveA except that the called remove method is on the bean
     * instead of the home.
     */
    public void _testCohBeanRemoveA(int tx) throws Exception {

        if (tx == TX_CONT) {
            // The transaction attribute of the remove method is TX_SUPPORT,
            // so the transaction cannot be initiate by the container
            fail("Transaction cannot be initiate by the container for this test");
        }

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a2");
        a.remove();
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking

        if (tx != TX_RB) {
            BRemote b1 = getBHome().findByPrimaryKey("b1");
            BRemote b2 = getBHome().findByPrimaryKey("b2");
            BRemote b3 = getBHome().findByPrimaryKey("b3");
            Collection idrelb1 = b1.retrieveA();
            Collection idrelb2 = b2.retrieveA();
            Collection idrelb3 = b3.retrieveA();
            ArrayList col_b1 = new ArrayList(1);
            col_b1.add("a1");
            ArrayList col_b2 = new ArrayList(2);
            col_b2.add("a1");
            col_b2.add("a3");
            ArrayList col_b3 = new ArrayList(1);
            col_b3.add("a3");
            assertTrue("Wrong relation b1 : Expected [a1] , found "+idrelb1, isCollectionEqual(idrelb1,col_b1));
            assertTrue("Wrong relation b2 : Expected [a1, a3] , found "+idrelb2 , isCollectionEqual(idrelb2,col_b2));
            assertTrue("Wrong relation b3 : Expected [a3] , found "+idrelb3, isCollectionEqual(idrelb3,col_b3));
            // undo
            getAHome().create("a2");
            ARemote a2 = getAHome().findByPrimaryKey("a2");
            ArrayList col_a2 = new ArrayList(3);
            col_a2.add("b1");
            col_a2.add("b2");
            col_a2.add("b3");

            a2.assignB(col_a2);
        }
        checkIsInitialState();
    }

    public void testCohBeanRemoveATxNo() throws Exception {
        _testCohBeanRemoveA(TX_NO);
    }

    public void testCohBeanRemoveATxCall() throws Exception {
        _testCohBeanRemoveA(TX_CALL);
    }

    public void testCohBeanRemoveATxRb() throws Exception {
        _testCohBeanRemoveA(TX_RB);
    }

}
