/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2009 Bull S.A.S
 * 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 1any 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.ow2.jonas.cluster.test.javaee5;

import junit.framework.TestSuite;

import org.ow2.jonas.cluster.test.common.AbsTestClient;
import org.ow2.jonas.cluster.test.common.ClientTask;
import org.ow2.jonas.cluster.test.common.RunnableTask;
/**
 *
 * @author eyindanga
 *
 */
public class ClientTest extends AbsTestClient {

    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }

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

    public ClientTest(String s) {
        super(s);
    }

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

    /**
     * Test Exception.
     * @throws Exception any.
     */
    public void testException() throws Exception {
        LOGGER.info("cluster-javaee5.testException");
        RunnableTask task = null;
        for(int i = 0; i < clientsNumber; i++) {
            task =  new ClientTask("cluster-javaee5.testException_" + i, "org.ow2.jonas.cluster.test.javaee5.client.ClientException",
                    outputDir, "cluster-javaee5.testException_" + i + ".txt");
            worker.addTask(task);
        }
        System.out.println("running the worker");
        worker.run();
        /**start the checker **/
        checker.start();
        while(!checker.stopped()) {
            // sleep.
            Thread.currentThread().sleep(checkerSleepTime);
        }
        System.out.println("Worker has terminated");
        if(worker.hasFailure()) {
            fail("on cluster-javaee5.testException" + worker.getTasksFailures());
        }
        worker.clear();
        LOGGER.info("cluster-javaee5.testException is ok");
    }
    /**
     * Test Fail over of Stateful session Bean.
     * @throws Exception any.
     */
    public void testFOSFSB() throws Exception {
        LOGGER.info("cluster-javaee5.testFOSFSB");
        RunnableTask task = null;
        for(int i = 0; i < clientsNumber; i++) {
            task = new ClientTask("cluster-javaee5.testFOSFSB_" + i, "org.ow2.jonas.cluster.test.javaee5.client.ClientFOSFSB",
                    outputDir, "cluster-javaee5.testFOSFSB_" + i + ".txt");
            worker.addTask(task);
        }
        worker.run();
        /**start the ckecker **/
        checker.start();
        while(!checker.stopped()) {
            // sleep.
            Thread.currentThread().sleep(checkerSleepTime);
        }
        if(worker.hasFailure()) {
            fail("on cluster-javaee5.testFOSFSB" + worker.getTasksFailures());
        }
        worker.clear();
        LOGGER.info("cluster-javaee5.testFOSFSB is ok");
    }

    /**
     * Test Load Balancing
     * @throws Exception any.
     */
    public void testLBRemote() throws Exception {
         LOGGER.info("cluster-javaee5.testLBRemote");
         RunnableTask task = null;
         for(int i = 0; i < clientsNumber; i++) {
             task = new ClientTask("cluster-javaee5.testLBRemote_" + i, "org.ow2.jonas.cluster.test.javaee5.client.ClientLBRemote",
                     outputDir, "cluster-javaee5.testLBRemote_" + i + ".txt");
             worker.addTask(task);
         }
         worker.run();
         /**start the ckecker **/
         checker.start();
         while(!checker.stopped()) {
             // sleep.
             Thread.currentThread().sleep(checkerSleepTime);
         }
         if(worker.hasFailure()) {
             fail("on cluster-javaee5.testLBRemote" + worker.getTasksFailures());
         }
         worker.clear();
         LOGGER.info("cluster-javaee5.testLBRemote is ok.");
    }

}
