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

import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.beanexc.AccountEHome;

public class F_CatcherEC2 extends A_CatcherEntity {

    private static String BEAN_HOME = "beanexcAccountEC2Home";
    protected static AccountEHome home = null;

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

    public AccountEHome getHome() {
	if (home == null) {
	    try {
		home = (AccountEHome) PortableRemoteObject.narrow(
					     ictx.lookup(BEAN_HOME),
					     AccountEHome.class);
	    } catch (NamingException e) {
		fail("Cannot get bean home");
	    }
        }

        return home;
    }

    protected void setUp() {
        super.setUp();
        try {
            getHome().findByNoAccount(101);
        } catch (FinderException fe) {
	    // init here tables if CMP2
	    try {
                utx.begin();
		home.create(101, "Antoine de St Exupery", 900);
		home.create(102, "Alexandre Dumas fils", 500);
		home.create(103, "Conan Doyle", 800);
		home.create(104, "Alfred de Musset", 140);
		home.create(105, "Phileas Lebegue", 235);
		home.create(81, "Philippe Coq", 20);
		home.create(82, "Philippe Durieux", 20);
		home.create(83, "Helene Joanin", 20);
		home.create(84, "Adriana Danes", 20);
		home.create(85, "Francois Exertier", 20);
		home.create(86, "John Doo", 20);
		home.create(87, "Jim", 20);
		home.create(999990, "For exception in ejbRemove", 20);
		home.create(999991, "For exception in ejbRemove", 20);
		home.create(999992, "For exception in ejbRemove", 20);
		home.create(999993, "For exception in ejbRemove", 20);
                utx.commit();
	    } catch (Exception i) {
                throw new RuntimeException(i);
	    }
	} catch (RemoteException re) {
            throw new RuntimeException(re);
        }
    }

    /**
     * This suite is all CMP version 2 test cases
     */
    public static Test suite() {
	return new TestSuite(F_CatcherEC2.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_CatcherEC2(testtorun));
	}
    }
}
