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

import junit.framework.Assert;

import org.objectweb.jonas.jtests.beans.etype.pdouble.Pdouble;
import org.objectweb.jonas.jtests.beans.etype.pdouble.PdoubleHome;
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 'double' field of entity bean .
 * Beans used: etype/pdouble
 * @author Helene Joanin
 */
public abstract class A_EtypePdouble extends JTestCase {

    private static final double DELTA = 0.01d;

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

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

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

    /**
     * findByPrimaryKey() test
     */
    public void testPrimDoubleFindByPk() throws Exception {
        String pk = "pk1";
        Pdouble bean = getHome().findByPrimaryKey(pk);
        Assert.assertEquals("Pk", pk, bean.getPk());
    }

    /**
     * findByF1() test
     */
    public void testPrimDoubleFindByF1() throws Exception {
        double f1 = 5.0d;
        Collection cBeans = getHome().findByF1(f1 - DELTA, f1 + DELTA);
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Pdouble bean = (Pdouble) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                           Pdouble.class);
            Assert.assertEquals("F1", f1, bean.getF1(), DELTA);
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * findByF1Literal5() test
     */
    public void testPrimDoubleFindByF1Literal5() throws Exception {
        double f1 = 5.0d;
        Collection cBeans = getHome().findByF1Literal5();
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Pdouble bean = (Pdouble) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                           Pdouble.class);
            Assert.assertEquals("F1", f1, bean.getF1(), DELTA);
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * getF1() test
     */
    public void testPrimDoubleGetF1() throws Exception {
        String pk = "pk2";
        Pdouble bean = getHome().findByPrimaryKey(pk);
        double f1 = bean.getF1();
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", 2.0d, f1, DELTA);
    }

    /**
     * setF1() test
     */
    public void testPrimDoubleSetF1() throws Exception {
        String pk = "pk3";
        Pdouble bean = getHome().findByPrimaryKey(pk);
        double f1 = 30.0d;
        bean.setF1(f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", f1, bean.getF1(), DELTA);
    }

    /**
     * create() test
     */
    public void testPrimDoubleCreate() throws Exception {
        String pk = "pkcreated";
        double f1 = 1959.65d;
        Pdouble bean = getHome().create(pk, f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", f1, bean.getF1(), DELTA);
        bean = getHome().findByPrimaryKey(pk);
        // cleaning
        bean.remove();
    }

    /**
     * remove() test
     */
    public void testPrimDoubleRemove() throws Exception {
        String pk = "pktoremove";
        Pdouble bean = getHome().findByPrimaryKey(pk);
        double f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }

}
