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

import java.rmi.RemoteException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.RollbackException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.stests.util.JTestCase;


public class A_manac extends JTestCase {

    static final int maxAccounts = 20;
    static final int initialValue = 1000;
    protected static ManagerHome home = null;
    protected static Manager manager = null;
    protected static boolean initialized = false;

    public static boolean threadfail;

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

    protected void setUp() throws Exception {
	super.setUp();
	if (home == null) {
	    useBeans("manac");
	    home = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup("manacManagerHome"), ManagerHome.class);
	}
	if (manager == null) {
	    manager = home.create(initialValue);
	}
	if (!initialized) {
	    manager.createAll(maxAccounts);
	    initialized = true;
	}
	threadfail = false;
    }

    protected void tearDown() throws Exception {
	super.tearDown();
	if (threadfail) {
	    initialized = false;
	}
	if (manager != null) {
	    manager.reinitAll();
	    manager.remove();
	    manager = null;
	}
    }

    /**
     * Run a multithreaded client test
     */
    public void manac(int accounts, int delay, int loops, int amount, int threads) throws Exception {

	// Create and start all threads
	A_thread[] t_thr = new A_thread[threads];
	for (int i = 0; i < threads; i++) {
	    t_thr[i] = new A_thread(i, ictx, accounts, delay, loops, amount);
	    t_thr[i].start();
	}

	// Wait end of all threads
	for (int p = 0; p < threads; p++) {
	    t_thr[p].join();
	}

	// Check if all threads run ok
	if (threadfail) {
	    fail("Error in a thread");
	}

	// Check Accounts after each test
	if (manager.checkAll() == false) {
	    fail("bad accounts");
	}
    }
}
