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

import junit.framework.Test;
import junit.framework.TestSuite;
import javax.rmi.PortableRemoteObject;
import org.objectweb.jonas.stests.util.JTestCase;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Set of test with only 1 thread at a time.
 */
public class F_appli extends JTestCase {

    protected static OrderEntryClerkHome home = null;
 
    protected static String oecName = "OrderEntryClerkHome";

    int nbThreads = 2;
    int nbPlaceOrders = 1;
    int nbOrderline = 3;

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

    protected void setUp() throws Exception {
        super.setUp();
        if (home == null) {
            useBeans("appli");
            home = (OrderEntryClerkHome) PortableRemoteObject.narrow(ictx.lookup(oecName),
                                                             OrderEntryClerkHome.class);
        }
 
    }

    protected void tearDown() throws Exception {
        super.tearDown();
  
    }

    /*
     * Test used for populating tables
     */
    public void testCreateAllTables() throws Exception {
           OrderEntryClerk oecbean = null;
           oecbean = home.create();
           oecbean.createAllTables();
    }
               
    public void test2placeOrder() throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        OrderEntryClerk oecbean = null;
        oecbean = home.create();
        for (int i = 0; i<100;i++) {
            System.out.print(">>>>> ");
            String  readLine = in.readLine();
            oecbean.setCustomer(new Integer(9));
            oecbean.addOrderLine(new Integer(20),24);
            /**/
            oecbean.addOrderLine(new Integer(10),12);
            oecbean.addOrderLine(new Integer(1),1);
            oecbean.addOrderLine(new Integer(2),6);
            /**/
            String numorder = oecbean.placeOrder();
            /**/
            oecbean.setCustomer(new Integer(5));
            oecbean.addOrderLine(new Integer(23),2);
            oecbean.addOrderLine(new Integer(13),1);
            oecbean.addOrderLine(new Integer(11),7);
            oecbean.addOrderLine(new Integer(22),8);
            numorder = oecbean.placeOrder();
            /**/
        }
    }

    public void test5placeOrder() throws Exception {
        OrderEntryClerk oecbean = null;
        oecbean = home.create();
        oecbean.setCustomer(new Integer(2));
        oecbean.addOrderLine(new Integer(1),24);
        oecbean.addOrderLine(new Integer(2),12);
        oecbean.addOrderLine(new Integer(3),1);
        oecbean.addOrderLine(new Integer(4),6);
        String numorder = oecbean.placeOrder();
        oecbean.setCustomer(new Integer(3));
        oecbean.addOrderLine(new Integer(5),4);
        oecbean.addOrderLine(new Integer(6),2);
        numorder = oecbean.placeOrder();
        oecbean.setCustomer(new Integer(4));
        oecbean.addOrderLine(new Integer(7),4);
        oecbean.addOrderLine(new Integer(8),2);
        oecbean.addOrderLine(new Integer(9),4);
        oecbean.addOrderLine(new Integer(10),2);
        oecbean.addOrderLine(new Integer(9),4);
        oecbean.addOrderLine(new Integer(10),2);
        numorder = oecbean.placeOrder();
        oecbean.setCustomer(new Integer(5));
        oecbean.addOrderLine(new Integer(15),4);
        oecbean.addOrderLine(new Integer(16),2);
        oecbean.addOrderLine(new Integer(3),5);
        numorder = oecbean.placeOrder();
        oecbean.setCustomer(new Integer(6));
        oecbean.addOrderLine(new Integer(5),4);
        oecbean.addOrderLine(new Integer(26),2);
        oecbean.addOrderLine(new Integer(23),5);
        oecbean.addOrderLine(new Integer(5),4);
        oecbean.addOrderLine(new Integer(26),2);
        oecbean.addOrderLine(new Integer(23),5);
        numorder = oecbean.placeOrder();

    }

    public void test_2Threads_1Orders_3Orderlines() throws Exception {
        applimulti(home, nbThreads, nbPlaceOrders, nbOrderline);
    }

    public void test_4Threads_5Orders_4Orderlines() throws Exception {
        int nbThreads = 4;
        int nbPlaceOrders = 5;
        int nbOrderline = 4;
        applimulti(home, nbThreads, nbPlaceOrders, nbOrderline);
    }

    public void test_10Threads_6Orders_10Orderlines() throws Exception {
        int nbThreads = 10;
        int nbPlaceOrders = 6;
        int nbOrderline = 10;
        applimulti(home, nbThreads, nbPlaceOrders, nbOrderline);
    }

    /**
     * Run a multithreaded client test. Common part to all test multi thread cases.
     */
    public void applimulti( OrderEntryClerkHome home, 
                            int threads, 
                            int nbOrders, 
                            int nbLines) 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(home, i, nbOrders, nbLines);
            t_thr[i].start();
        }

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

    
    }

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

    public static void main (String args[]) {
        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_appli(testtorun));
        }
    }

}
