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

import javax.rmi.PortableRemoteObject;
import org.objectweb.jonas.stests.util.JTestCase;


public abstract class A_bank extends JTestCase {

    protected static final int OP_READ = 1;
    protected static final int OP_MOVE = 2;
    protected static final int OP_CREATE = 3;
    protected static final int OP_REMOVE = 4;
    protected static final int OP_ONEMOVE = 5;
    protected static final int OP_READTX = 6;
    protected static final int OP_ONEMOVETX = 7;
    protected static final int OP_MOVETO = 8;

    static final int initialValue = 1000;
    static final int accountNb = 20;

    protected static ManagerHome home = null;
    protected static Manager manager = null;
    public static boolean threadfail;

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

    public abstract String getManagerHomeName();
    public abstract boolean getPrefetch();

    protected void setUp() throws Exception {
        super.setUp();
        if (home == null) {
            useBeans("bank");
            home = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(getManagerHomeName()), ManagerHome.class);
        }
        if (manager == null) {
            manager = home.create(initialValue, getPrefetch());
        }
        // Initializes the test by creating accounts
        manager.createAll(accountNb);
        threadfail = false;
    }

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

    public void ope(int action, int accmin, int accmax, int loops, int threads) throws Exception {
        ope(action, accmin, accmax, loops, threads, 10, true);
    }

    public void ope(int action, int accmin, int accmax, int loops, int threads, int amount) throws Exception {
        ope(action, accmin, accmax, loops, threads, amount, true);
    }

    /**
     * generic operation.
     * @param accmin min range for account nb
     * @param accmax max range for account nb
     * @param loops nb of loop in each thread
     * @param threads nb of threads running the same operations
     */
    public void ope(int action, int accmin, int accmax, int loops, int threads, int amount, boolean verify) throws Exception {

        // Set transaction timeout in the server (global value!)
        stopTxAt(20);

        // 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(getManagerHomeName(), i, ictx, action, accmin, accmax, loops, amount, getPrefetch());
            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");
        }
        if (verify && !manager.checkAll()) {
            threadfail = true;  // to reinit the database
            fail("Bad global state");
        }
    }

    public void testBasicRead() throws Exception {
        ope(OP_READ, 0, 9, 1, 1, 10, false);
    }

    public void testPrefetch() throws Exception {
        ope(OP_READ, 0, 9, 50, 8);
    }

    public void testDeadlock() throws Exception {
        ope(OP_MOVETO, 3, 5, 1, 2, 20);
    }

    public void testSameAccount() throws Exception {
        ope(OP_MOVETO, 2, 2, 10, 1, 10);
    }

    public void testMultiSameAccount() throws Exception {
        ope(OP_MOVETO, 6, 6, 1, 10, 10);
    }

    public void testBasicReadTx() throws Exception {
        ope(OP_READTX, 0, 9, 1, 1, 10, false);
    }

    public void testLongRead() throws Exception {
        ope(OP_READ, 0, 9, 100, 1);
    }

    public void testLongReadTx() throws Exception {
        ope(OP_READTX, 0, 9, 100, 1);
    }

    public void testMultiRead() throws Exception {
        ope(OP_READ, 0, 9, 1, 10);
    }

    public void testMultiReadTx() throws Exception {
        ope(OP_READTX, 0, 9, 1, 10);
    }


    public void testMultiShortReadTx() throws Exception {
        ope(OP_READTX, 0, 9, 10, 10);
    }

    public void testMultiOneMove() throws Exception {
        ope(OP_ONEMOVE, 0, 5, 1, 20);
    }

    public void testTh3OneMoveTx() throws Exception {
        ope(OP_ONEMOVETX, 0, 5, 100, 3);
    }

    public void testTh5OneMoveTx() throws Exception {
        ope(OP_ONEMOVETX, 0, 5, 100, 5);
    }

    public void testTh7OneMoveTx() throws Exception {
        ope(OP_ONEMOVETX, 0, 5, 100, 7);
    }

    public void testMultiOneMoveTx() throws Exception {
        ope(OP_ONEMOVETX, 0, 5, 1, 20);
    }

    /**
     * This test is also used for perf measures
     * => DO NOT CHANGE ARG VALUES
     */
    public void testMultiLongRead() throws Exception {
        ope(OP_READ, 0, 9, 200, 12);
    }

    /**
     * This test is also used for perf measures
     * => DO NOT CHANGE ARG VALUES
     */
    public void testMultiLongReadTx() throws Exception {
        ope(OP_READTX, 0, 19, 200, 12, 1);
    }

    /**
     * This test is also used for perf measures
     * => DO NOT CHANGE ARG VALUES
     */
    public void testMultiLongOneMove() throws Exception {
        ope(OP_ONEMOVE, 0, 19, 200, 12, 1);
    }

    /**
     * This test is also used for perf measures
     * => DO NOT CHANGE ARG VALUES
     */
    public void testMultiLongOneMoveTx() throws Exception {
        ope(OP_ONEMOVETX, 0, 19, 200, 12, 1);
    }

}
