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


import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.sequence.WoSes;
import org.objectweb.jonas.jtests.beans.sequence.WoSesHome;
import org.objectweb.jonas.jtests.beans.sequence.SequenceSesHome;
import org.objectweb.jonas.jtests.util.JTestCase;



/**
 * Test for reproducing the Bug  #303488
 * @author Philippe Coq
 */

/**
 *  Thread that does the work
 */
class B_thread extends Thread {
    WoSesHome home;
    int loops;

    public B_thread(WoSesHome home, int loops) {
        this.home = home;
        this.loops = loops;
    }

    public void run() {
        try {
            WoSes wos = home.create();
            for (int i = 0; i < loops; i++) {
                wos.schedule(Thread.currentThread().getName(),i);
            }
            wos.remove();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

public class F_Sequence extends JTestCase {

    protected static WoSesHome woseshome = null;


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

    protected void setUp() {
        super.setUp();
        if (woseshome == null) {
            // Create table with sql (not a create) because we don't want
            // that an instance is mapped in memory before doing the test.
            useBeans("sequence", true);
            try {
                woseshome= (WoSesHome ) PortableRemoteObject.narrow(ictx.lookup("WoSesHome"), WoSesHome.class);
                assertNotNull("home of clusterId_1 is null", woseshome);
            } catch (NamingException e) {
                fail("Cannot get bean home: " + e.getMessage());
            }
        }
    }

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

    /**
     * Several threads run a findByPrimreyKey on the same element in the
     * table jt2_Sequence and modify the next Number field
     * so if the bug isn't fixed we got duplicate key exception (constraints violation)
     *
     */
    public void multiThreadTest(int threads, int loops) throws Exception {

        // Create and start all threads
        B_thread[] t_thr = new B_thread[threads];
        for (int i = 0; i < threads; i++) {
            t_thr[i] = new B_thread(woseshome, loops);
        }
        for (int i = 0; i < threads; i++) {
            t_thr[i].start();
        }
        // Wait end of all threads
        for (int p = 0; p < threads; p++) {
            t_thr[p].join();
        }

    }

    public void testTh20L10() throws Exception {
        multiThreadTest(20, 10);
    }

    public void testTh2L100() throws Exception {
        multiThreadTest(2, 100);
    }

    public void testTh3L1() throws Exception {
        multiThreadTest(3, 1);
    }


    public void testTh5L10() throws Exception {
        multiThreadTest(5, 10);
    }

    public void testTh1L2() throws Exception {
        multiThreadTest(1, 2);
    }


    public void testTh10L10() throws Exception {
        multiThreadTest(10, 10);
    }



    public void testTh50L1() throws Exception {
        multiThreadTest(50, 1);
    }


    public static Test suite() {
        return new TestSuite(F_Sequence.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_Sequence(testtorun));
        }
    }
}
