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

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

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

import org.objectweb.jonas.jtests.beans.message.Sender1_1;
import org.objectweb.jonas.jtests.beans.message.Sender1_1Home;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * test TimerService on Message Driven Beans
 */
public class F_TimerMD extends JTestCase {

    // Uses JMS 1.1 (std API for J2EE 1.4)
    private static String BEAN_HOME = "messageSender1_1SFHome";
    protected static Sender1_1Home home = null;

    // Must be the same values than in org.objectweb.jonas.jtests.beans.message.Listener
    public static final int CREATE_TIMER_MIN = 1500;
    public static final int CREATE_TIMER_MAX = 1600;
    public static final int CREATE_P_TIMER_MIN = 25000;
    public static final int CREATE_P_TIMER_MAX = 26000;

    /**
     * constructor
     * @param name name of the test suite.
     */
    public F_TimerMD(String name) {
        super(name);
    }

    /**
     * Sets up the fixture, here load the beans if not loaded yet.
     * This method is called before a test is executed.
     */
    protected void setUp() {
        super.setUp();
        useBeans("message", true);
    }


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

    /**
     * Basic test: Send 1 message on a topic
     * 2 MDB are reading the topic.
     * No tx.
     */
    public void testBasicSendOnTopic1() throws Exception {
        Sender1_1 s = getHome().create();
        int val = 200;
        s.sendOnDestination("JunitTopic1", val, 1);
        assertEquals(2, s.check(val, 2, 4));
        s.remove();
    }

    /**
     * Start a timer on Topic MDB
     */
    public void testTimerTopic1() throws Exception {
        Sender1_1 s = getHome().create();
        int val = CREATE_TIMER_MIN + 4;
        s.sendOnDestination("JunitTopic1", val, 1);
        assertEquals(2, s.check(val, 2, 5));
        s.remove();
    }

    /**
     * Start a timer on Queue MDB
     */
    public void testTimerQueue1() throws Exception {
        Sender1_1 s = getHome().create();
        int val = CREATE_TIMER_MIN + 5;
        s.sendOnDestination("JunitQueue1", val, 1);
        assertEquals(1, s.check(val, 1, 6));
        s.remove();
    }

    /**
     * Start a persistent timer on Queue MDB, with info
     * containing a PK.
     */
    public void testPersistTimerQueue() throws Exception {
        Sender1_1 s = getHome().create();
        for (int i = 0; i < 10; i++) {
            int val = CREATE_P_TIMER_MIN + i;
            s.sendOnDestination("JunitQueue1", val, 1);
        }
    }

    /**
     * Start several timers on Queue MDB
     */
    public void testTimersQueue1() throws Exception {
        Sender1_1 s = getHome().create();
        int val = CREATE_TIMER_MIN + 5;
        s.sendOnDestination("JunitQueue1", val, 5);
        assertEquals(5, s.check(val, 5, 7));
        s.remove();
    }

    /**
     * This suite is all Message Driven Timer test cases
     */
    public static Test suite() {
        return new TestSuite(F_TimerMD.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_TimerMD(testtorun));
        }
    }
}
