/*
 * 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 javax.ejb.FinderException;

import org.objectweb.jonas.jtests.beans.ebasic.Simple;

/**
 * This set of test are basic tests on CMP entity home interface.
 * Only specific tests to CMP are here, all tests common to CMP and BMP
 * are in the inherited class A_BasicHomeInterface.
 * @author Philippe Coq, Philippe Durieux, Helene Joanin. (jonas team)
 */
public abstract class A_BasicHomeInterfaceEC extends A_BasicHomeInterface {

    public A_BasicHomeInterfaceEC(String name) {
        super(name);
    }
  
    // --------------------------------------------------------------------
    // tests on clause where for CMP finder methods
    // These tests are specific to CMP 
    // ---------------------------------------------------------------------

    /**
     * testFinderBetween  verify a finder method that return a Enumeration
     * whith a where clause :
     *  where ?1 <= c_info and c_info <= ?2 and ?1 <= c_numtest and c_numtest <= ?2
     * the purpose of this test is to verify the correct interpretation of parameters
     * ?1 ?2
     */
    public void testFinderBetween() throws Exception {
        int value = 991959;
        getHome().create("991959_1", value, value);
        getHome().create("991959_2", value, value);
        Simple entity = null;
        Enumeration eList = null;
        eList = getHome().findInfo_beetwen(value, value);
        int nb = 0;
        while (eList.hasMoreElements()) {
            entity = (Simple) javax.rmi.PortableRemoteObject.narrow(eList.nextElement(), Simple.class);
            nb++;
            assertEquals("Wrong Info value", entity.getInfo(), value);
            assertEquals("Wrong NumTest value", entity.getNumTest(), value);
        }
        assertEquals("Wrong number of entities found", 2, nb);
        getHome().remove("991959_1");
        getHome().remove("991959_2");
    }

    /**
     * testFinderEqualOne  verify a finder method that return a Enumeration
     * whith a where clause :
     *   where c_info = ?1 and c_numtest = ?1
     * the object of this test is to verify the correct interpretation of parameters
     * ?1 ?2
     */
    public void testFinderEqualOne() throws Exception {
        Simple entity = null;
        Enumeration eList = null;
        eList = getHome().findEqualOne(80);
        int nb = 0;
        while (eList.hasMoreElements()) {
            entity = (Simple) javax.rmi.PortableRemoteObject.narrow(eList.nextElement(), Simple.class);
            nb++;
            assertEquals(80, entity.getInfo());
            assertEquals(80, entity.getNumTest());
        }
        assertEquals(2, nb);
    }

    /**
     * testFinderEqualTwo  verify a finder method that return a Enumeration
     * whith a where clause :
     *   where c_info = ? and c_numtest = ?
     * the object of this test is to verify the correct interpretation of parameters
     * ?1 ?2
     */
    public void testFinderEqualTwo() throws Exception {
        Simple entity = null;
        Enumeration eList = null;
        eList = getHome().findEqualTwo(100, 90);
        int nb = 0;
        while (eList.hasMoreElements()) {
            entity = (Simple) javax.rmi.PortableRemoteObject.narrow(eList.nextElement(), Simple.class);
            nb++;
            assertEquals(100, entity.getInfo());
            assertEquals(90, entity.getNumTest());
        }
        assertEquals(1, nb);
    }

    /**
     * Execute an finder method defined to expect a single object with the result
     * set that returns more than one.
     * Ensure that a FinderException is thrown.
     */
    public void testFindOneByNum() throws Exception {
        try {
            Simple p = getHome().findOneByNum(4);
        } catch (FinderException e) {
            // Pass
            return;
        }
        fail("FinderException not thrown as expected");
    }

    /**
     * testManyOperations verify many various operation calls.
     *  N times (create, finderXXX, getXXX, remove)
     * (do not launch systematicaly this test because it's long)
     */
    public void testManyOperations() throws Exception {
        for (int i = 0; i < 100; i++) {
            getHome().create("pkmany" + i, 20, 6);
            Simple entity2 = getHome().findByTestName("pkmany" + i);
            assertEquals(20, entity2.getInfo());
            // cleaning
            entity2.remove();
        }
    }

    /**
     * test of allowed operation on home method
     * THis test fails today (bug #300413)
     */
    public void testHomeMethod() throws Exception {
        getHome().globalOpe();
    }
}
