/*
 * 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.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.EJBObject;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.relation.remon.Main;
import org.objectweb.jonas.jtests.beans.relation.remon.MainHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This test suite tests specifics mapping used in some applications
 * for example, cmr field and cmp field mapped on the same table column
 * @author Philippe Durieux
 */
public class F_Remon extends JTestCase {

    /**
     * Reference on the remote Home's
     */
    private static MainHome mainhome = null;

    /**
     * Standard constructor
     */
    public F_Remon(String name) {
        super(name);
    }

    protected boolean isInit = false;

    /**
     * setup is called before each test case.
     * If not initialized, load the remon bean and lookup the Home.
     */
    protected void setUp() {
        super.setUp();
        if (!isInit) {
            useBeans("remon", false);
            try {
                mainhome = (MainHome) PortableRemoteObject.narrow(ictx.lookup("relation_remon_MainHome"),
                                                                  MainHome.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();
        //sync(false);
    }

    /**
     * Cleanup all the remon after each test, to make tests independant.
     * Use transaction to avoid known bugs during cleanup.
     * (With no transaction -> 1 remove will fail)
     */
    private void cleanall() throws Exception {
        debug("remove all beans");

        // In case there is a problem during remove, we must unload
        // the bean to restart properly.
        try {
            cleanAllBeans(mainhome);
        } catch (Exception e) {
            error("Exception in cleanup: " + e);
            unloadBeans("remon");
            isInit = false;
        }
    }
        
    private void cleanAllBeans(MainHome home) throws RemoteException, RemoveException {
        try {
            Collection c = home.findAll();
            for (Iterator i = c.iterator(); i.hasNext(); ) {
                EJBObject p = (EJBObject) i.next();
                p.remove();
            }
        } catch (FinderException e) {
        }
    }

    // ---------------------------------------------------------------
    // Set of the tests that are passed automatically.
    // ---------------------------------------------------------------

    public void testCreateMain() throws Exception {
        mainhome.create("A1", "100", "at1");
    }

    /**
     * Run all the tests of this suite.
     */
    public static Test suite() {
        return new TestSuite(F_Remon.class);
    }

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

}
