/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 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.Iterator;
import javax.ejb.FinderException;

import org.objectweb.jonas.jtests.beans.etype.ofloat4pk.Ofloat4pk;
import org.objectweb.jonas.jtests.beans.etype.ofloat4pk.Ofloat4pkHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This set of test are common to CMP version 1 and CMP version 2
 * These are tests about java.lang.Float field of entity bean .
 * Beans used: etype/ofloat4pk
 * @author Helene Joanin
 */
public abstract class A_EtypeOfloat4pk extends JTestCase {

    private static final float DELTA = 0.01f;

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

    protected void setUp() {
        super.setUp();
        useBeans("ofloat4pk", false);
    }

    /**
     * Return Ofloat4pkHome, that can be either CMP version 1 or CMP version 2 bean.
     */
    abstract public Ofloat4pkHome getHome();

    /**
     * findByPrimaryKey() test
     */
    public void testObjFloat4pkFindByPk() throws Exception {
        Float pk = new Float(1.0);
        Ofloat4pk bean = getHome().findByPrimaryKey(pk);
        assertEquals("Pk", pk, bean.getPk());
    }

    /**
     * findByF1() test
     */
    public void testObjFloat4pkFindByF1() throws Exception {
        float f1 = 5.0f;
        Collection cBeans = getHome().findByF1(new Float(f1 - DELTA), new Float(f1 + DELTA));
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Ofloat4pk bean = (Ofloat4pk) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                         Ofloat4pk.class);
            assertEquals("F1", f1, (bean.getF1()).floatValue(), DELTA);
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * getF1() test
     */
    public void testObjFloat4pkGetF1() throws Exception {
        Float pk = new Float(2.0);
        Ofloat4pk bean = getHome().findByPrimaryKey(pk);
        Float f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", 2.0, f1.floatValue(), DELTA);
    }

    /**
     * setF1() test
     */
    public void testObjFloat4pkSetF1() throws Exception {
        Float pk = new Float(3.0);
        Ofloat4pk bean = getHome().findByPrimaryKey(pk);
        float f1 = 30.0f;
        bean.setF1(new Float(f1));
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, (bean.getF1()).floatValue(), DELTA);
    }

    /**
     * create() test
     */
    public void testObjFloat4pkCreate() throws Exception {
        float f1 = 1959.65f;
        Float pk = new Float(f1);
        Ofloat4pk bean = getHome().create(pk, new Float(f1));
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, (bean.getF1()).floatValue(), DELTA);
        bean = getHome().findByPrimaryKey(pk);
        // cleaning
        bean.remove();
    }

    /**
     * remove() test
     */
    public void testObjFloat4pkRemove() throws Exception {
        Float pk = new Float(10000.0);
        Ofloat4pk bean = getHome().findByPrimaryKey(pk);
        Float f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }

}
