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

import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.util.JTestCase;
import org.objectweb.jonas.jtests.beans.message.Sender;
import org.objectweb.jonas.jtests.beans.message.SenderHome;
import org.objectweb.jonas.jtests.beans.message.Sender1_2;
import org.objectweb.jonas.jtests.beans.message.Sender1_2Home;

public class F_TestsWithBMT extends JTestCase {


    private static String BEAN_HOME = "messageSender1_2SFHome";

    protected static Sender1_2Home home = null;

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


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

    /**
     * init environment:
     * - load beans
     * - create/init database for entities.
     */
    protected void setUp() {
        super.setUp();
        useBeans("message", true);
    }

    // --------------------------------------------------------
    // Basic Tests on Topics
    // --------------------------------------------------------

    /**
     * test: Send 1 message on a topic inside a transaction that commits
     * the message must be received
     * transaction is demarcated explicitly in a BMT session bean
     * the begin transaction occurs before the create_session
     * 2 MDB are reading the topic.
     * 
     */
    public void testSendOnTopic1_1() throws Exception {
        Sender1_2 s = getHome().create();
        int val = 200;
        s.sendOnDestinationWithTxBeforeSession("JunitTopic1", val, 1, true);
        assertEquals(2, s.check(val, 2, 4));
        s.remove();
    }

    /**
     * test: Send 1 message on a topic inside a transaction that rollback
     * no message must be received
     * transaction is demarcated explicitly in a BMT session bean
     * the begin transaction occurs before the create_session
     * 2 MDB are reading the topic.
     * 
     */
    public void testSendOnTopic1_2() throws Exception {
        Sender1_2 s = getHome().create();
        int val = 200;
        s.sendOnDestinationWithTxBeforeSession("JunitTopic1", val, 1, false);
        assertEquals(0, s.check(val, 2, 4));
        s.remove();
    }

   /**
     * test: Send 1 message on a topic inside a transaction that commits
     * the message must be received
     * transaction is demarcated explicitly in a BMT session bean
     * the begin transaction occurs after the create_session
     * 2 MDB are reading the topic.
     * 
     */
    public void testSendOnTopic1_3() throws Exception {
        Sender1_2 s = getHome().create();
        int val = 200;
        s.sendOnDestinationWithTxAfterSession("JunitTopic1", val, 1, true);
        assertEquals(2, s.check(val, 2, 4));
        s.remove();
    }

    /**
     * test: Send 1 message on a topic inside a transaction that roll back
     * no message must be received
     * transaction is demarcated explicitly in a BMT session bean
     * the begin transaction occurs after the create_session
     * 2 MDB are reading the topic.
     * 
     * reproduces the bug #300587
     */
    public void testSendOnTopic1_4() throws Exception {
        Sender1_2 s = getHome().create();
        int val = 200;
        s.sendOnDestinationWithTxAfterSession("JunitTopic1", val, 1, false);
        assertEquals(0, s.check(val, 2, 4));
        s.remove();
    }


 

    /**
     * Run all the tests
     */
    public static Test suite() {
        return new TestSuite(F_TestsWithBMT.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_TestsWithBMT(testtorun));
        }
    }
}
