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

import java.rmi.RemoteException;

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

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

import org.objectweb.jonas.jtests.beans.worker.Worker;
import org.objectweb.jonas.jtests.beans.worker.WorkerHome;
import org.objectweb.jonas.jtests.util.JTestCase;


public class F_Worker extends JTestCase {

    private static String BEAN_HOME = "workerHome";
    private static WorkerHome home = null;

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

    public WorkerHome getHome() {
        if (home == null) {
            try {
                home = (WorkerHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME), WorkerHome.class);
            } catch (NamingException e) {
                fail("Cannot get bean home");
            }
        }
        return home;
    }

    protected void setUp() {
        super.setUp();
        useBeans("worker", false);
    }

    public void testDoWorkTx() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.doWorkInTx();
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testStartWorkTx() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.startWorkInTx();
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testScheduleWorkTx() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.scheduleWorkInTx();
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testDoWorkBasic() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.doWorks(count);
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testDo20Works() throws Exception {
        Worker w = getHome().create();
        int count = 20;
        w.doWorks(count);
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testStartWorkBasic() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.startWorks(count);
        sleep(500);
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testStart20Works() throws Exception {
        Worker w = getHome().create();
        int count = 20;
        w.startWorks(count);
        sleep(2000);
        assertEquals("count", count, w.getwcount());
        w.remove();
    }

    public void testScheduleWorkBasic() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.scheduleWorks(count, 20000);
        sleep(500);
        assertEquals("count", count, w.getwcount());
        assertEquals("accepted", count, w.getNotifyAccepted());
        assertEquals("started", count, w.getNotifyStarted());
        assertEquals("rejected", 0, w.getNotifyRejected());
        assertEquals("completed", count, w.getNotifyCompleted());
        w.remove();
    }

    public void testScheduleWorkTimedout() throws Exception {
        Worker w = getHome().create();
        int count = 1;
        w.scheduleWorks(count, 1L);
        sleep(500);
        assertEquals("count", 0, w.getwcount());
        assertEquals("accepted", count, w.getNotifyAccepted());
        assertEquals("started", 0, w.getNotifyStarted());
        assertEquals("rejected", count, w.getNotifyRejected());
        assertEquals("completed", 0, w.getNotifyCompleted());
        w.remove();
    }

    public void testSchedule20Works() throws Exception {
        Worker w = getHome().create();
        int count = 20;
        w.scheduleWorks(count, 20000);
        sleep(2000);
        assertEquals("count", count, w.getwcount());
        assertEquals("accepted", count, w.getNotifyAccepted());
        assertEquals("started", count, w.getNotifyStarted());
        assertEquals("rejected", 0, w.getNotifyRejected());
        assertEquals("completed", count, w.getNotifyCompleted());
        w.remove();
    }

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

