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

import org.objectweb.jonas.jtests.beans.ebasic.Simple;
import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
import org.objectweb.jonas.jtests.util.JTestCase;

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

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

    protected void setUp() {
        super.setUp();
        useBeans("ebasic", 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();
        try {
            assertEquals(val1, s1.getInfo());
            s1.setInfo(val2);
        } finally {
            utx.commit();
        }
        assertEquals(val2, s1.getInfo());
    }

    /**
     * test finder method inside a transaction.
     */
    public void testFinderInTx() 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 testFinderOutTx() 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();
        }
    }
}
