/*
 * 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 junit.framework.Test;
import junit.framework.TestSuite;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

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


/**
 * For testing one-to-many unidirectional relationships
 * @author S.Chassande-Barrioz, Helene Joanin
 */

public abstract class A_omu extends A_Cmp2Util {

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

    static Hashtable tbRelationA2B = new Hashtable();

    static {
        tbRelationA2B.put("as0", new String[]{});
        tbRelationA2B.put("as1", new String[]{"bs1_1"});
        tbRelationA2B.put("as2", new String[]{"bs2_1", "bs2_2", "bs2_3"});
        tbRelationA2B.put("a0", new String[]{});
        tbRelationA2B.put("a1", new String[]{"b1_1", "b1_2", "b1_3"});
        tbRelationA2B.put("a2", new String[]{"b2_1", "b2_2", "b2_3"});
        tbRelationA2B.put("a3", new String[]{"b3_1"});
        // 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_omu(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("omu", true);
                // check if tables have been initialized
                try {
                    getAHome().findByPrimaryKey("a0");
                } catch (Exception e) {
                    try {
                        utx.begin();
                        getBHome().create("bs0");
                        getBHome().create("bs1_1");
                        getBHome().create("bs2_1");
                        getBHome().create("bs2_2");
                        getBHome().create("bs2_3");
                        getBHome().create("bs3");
                        getBHome().create("b0");
                        getBHome().create("b1_1");
                        getBHome().create("b1_2");
                        getBHome().create("b1_3");
                        getBHome().create("b2_1");
                        getBHome().create("b2_2");
                        getBHome().create("b2_3");
                        getBHome().create("b3_1");
                        ARemote as0 = getAHome().create("as0");
                        ARemote as1 = getAHome().create("as1");
                        ARemote as2 = getAHome().create("as2");
                        ARemote a0 = getAHome().create("a0");
                        ARemote a1 = getAHome().create("a1");
                        ARemote a2 = getAHome().create("a2");
                        ARemote a3 = getAHome().create("a3");
                        as1.assignB((Collection)tbRelationA2B.get("as1"));
                        as2.assignB((Collection)tbRelationA2B.get("as2"));
                        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("omu");
            }
        }
    }

    /*
     * 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));
            debug("Relation for " + aname + " :: expected:" + colExpected + ", found:" + colActual );
            if (!isCollectionEqual(colExpected, colActual)) {
                isOk = false;
                msgerror = msgerror.append("Wrong relation for " + aname
                                           + " (expected:" + colExpected
                                           + ", found:" + colActual + ")");
            }
        }
        return isOk;
    }

    /**
     * read A, but not B
     */
    public void testReadA() throws Exception {
        Collection c = getAHome().findAll();
        for (Iterator i = c.iterator(); i.hasNext(); ) {
            ARemote a = (ARemote) PortableRemoteObject.narrow(i.next(), ARemote.class);
            String n = a.getId();
        }
    }

