/*
 * 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.mnu.AHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mnu.ARemote;
import org.objectweb.jonas.jtests.beans.relation.mnu.BHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mnu.BRemote;

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

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

    /**
     * predefined relations between a and b beans
     */
    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);
        }
    }


    public A_mnu(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("mnu", false);
                // check if tables have been initialized
                try {
                    getAHome().findByPrimaryKey("a0");
                } catch (Exception e) {
                    try {
                        utx.begin();
                        ARemote a0 = getAHome().create("a0");
                        ARemote a1 = getAHome().create("a1");
                        ARemote a2 = getAHome().create("a2");
                        ARemote a3 = getAHome().create("a3");
                        getBHome().create("b0");
                        getBHome().create("b1");
                        getBHome().create("b2");
                        getBHome().create("b3");
                        getBHome().create("b4");
                        a0.assignB((Collection) tbRelationA2B.get("a0"));
                        a1.assignB((Collection) tbRelationA2B.get("a1"));
                        a2.assignB((Collection) tbRelationA2B.get("a2"));
                        a3.assignB((Collection) tbRelationA2B.get("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) {
                isInit = false;
                unloadBeans("mnu");
            }
        }
    }

    /*
     * 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();
        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 + ")");
            }
        }
        return isOk;
    }


    /**
     * Check that the bean 'a3' 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);
    }

    /**
     * Ckeck 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));
            // undo
            a.assignB(new ArrayList(0));
        }
        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);
    }

    /**
     * See 10.3.7.7 of spec  : 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") &&
     */
    public void _testBasicSetFirst(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) {
            ArrayList c1_att = new ArrayList(3);
            c1_att.add("b2");
            c1_att.add("b3");
            c1_att.add("b4");
            assertTrue("Wrong test of coherence First before commit: ", isCollectionEqual(a1.retrieveB(),c1_att));
        } else {
            assertTrue("Wrong test of coherence First before roolback: ", isCollectionEqual(a1.retrieveB(),c1));
        }
        // undo
        if (tx != TX_RB) {
            a1.assignB(null);
            a1.assignB(c1);
        }
        checkIsInitialState();
    }
    public void testBasicSetFirstTxNo() throws Exception {
        _testBasicSetFirst(TX_NO);
    }
    public void testBasicSetFirstTxCall() throws Exception {
        _testBasicSetFirst(TX_CALL);
    }
    public void testBasicSetFirstTxCont() throws Exception {
        _testBasicSetFirst(TX_CONT);
    }
    public void testBasicSetFirstTxRb() throws Exception {
        _testBasicSetFirst(TX_RB);
    }

    /**
     * See  10.3.7.7 of spec : a1.getB().add(b3) =>
     *   a1.retreiveB().contains("b1") &&
     *   a1.retreiveB().contains("b2") &&
     *   a1.retreiveB().contains("b3") &&
     */
    public void _testBasicSetSecond(int tx) throws Exception {
        String c = null;

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

        if (tx == TX_CONT) {
            a1.addInB("b3");
        } else {
            a1.addInB("b3");
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        if (tx != TX_RB) {
            ArrayList c1_att = new ArrayList(3);
            c1_att.add("b1");
            c1_att.add("b2");
            c1_att.add("b3");
            assertTrue("Wrong test of coherence Second before commit: ", isCollectionEqual(a1.retrieveB(),c1_att));
        } else {
            assertTrue("Wrong test of coherence Second before roolback: ", isCollectionEqual(a1.retrieveB(),c1));
        }
        // undo
        if (tx != TX_RB) {
            a1.assignB(null);
            a1.assignB(c1);
        }
        checkIsInitialState();
    }

    public void testBasicSetSecondTxNo() throws Exception {
        _testBasicSetSecond(TX_NO);
    }
    public void testBasicSetSecondTxCall() throws Exception {
        _testBasicSetSecond(TX_CALL);
    }
    public void testBasicSetSecondTxCont() throws Exception {
        _testBasicSetSecond(TX_CONT);
    }
    public void testBasicSetSecondTxRb() throws Exception {
        _testBasicSetSecond(TX_RB);
    }


    /**
     * See  10.3.7.7 of spec : a2.getB().remove(b2) =>
     *   a2.retreiveB().contains("b1") &&
     *   a2.retreiveB().contains("b3") &&
     */
    public void _testBasicSetRemove(int tx) throws Exception {
        String c = null;

        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        Collection c2 = a2.retrieveB();
        if (tx == TX_CONT) {
            a2.removeFromB("b2");
        } else {
            a2.removeFromB("b2");
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        if (tx != TX_RB) {
            ArrayList c2_att = new ArrayList(3);
            c2_att.add("b1");
            c2_att.add("b3");
            assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(),c2_att));
        } else {
            assertTrue("Wrong test of coherence Remove before roolback: ", isCollectionEqual(a2.retrieveB(),c2));
        }
        // undo
        if (tx != TX_RB) {
            a2.assignB(c2);
        }
        checkIsInitialState();
    }

    public void testBasicSetRemoveTxNo() throws Exception {
        _testBasicSetRemove(TX_NO);
    }
    public void testBasicSetRemoveTxCall() throws Exception {
        _testBasicSetRemove(TX_CALL);
    }
    public void testBasicSetRemoveTxCont() throws Exception {
        _testBasicSetRemove(TX_CONT);
    }
    public void testBasicSetRemoveTxRb() throws Exception {
        _testBasicSetRemove(TX_RB);
    }

    /**
     * Ckeck 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();
        if (tx != TX_RB) {
            assertTrue("Wrong relations a2 (required: Empty, found:" + a2_value + ")", a2_value.isEmpty());

            // 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();
        if (tx != TX_RB) {
            assertEquals("Wrong relations size for a3: ", 2, ca.size());
        } else {
            assertEquals("Wrong relations size for a3: ", 3, ca.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);
    }

    /**
     * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
     */
    public void _testCohRemove(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
        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");
        if (tx != TX_RB) {
            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
            getBHome().create("b2");
            col_a1 = new ArrayList(2);
            col_a1.add("b1");
            col_a1.add("b2");
            col_a2 = new ArrayList(3);
            col_a2.add("b1");
            col_a2.add("b2");
            col_a2.add("b3");
            a1.assignB(col_a1);
            col_a3 = new ArrayList(3);
            col_a3.add("b2");
            col_a3.add("b3");
            col_a3.add("b4");

            a1.assignB(col_a1);
            a2.assignB(col_a2);
            a3.assignB(col_a3);
        }
        checkIsInitialState();
    }

    public void testCohRemoveTxNo() throws Exception {
        _testCohRemove(TX_NO);
    }

    public void testCohRemoveTxCall() throws Exception {
        _testCohRemove(TX_CALL);
    }

    public void testCohRemoveTxRb() throws Exception {
        _testCohRemove(TX_RB);
    }

    /**
     * Test of coherence b2.remove() => a1.retrieveB()=null && a2.retrieveB()=null && a3.retrieveB()=null
     * Same as _testCohRemove except that the called remove method is on the bean
     * instead of the home.
     */
    public void _testCohBeanRemove(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
        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");
        if (tx != TX_RB) {
            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
            getBHome().create("b2");
            col_a1 = new ArrayList(2);
            col_a1.add("b1");
            col_a1.add("b2");
            col_a2 = new ArrayList(3);
            col_a2.add("b1");
            col_a2.add("b2");
            col_a2.add("b3");
            a1.assignB(col_a1);
            col_a3 = new ArrayList(3);
            col_a3.add("b2");
            col_a3.add("b3");
            col_a3.add("b4");

            a1.assignB(col_a1);
            a2.assignB(col_a2);
            a3.assignB(col_a3);
        }
        checkIsInitialState();
    }

    public void testCohBeanRemoveTxNo() throws Exception {
        _testCohBeanRemove(TX_NO);
    }

    public void testCohBeanRemoveTxCall() throws Exception {
        _testCohBeanRemove(TX_CALL);
    }

    public void testCohBeanRemoveTxRb() throws Exception {
        _testCohBeanRemove(TX_RB);
    }

    /**
     * These several tests linked fails on Oracle (testCohBeanRemoveTxCall)
     */
    public void testRemoveSeries() throws Exception {

        testBasicSetRemoveTxCall();
        testBasicClearTxCall();
        testBasicClearTxRb();
        testCohRemoveTxNo();
        testCohRemoveTxCall();
    }

    public void testRemoves1() throws Exception {
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection col;
        Collection b;
        Collection a1bsave = a1.retrieveB();
        Collection a2bsave = a2.retrieveB();
        Collection a3bsave = a3.retrieveB();

        //_testBasicSetRemove(TX_CALL);
        utx.begin();
        a2.removeFromB("b2");
        utx.commit();
        // checking
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(), col));
        // undo
        a2.assignB(a2bsave);

        //_testBasicClear(TX_CALL);
        utx.begin();
        a2.clearB();
        utx.commit();
        // checking
        b = a2.retrieveB();
        assertTrue("Wrong relations a2 (required: Empty, found:" + b + ")", b.isEmpty());
	    // undo
        a2.assignB(a2bsave);

        //_testBasicClear(TX_RB);
        utx.begin();
        a2.clearB();
        utx.rollback();

        //_testCohRemove(TX_NO);
        getBHome().remove("b2");
        // check a1
        col = new ArrayList(1);
        col.add("b1");
        b = a1.retrieveB();
        assertTrue("Wrong relation a1 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a2
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        b = a2.retrieveB();
        assertTrue("Wrong relation a2 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a3
        col = new ArrayList(2);
        col.add("b3");
        col.add("b4");
        b = a3.retrieveB();
        assertTrue("Wrong relation a3 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // undo
        getBHome().create("b2");
        a1.assignB(a1bsave);
        a2.assignB(a2bsave);
        a3.assignB(a3bsave);
        checkIsInitialState();
    }

    public void testRemoves2() throws Exception {
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection col;
        Collection b;
        Collection a1bsave = a1.retrieveB();
        Collection a2bsave = a2.retrieveB();
        Collection a3bsave = a3.retrieveB();

        //_testBasicSetRemove(TX_CALL);
        utx.begin();
        a2.removeFromB("b2");
        utx.commit();
        // checking
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(), col));
        // undo
        a2.assignB(a2bsave);

        //_testBasicClear(TX_RB);
        utx.begin();
        a2.clearB();
        utx.rollback();

        //_testCohRemove(TX_NO);
        getBHome().remove("b2");
        // check a1
        col = new ArrayList(1);
        col.add("b1");
        b = a1.retrieveB();
        assertTrue("Wrong relation a1 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a2
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        b = a2.retrieveB();
        assertTrue("Wrong relation a2 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a3
        col = new ArrayList(2);
        col.add("b3");
        col.add("b4");
        b = a3.retrieveB();
        assertTrue("Wrong relation a3 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // undo
        getBHome().create("b2");
        a1.assignB(a1bsave);
        a2.assignB(a2bsave);
        a3.assignB(a3bsave);
        checkIsInitialState();
    }

    public void testRemoves3() throws Exception {
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection col;
        Collection b;
        Collection a1bsave = a1.retrieveB();
        Collection a2bsave = a2.retrieveB();
        Collection a3bsave = a3.retrieveB();

        //_testBasicSetRemove(TX_CALL);
        utx.begin();
        a2.removeFromB("b2");
        utx.commit();
        // checking
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(), col));
        // undo
        a2.assignB(a2bsave);

        //_testBasicClear(TX_CALL);
        utx.begin();
        a2.clearB();
        utx.commit();
        // checking
        b = a2.retrieveB();
        assertTrue("Wrong relations a2 (required: Empty, found:" + b + ")", b.isEmpty());
	    // undo
        a2.assignB(a2bsave);

        //_testCohRemove(TX_NO);
        getBHome().remove("b2");
        // check a1
        col = new ArrayList(1);
        col.add("b1");
        b = a1.retrieveB();
        assertTrue("Wrong relation a1 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a2
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        b = a2.retrieveB();
        assertTrue("Wrong relation a2 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a3
        col = new ArrayList(2);
        col.add("b3");
        col.add("b4");
        b = a3.retrieveB();
        assertTrue("Wrong relation a3 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // undo
        getBHome().create("b2");
        a1.assignB(a1bsave);
        a2.assignB(a2bsave);
        a3.assignB(a3bsave);
        checkIsInitialState();
    }

    public void testRemoves4() throws Exception {
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection col;
        Collection b;
        Collection a1bsave = a1.retrieveB();
        Collection a2bsave = a2.retrieveB();
        Collection a3bsave = a3.retrieveB();

        //_testBasicClear(TX_RB);
        utx.begin();
        a2.clearB();
        utx.rollback();
        // check a2
        col = new ArrayList(2);
        col.add("b1");
        col.add("b2");
        col.add("b3");
        b = a2.retrieveB();
        assertTrue("Wrong relation a2 : Expected " + col + ", found " + b, isCollectionEqual(b, col));

        getBHome().remove("b2");
        // check a1
        col = new ArrayList(1);
        col.add("b1");
        b = a1.retrieveB();
        assertTrue("Wrong relation a1 : Expected " + col + ", found " + b, isCollectionEqual(b, col));

        // check a2
        col = new ArrayList(2);
        col.add("b1");
        col.add("b3");
        b = a2.retrieveB();
        assertTrue("Wrong relation a2 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // check a3
        col = new ArrayList(2);
        col.add("b3");
        col.add("b4");
        b = a3.retrieveB();
        assertTrue("Wrong relation a3 : Expected " + col + ", found " + b, isCollectionEqual(b, col));
        // undo
        getBHome().create("b2");
        a1.assignB(a1bsave);
        a2.assignB(a2bsave);
        a3.assignB(a3bsave);
        checkIsInitialState();
    }

}
