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

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;
import org.objectweb.jonas.jtests.beans.fbasic.Simple;
import org.objectweb.jonas.jtests.beans.fbasic.SimpleHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * test cases common to both suites CMP and BMP.
 */
public abstract class B_Isolation extends JTestCase {

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

    protected void setUp() {
        super.setUp();
        useBeans("fbasic", true);
    }

    /**
     * return SimpleHome, that can be either BMP or CMP bean.
     */
    abstract public SimpleHome getHome();

    /**
     * Access an instance outside tx and inside tx and verify that state
     * has changed and is ok.
     *  pre condition: an element with "pk4" as primary key must exist in the database
     */
    public void testIsolation1() throws Exception {
        int val1 = 11;
        int val2 = 12;
        Simple s1 = getHome().findByPrimaryKey("pk4");
        s1.setInfo(val1);
        utx.begin();
        assertEquals(val1, s1.getInfo());
        s1.setInfo(val2);
        utx.commit();
        assertEquals(val2, s1.getInfo());
    }

    /**
     * test finder method inside a transaction.
     */
    public void testCreateFindInfoForNumInTx() throws Exception {
        int found2 = 0;
        int found3 = 0;
        int newval = 14;
        utx.begin();
        try {
            Simple s1 = getHome().create("n1", 1001, newval);
            Simple s2 = getHome().create("n2", 1002, 2);
            s2.setNumTest(newval);
            Enumeration list = getHome().findInfoForNum(newval);
            while (list.hasMoreElements()) {
                list.nextElement();
                found2++;
            }
            assertEquals("first try", 2, found2);
        } finally {
            // rolback avoids cleaning objects
            utx.rollback();
        }
    }

    /**
     * test finder method outside a transaction.
     */
    public void testCreateFindInfoForNumOutTx() throws Exception {
        int found2 = 0;
        int found3 = 0;
        int newval = 14;
        Simple s1 = null;
        Simple s2 = null;
        try {
            s1 = getHome().create("n3", 1001, newval);
            s2 = getHome().create("n4", 1002, 2);
            s2.setNumTest(newval);
            Enumeration list = getHome().findInfoForNum(newval);
            while (list.hasMoreElements()) {
                list.nextElement();
                found2++;
            }
            assertEquals("first try", 2, found2);
        } finally {
            // cleaning
            if (s1 != null) {
                s1.remove();
            }
            if (s2 != null) {
                s2.remove();
            }
        }
    }

    /**
     * test finder method inside a transaction.
     */
    public void testFinderInTx2() throws Exception {
        int found2 = 0;
        int found3 = 0;
        int newval = 14;
        utx.begin();
        try {
            Simple s1 = getHome().create("n5", 1001, newval);
            Simple s2 = getHome().create("n6", 1002, 2);
            Simple s3 = getHome().create("n7", 1003, 3);
            s2.setNumTest(newval);
            Enumeration list = getHome().findInfoForNum(newval);
            while (list.hasMoreElements()) {
                list.nextElement();
                found2++;
            }
            s3.setNumTest(newval);
            list = getHome().findInfoForNum(newval);
            while (list.hasMoreElements()) {
                list.nextElement();
                found3++;
            }
            assertEquals("second try", 3, found3);
            assertEquals("first try", 2, found2);
        } finally {
            // rollback avoids cleaning objects
            utx.rollback();
        }
    }
}