    /**
     * Check that the bean 'as0' has no relation.
     */
    public void _testBasicGetEmpty(int tx) throws Exception {
        Collection c = null;
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as0");
        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);
    }

    /**
     * Check that the bean 'as1' has only one relation with 'bs1_1'.
     */
    public void _testBasicGetOne(int tx) throws Exception {

        Collection c = null;
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as1");
        if (tx == TX_CONT) {
            c = a.retrieveBInNewTx();
        } else {
            c = a.retrieveB();
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        assertEquals("Wrong relations size for as1: ", 1, c.size());
        String s = "bs1_1";
        assertTrue("Relation missing " + s + " for as1: ", c.contains(s));
        checkIsInitialState();
    }

    public void testBasicGetOneTxNo() throws Exception {
        _testBasicGetOne(TX_NO);
    }

    public void testBasicGetOneTxCall() throws Exception {
        _testBasicGetOne(TX_CALL);
    }

    public void testBasicGetOneTxCont() throws Exception {
        _testBasicGetOne(TX_CONT);
    }

    public void testBasicGetOneTxRb() throws Exception {
        _testBasicGetOne(TX_RB);
    }

    /**
     * Check that the bean 'as2' has many relations with 'bs2_1', 'bs2_2', 'bs2_3'.
     */
    public void _testBasicGetMany(int tx) throws Exception {

        Collection c = null;
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as2");
        if (tx == TX_CONT) {
            c = a.retrieveBInNewTx();
        } else {
            c = a.retrieveB();
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        assertEquals("Relations size of: ", 3, c.size());
        String s = "bs2_1";
        assertTrue("Relation missing " + s + ": ", c.contains(s));
        s = "bs2_2";
        assertTrue("Relation missing " + s + ": ", c.contains(s));
        s = "bs2_3";
        assertTrue("Relation missing " + s + ": ", c.contains(s));
        checkIsInitialState();
    }

    public void testBasicGetManyTxNo() throws Exception {
        _testBasicGetMany(TX_NO);
    }

    public void testBasicGetManyTxCall() throws Exception {
        _testBasicGetMany(TX_CALL);
    }

    public void testBasicGetManyTxCont() throws Exception {
        _testBasicGetMany(TX_CONT);
    }


    /**
     * Set a relation to empty.
     * Before: as1 -> bs1_1
     * After:  as1    bs1_1
     */
    public void _testBasicSetEmpty(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as1");
        if (tx == TX_CONT) {
            a.assignBInNewTx(ce);
        } else {
            a.assignB(ce);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertEquals("Relations size of: ", 0, ca.size());
        } else {
            assertEquals("Relations size of: ", 1, ca.size());
        }
        // undo
        if (tx != TX_RB) {
            a.addInB("bs1_1");
            // Check undo
            ca = a.retrieveB();
            assertEquals("After undo, Relations size of: ", 1, ca.size());
        }
        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);
    }


    /**
     * Set a relation to empty by clearing the collection.
     * Before: as1 -> bs1_1
     * After:  as1    bs1_1
     */
    public void _testBasicClear(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as1");
        if (tx == TX_CONT) {
            a.clearBInNewTx();
        } else {
            a.clearB();
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertEquals("Relations size of: ", 0, ca.size());
        } else {
            assertEquals("Relations size of: ", 1, ca.size());
        }
        // undo
        if (tx != TX_RB) {
            a.addInB("bs1_1");
            // Check undo
            ca = a.retrieveB();
            assertEquals("After undo, Relations size of: ", 1, ca.size());
        }
        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);
    }


    /**
     * Set a empty relation with 1 element. This added element was'nt already in
     * an other relation. Before: as0 b0 After: as0 -> b0
     */
    public void _testBasicSetOne(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        ce.add("b0");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as0");
        if (tx == TX_CONT) {
            a.assignBInNewTx(ce);
        } else {
            a.assignB(ce);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertTrue("Wrong relations as0 (required:" + ce + ", found:" + ca
                       + ")", isCollectionEqual(ce, ca));
        } else {
            assertEquals("Relations size of: ", 0, ca.size());
        }
        // undo
        if (tx != TX_RB) {
            a.assignB(new ArrayList());
            // Check undo
            ca = a.retrieveB();
            assertEquals("After undo, Relations size of: ", 0, ca.size());
        }
        checkIsInitialState();
    }

    public void testBasicSetOneTxNo() throws Exception {
        _testBasicSetOne(TX_NO);
    }

    public void testBasicSetOneTxCall() throws Exception {
        _testBasicSetOne(TX_CALL);
    }

    public void testBasicSetOneTxCont() throws Exception {
        _testBasicSetOne(TX_CONT);
    }

    public void testBasicSetOneTxRb() throws Exception {
        _testBasicSetOne(TX_RB);
    }


    /**
     * Add an element to a empty relation.
     * Same as _testBasicSetOne except that we add the element in the collection
     * instead of we set directly the collection.
     * Before: as0    bs0
     * After:  as0 -> bs0
     */
    public void _testBasicAddOne(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        ce.add("bs0");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as0");
        if (tx == TX_CONT) {
            a.addInBInNewTx("bs0");
        } else {
            a.addInB("bs0");
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertTrue("Wrong relations as0 (required:" + ce + ", found:" + ca
                       + ")", isCollectionEqual(ce, ca));
        } else {
            assertEquals("Relations size of: ", 0, ca.size());
        }
        // undo
        if (tx != TX_RB) {
            a.assignB(new ArrayList());
            // Check undo
            ca = a.retrieveB();
            assertEquals("After undo, Relations size of: ", 0, ca.size());
        }
        checkIsInitialState();
    }

    public void testBasicAddOneTxNo() throws Exception {
        _testBasicAddOne(TX_NO);
    }

    public void testBasicAddOneTxCall() throws Exception {
        _testBasicAddOne(TX_CALL);
    }

    public void testBasicAddOneTxCont() throws Exception {
        _testBasicAddOne(TX_CONT);
    }

    public void testBasicAddOneTxRb() throws Exception {
        _testBasicAddOne(TX_RB);
    }


    /**
     * Re-Set a relation with N element.
     * Before: as1 -> bs1_1
     *                bs0
     *                bs3
     * After:  as1 -> bs0
     *         as1 -> bs3
     *                bs1_1
     *
     */
    public void _testBasicSetMulti(int tx) throws Exception {
        ArrayList ce = new ArrayList(2);
        ce.add("bs0");
        ce.add("bs3");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as1");
        Collection co = a.retrieveB();
        if (tx == TX_CONT) {
            a.assignBInNewTx(ce);
        } else {
            a.assignB(ce);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertTrue("Wrong relations as1 (required:" + ce + ", found:" + ca
                       + ")", isCollectionEqual(ce, ca));
        } else {
            assertTrue("Wrong relations as1 (required:" + co + ", found:" + ca
                       + ")", isCollectionEqual(co, ca));
        }
        // undo
        if (tx != TX_RB) {
            a.assignB(co);
            // Check undo
            ca = a.retrieveB();
            assertTrue("After undo, wrong relations: ", isCollectionEqual(co, ca));
        }
        checkIsInitialState();
    }

    public void testBasicSetMultiTxNo() throws Exception {
        _testBasicSetMulti(TX_NO);
    }

    public void testBasicSetMultiTxCall() throws Exception {
        _testBasicSetMulti(TX_CALL);
    }

    public void testBasicSetMultiTxCont() throws Exception {
        _testBasicSetMulti(TX_CONT);
    }

    public void testBasicSetMultiTxRb() throws Exception {
        _testBasicSetMulti(TX_RB);
    }


    /**
     * Add an element to a relation already multiple.
     * Before: as1 -> bs1_1
     *                bs0
     * After:  as1 -> bs1_1
     *         as1 -> bs0
     */
    public void _testBasicAddMulti(int tx) throws Exception {
        ArrayList ce = (ArrayList) tbRelationA2B.get("as1");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("as1");
        if (tx == TX_CONT) {
            a.addInBInNewTx("bs0");
        } else {
            a.addInB("bs0");
        }
        ce.add("bs0");
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        if (tx == TX_RB)
            ce.remove("bs0");
        try {
            assertTrue(
                       "Wrong relations as1 (required:" + ce + ", found:" + ca + ")",
                       isCollectionEqual(ce, ca));
        } finally {
            if (tx != TX_RB)
                ce.remove("bs0");
        }
        // undo
        if (tx != TX_RB) {
            a.removeFromB("bs0");
            // Check undo
            ca = a.retrieveB();
            assertTrue("After undo, wrong relations: (required:" + tbRelationA2B.get("as1") + ", found:" + ca
                       + ")", isCollectionEqual((Collection) tbRelationA2B.get("as1"), ca));
        }
        checkIsInitialState();
    }

    public void testBasicAddMultiTxNo() throws Exception {
        _testBasicAddMulti(TX_NO);
    }

    public void testBasicAddMultiTxCall() throws Exception {
        _testBasicAddMulti(TX_CALL);
    }

    public void testBasicAddMultiTxCont() throws Exception {
        _testBasicAddMulti(TX_CONT);
    }

    public void testBasicAddMultiTxRb() throws Exception {
        _testBasicAddMulti(TX_RB);
    }

    /**
     * Remove an element in a relation.
     * Before: a3 -> b3_1
     * Change: a3.retrieveB().remove(b3_1)
     * After:  a3    b3_1
     *
     * See Spec chapter 10.3.7.4, change example: a1.getB().remove(b1n)
     */
    public void _testBasicRemoveInRel(int tx) throws Exception {
        String bRemoved = "b3_1";
        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: ", 0, ca.size());
        } else {
            assertEquals("Wrong relations size for a3: ", 1, ca.size());
        }

        // undo
        if (tx != TX_RB) {
            a.addInB(bRemoved);
        }
        // check to initial state
        checkIsInitialState();

    }

    public void testBasicRemoveInRelTxNo() throws Exception {
        _testBasicRemoveInRel(TX_NO);
    }

    public void testBasicRemoveInRelTxCall() throws Exception {
        _testBasicRemoveInRel(TX_CALL);
    }

    public void testBasicRemoveInRelTxCont() throws Exception {
        _testBasicRemoveInRel(TX_CONT);
    }

    public void testBasicRemoveInRelTxRb() throws Exception {
        _testBasicRemoveInRel(TX_RB);
    }


    /**
     * Remove a bean B which is in a relation.
     * Before: a3 -> b3_1
     * change: B.remove(b3_1)
     * After:  a3
     */
    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");
        }
        String bRemoved = "b3_1";
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        getBHome().remove(bRemoved);
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        ARemote a = getAHome().findByPrimaryKey("a3");
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertEquals("Wrong relations size for a3: ", 0, ca.size());

            // undo
            getBHome().create(bRemoved);
            a.addInBInNewTx(bRemoved);
        } else {
            assertEquals("Wrong relations size for a3: ", 1, ca.size());
        }
        // check to initial state
        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);
    }


    /**
     * Remove a bean B which is in a relation.
     * Same as _testCohRemove except that the rcalled remove method is on the bean
     * instead of the home.
     * Before: a3 -> b3_1
     * change: B.remove(b3_1)
     * After:  a3
     */
    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");
        }
        String bRemoved = "b3_1";
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        BRemote b = getBHome().findByPrimaryKey(bRemoved);
        b.remove();
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        ARemote a = getAHome().findByPrimaryKey("a3");
        Collection ca = a.retrieveB();
        if (tx != TX_RB) {
            assertEquals("Wrong relations size for a3: ", 0, ca.size());

            // undo
            getBHome().create(bRemoved);
            a.addInBInNewTx(bRemoved);
        } else {
            assertEquals("Wrong relations size for a3: ", 1, ca.size());
        }
        // check to initial state
        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);
    }


    /**
     * Set a empty relation with 1 element.
     * This added element was already in a other relation.
     * Before: a0
     *         a3 -> b3_1
     * Change: a0.assignB({b3_1})
     * After:  a0 -> b3_1
     *         a3
     */
    public void _testCohSetOne(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        String bAdded = "b3_1";
        ce.add(bAdded);
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a0");
        if (tx == TX_CONT) {
            a.assignBInNewTx(ce);
        } else {
            a.assignB(ce);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        Collection ca3 = a3.retrieveB();
        if (tx != TX_RB) {
            assertTrue("Wrong relations a0 (required:" + ce + ", found:" + ca
                       + ")", isCollectionEqual(ce, ca));
            assertTrue("Wrong relations for " +  bAdded + ": found:" + ca3 ,
                       !ca3.contains(bAdded));

            // undo
            a.assignB(new ArrayList());
            a3.addInB(bAdded);
            // Check undo
            ca = a.retrieveB();
            assertEquals("After undo, Relations size of: ", 0, ca.size());
        } else {
            assertTrue("Wrong relations for " + bAdded + ": ", a3.containInB(bAdded));
        }
        checkIsInitialState();
    }

    public void testCohSetOneTxNo() throws Exception {
        _testCohSetOne(TX_NO);
    }

    public void testCohSetOneTxCall() throws Exception {
        _testCohSetOne(TX_CALL);
    }

    public void testCohSetOneTxCont() throws Exception {
        _testCohSetOne(TX_CONT);
    }

    public void testCohSetOneTxRb() throws Exception {
        _testCohSetOne(TX_RB);
    }


    /**
     * Add an element to a relation already multiple.
     * The element was already in a relation.
     * Check also the assignement rules for relationships.
     * Before: a1 -> b1_1
     *            -> b1_2
     *            -> b1_3
     *         a3 -> b3_1
     * Change: a1.addInB(b3_1)
     * After:  a1 -> b1_1
     *            -> b1_2
     *            -> b1_3
     *            -> b3_1
     *         a3
     *
     * See Spec chapter 10.3.7.4, change example: a1.getB().add(b2m)
     */
    public void _testCohAddOneInMulti(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        ce.add("b1_1");
        ce.add("b1_2");
        ce.add("b1_3");
        String bAdded = "b3_1";
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a1");
        Collection co = a.retrieveB();
        if (tx == TX_CONT) {
            a.addInBInNewTx(bAdded);
        } else {
            a.addInB(bAdded);
        }
        ce.add(bAdded);
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        if (tx == TX_RB) {
            assertTrue("Wrong relations a1 (required:" + co + ", found:" + ca
                       + ") (Rollback problem)", isCollectionEqual(co, ca));
            assertTrue("Wrong relations for " + bAdded + ": (coherence/rollback problem)", a3.containInB(bAdded));
        } else {
            try {
                assertTrue("Wrong relations: ", isCollectionEqual(ce, ca));
                assertTrue("The added element (" + bAdded
                           + ") is yet in the relation a3", !a3.containInB(bAdded));
            } finally {
                // undo
                a.removeFromB(bAdded);
                a3.addInB(bAdded);
            }
        }
        checkIsInitialState();
    }

    public void testCohAddOneInMultiTxNo() throws Exception {
        _testCohAddOneInMulti(TX_NO);
    }

    public void testCohAddOneInMultiTxCall() throws Exception {
        _testCohAddOneInMulti(TX_CALL);
    }

    public void testCohAddOneInMultiTxCont() throws Exception {
        _testCohAddOneInMulti(TX_CONT);
    }

    public void testCohAddOneInMultiTxRb() throws Exception {
        _testCohAddOneInMulti(TX_RB);
    }

    /**
     * Add elements in an relationship.
     * The relationship was not already empty.
     * One element added had no relation, the other element had a relation
     */
    public void _testCohAddAllInMulti(int tx) throws Exception {
        ArrayList c = new ArrayList();
        c.add("b0");
        c.add("b3_1");
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a1");
        Collection co = a.retrieveB();
        if (tx == TX_CONT) {
            a.addAllInBInNewTx(c);
        } else {
            a.addAllInB(c);
        }
        c.addAll(co);    // c, added collection, becomes the expected collection
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection ca = a.retrieveB();
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        if (tx != TX_RB) {
            assertTrue("Wrong relations: ", isCollectionEqual(c, ca));
            assertTrue("Wrong relations for a3 ", !a3.containInB("b3_1"));
        } else {
            assertTrue("Wrong relations: ", isCollectionEqual(co, ca));
            assertTrue("Wrong relations for a3 ", a3.containInB("b3_1"));
        }
        // undo
        if (tx != TX_RB) {
            a.removeFromB("b0");
            a3.addInB("b3_1");
        }
        // check to initial
        checkIsInitialState();

    }

    public void testCohAddAllInMultiTxNo() throws Exception {
        _testCohAddAllInMulti(TX_NO);
    }

    public void testCohAddAllInMultiTxCall() throws Exception {
        _testCohAddAllInMulti(TX_CALL);
    }

    public void testCohAddAllInMultiTxCont() throws Exception {
        _testCohAddAllInMulti(TX_CONT);
    }

    public void testCohAddAllInMultiTxRb() throws Exception {
        _testCohAddAllInMulti(TX_RB);
    }

    /*
     * Set a relation to multiple.
     * All of element was already in an other relation.
     * Check also the assignement rules for relationships.
     * Before: a1 -> b1_1
     *            -> b1_2
     *            -> b1_3
     *         a2 -> b2_1
     *            -> b2_2
     *            -> b2_3
     * Change: a1.assignB(a2.retrieveB())
     * After:  a1 -> b2_1
     *            -> b2_2
     *            -> b2_3
     *         a2
     *               b1_1
     *               b1_2
     *               b1_3
     *
     * Spec chapter 10.3.7.4, change example: a1.setB(a2.getB())
     */
    public void _testCohSetMulti(int tx) throws Exception {
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a1 = getAHome().findByPrimaryKey("a1");
        Collection co1 = a1.retrieveB();
        ARemote a2 = getAHome().findByPrimaryKey("a2");
        Collection co2 = a2.retrieveB();
        if (tx == TX_CONT) {
            a1.assignBInNewTx(co2);
        } else {
            a1.assignB(co2);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        if (tx != TX_RB) {
            Collection ca1 = a1.retrieveB();
            assertTrue("Wrong relations for a1 (required:" + co2 + ", found:" + ca1 + ")", isCollectionEqual(co2, ca1));
            Collection ca2 = a2.retrieveB();
            assertEquals("Wrong relations size for a2: ", 0, ca2.size());
            // Undo
            a1.assignB(co1);
            a2.assignB(co2);
        }
        checkIsInitialState();
    }

    public void testCohSetMultiTxNo() throws Exception {
        _testCohSetMulti(TX_NO);
    }

    public void testCohSetMultiTxCall() throws Exception {
        _testCohSetMulti(TX_CALL);
    }

    public void testCohSetMultiTxCont() throws Exception {
        _testCohSetMulti(TX_CONT);
    }

    public void testCohSetMultiTxRb() throws Exception {
        _testCohSetMulti(TX_RB);
    }

    /**
     * Set a relation to multiple
     * Some of element was already in this relation, other was in other relation,
     * and other wasn't in any relation.
     * Check also the assignement rules for relationships.
     */
    public void _testCohSetMultiVarious(int tx) throws Exception {
        ArrayList ce = new ArrayList();
        ce.add("b0");
        ce.add("b1_2");
        String b3Added = "b3_1";
        ce.add(b3Added);
        if ((tx == TX_CALL) || (tx == TX_RB)) {
            utx.begin();
        }
        ARemote a = getAHome().findByPrimaryKey("a1");
        if (tx == TX_CONT) {
            a.assignBInNewTx(ce);
        } else {
            a.assignB(ce);
        }
        if (tx == TX_CALL) {
            utx.commit();
        } else if (tx == TX_RB) {
            utx.rollback();
        }
        // checking
        Collection co = new ArrayList(3);
        co.add("b1_1");
        co.add("b1_2");
        co.add("b1_3");
        Collection ca = a.retrieveB();
        ARemote a3 = getAHome().findByPrimaryKey("a3");
        if (tx != TX_RB) {
            assertTrue("Wrong relations: ", isCollectionEqual(ce, ca));
            assertTrue("Wrong relations for " + b3Added + ": ", !a3.containInB(b3Added));

            //Undo
            a.assignB(co);
            a3.addInB(b3Added);
            // Check undo
            ca = a.retrieveB();
            if (!isCollectionEqual(co, ca)) {
                fail("After undo, wrong relations: expected:" + co + ", found:" + ca + ")");
            }
        }
        checkIsInitialState();
    }

    public void testCohSetMultiVariousTxNo() throws Exception {
        _testCohSetMultiVarious(TX_NO);
    }

    public void testCohSetMultiVariousTxCall() throws Exception {
        _testCohSetMultiVarious(TX_CALL);
    }

    public void testCohSetMultiVariousTxCont() throws Exception {
        _testCohSetMultiVarious(TX_CONT);
    }

    public void testCohSetMultiVariousTxRb() throws Exception {
        _testCohSetMultiVarious(TX_RB);
    }



    /**
     * Test that a muti-valued cmr field is initialized to an empty collection after its creation.
     * See spec. 2.0, & 10.5.5 page 171
     */
    public void _testCmrMultiIsInit(int tx) throws Exception {
        if (tx == TX_CONT) {
            // The transaction attribute of the create 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_RB) {
            fail("Transaction attribute 'TX_RB' has no sense for this test");
        }
        if (tx == TX_CALL) {
            utx.begin();
        }
        ARemote a = getAHome().create("aX");
        if (tx == TX_CALL) {
            utx.commit();
        }
        // checking
        Collection cb = a.retrieveB();
        assertEquals("The multi-valued cmr field 'b' not initialized with an empty collection",
                     0, cb.size());
        if (tx != TX_RB) {
            // undo
            a.remove();
        }
        // check to initial state
        checkIsInitialState();
    }

    public void testCmrMultiIsInitTxNo() throws Exception {
        _testCmrMultiIsInit(TX_NO);
    }

    public void testCmrMultiIsInitTxCall() throws Exception {
        _testCmrMultiIsInit(TX_CALL);
    }

    public void testRepeatedAssign() throws Exception {
        Collection co = new ArrayList(3);
        co.add("b1_1");
        co.add("b1_2");
        co.add("b1_3");
        ArrayList ce = new ArrayList();
        ce.add("b0");
        ce.add("b1_2");
        String b3Added = "b3_1";
        ce.add(b3Added);

        ARemote a = getAHome().findByPrimaryKey("a1");
        ARemote a3 = getAHome().findByPrimaryKey("a3");

        utx.begin();
        a.assignB(ce);
        utx.commit();
        a.assignB(co);
        sync(false);
        a.assignB(ce);

        // undo
        a.assignB(co);
        a3.addInB(b3Added);
        sync(false);
        checkIsInitialState();
    }

}
