/*
 * 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.Collection;
import java.util.Iterator;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mou.ARemote;
import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote;
import org.objectweb.jonas.jtests.beans.relation.mou.BRemote;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * For testing many-to-one unidirectional relationships
 * with relations created in the ejbPostCreate, as the EJB spec recommands it.
 * @author Philippe Durieux
 **/
public class F_Relation2_mouEC2 extends JTestCase {

    private static String BEAN_HOME_A = "relation_mou_A1Home";
    protected static AHomeRemote ahome = null;
    private static String BEAN_HOME_B = "relation_mou_B1Home";
    protected static BHomeRemote bhome = null;

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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        if (!isInit) {
            useBeans("mou", true);
            try {
                ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A),
                                                                  AHomeRemote.class);
                bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B),
                                                                  BHomeRemote.class);
            } catch (NamingException e) {
                fail("Cannot get bean home: " + e.getMessage());
            }
            isInit = true;
        }
    }

    /**
     * teardown is called after each test case.
     * Notes for debugging :
     * To see DataBase state after a test is passed :
     * Replace the cleanall() by sync(false).
     */
    protected void tearDown() throws Exception {
        cleanall();
    }

    private void cleanall() throws Exception {
        debug("remove all beans");

        // Start the Transaction. In case a Transaction is still running,
        // roll it back first and start a new one.
        try {
            utx.begin();
        } catch (Exception e) {
            debug("rollback first and start a new tx");
            utx.rollback();
            utx.begin();
        }
        // In case there is a problem during remove, we must unload
        // the bean to restart properly.
        try {
            Collection c = ahome.findAll();
            for (Iterator i = c.iterator(); i.hasNext(); ) {
                ARemote a = (ARemote) PortableRemoteObject.narrow(i.next(), ARemote.class);
                a.remove();
            }
            c = bhome.findAll();
            for (Iterator i = c.iterator(); i.hasNext(); ) {
                BRemote b = (BRemote) PortableRemoteObject.narrow(i.next(), BRemote.class);
                b.remove();
            }
            utx.commit();
        } catch (Exception e) {
            error("Exception in cleanup: " + e);
            try {
                utx.rollback();
            } finally {
                unloadBeans("mou");
                isInit = false;
            }
        }
    }

    public void testCreateB() throws Exception {
        bhome.create("b0");
        assertTrue(null != bhome.findByPrimaryKey("b0"));
    }

    public void testCreateBA() throws Exception {
        bhome.create("b0");
        ahome.create("a0", "b0");
        assertEquals("b0", ahome.findByPrimaryKey("a0").retrieveB());
    }

    public static Test suite() {
        return new TestSuite(F_Relation2_mouEC2.class);
    }

    public static void main(String args[]) {
        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            Integer i_arg;
            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_Relation2_mouEC2(testtorun));
        }
    }

}
