/*
 * 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.rmi.PortableRemoteObject;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.bank.Manager;
import org.objectweb.jonas.jtests.beans.bank.ManagerHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * Test a session bean remote accessing CMP2 entities local and 
 * direct jdbc on another datasource, all inside a same transaction.
 * @author Philippe Durieux
 */
public class F_mixJdbcEJB extends JTestCase {

    protected static ManagerHome mgrhome = null;
    protected static Manager mgr = null;
    protected static int accountNb = 10;

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

    protected void setUp() {
        super.setUp();
        try {
            if (mgrhome == null) {
                // true = create tables (for CMP1 bean)
                useBeans("bank", true);
                mgrhome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(getManagerHomeName()), ManagerHome.class);
            }
            if (mgr == null) {
                mgr = mgrhome.create(1000, false);
            }
            // Initializes the test by creating accounts
            mgr.createAll(accountNb);
        } catch (Exception e) {
            fail("Exception raised in setup: " + e);
        }
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        if (mgr != null) {
            try {
                try {
                    mgr.reinitAll();
                } finally {
                    mgr.remove();
                }
            } finally {
                mgr = null;
            }
        }
    }

    public void testMixOpeRB() throws Exception {
        int ac1 = 1;
        int ac2 = 2;
        // first get initial account balances
        int b1 = mgr.readBalanceTx(ac1);
        int b2 = mgr.readBalanceTx(ac2);
        // Get num value for Product
        int num = mgr.readProdNum();
        // call mixed operation, that should be rolled back
        int newb1 = b1 + 1;
        mgr.mixOpe(ac1, ac2, newb1, false);
        // Check that ac1 has been changed (because RequiresNew)
        Assert.assertEquals("RequiresNew should commit", newb1, mgr.readBalanceTx(ac1));
        // Check that ac2 has not been changed (because rollback)
        Assert.assertEquals("Rollback did not occur on ac2", b2, mgr.readBalanceTx(ac2));
        // Check that Product has not been changed
        Assert.assertEquals("Rollback did not occur on product", num, mgr.readProdNum());
        // Reset ac1 to his value
        mgr.setBalance(ac1, b1);
    }

    public void testMixOpeRBCMP1() throws Exception {
        int ac1 = 1;
        int ac2 = 2;
        // first get initial account balances
        int b1 = mgr.readBalanceTx(ac1);
        int b2 = mgr.readBalanceTx(ac2);
        // Get num value for Product
        int num0 = mgr.readProdNum();
        int num1 = mgr.readCMP1Num();
        // call mixed operation, that should be rolled back
        int newb1 = b1 + 1;
        mgr.mixOpeCmp1(ac1, ac2, newb1, false);
        // Check that ac1 has been changed (because RequiresNew)
        Assert.assertEquals("RequiresNew should commit", newb1, mgr.readBalanceTx(ac1));
        // Check that ac2 has not been changed (because rollback)
        Assert.assertEquals("Rollback did not occur on ac2", b2, mgr.readBalanceTx(ac2));
        // Check that Product has not been changed
        Assert.assertEquals("Rollback did not occur on prod0", num0, mgr.readProdNum());
        Assert.assertEquals("Rollback did not occur on prod1", num1, mgr.readCMP1Num());
        // Reset ac1 to his value
        mgr.setBalance(ac1, b1);
    }

    public void testMixOpeRBJdbcLC() throws Exception {
        mgr.openLongConnection();
        testMixOpeRBJdbc();
        mgr.closeLongConnection();
    }
        
    public void testMixOpeRBJdbc() throws Exception {
        int ac1 = 1;
        int ac2 = 2;
        // first get initial account balances
        int b1 = mgr.readBalanceTx(ac1);
        int b2 = mgr.readBalanceTx(ac2);
        // Get num value for Product
        int num0 = mgr.readProdNum();
        int num1 = mgr.readCMP1Num();
        // call mixed operation, that should be rolled back
        int newb1 = b1 + 1;
        mgr.mixOpeJdbc(ac1, ac2, newb1, false);
        // Check that ac1 has been changed (because RequiresNew)
        Assert.assertEquals("RequiresNew should commit", newb1, mgr.readBalanceTx(ac1));
        // Check that ac2 has not been changed (because rollback)
        Assert.assertEquals("Rollback did not occur on ac2", b2, mgr.readBalanceTx(ac2));
        // Check that Product has not been changed
        Assert.assertEquals("Rollback did not occur on prod0", num0, mgr.readProdNum());
        Assert.assertEquals("Rollback did not occur on prod1", num1, mgr.readCMP1Num());
        // Reset ac1 to his value
        mgr.setBalance(ac1, b1);
    }

    public void testLongConnection() throws Exception {
        mgr.openLongConnection();
        mgr.closeLongConnection();
    }

    public String getManagerHomeName() {
        return "bankManagerCRWHome";
    }

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