/*
 * 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.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.FinderException;

import org.objectweb.jonas.jtests.beans.etype.obigd.Obigdecimal;
import org.objectweb.jonas.jtests.beans.etype.obigd.ObigdecimalHome;
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.math.BigDecimal field of entity bean .
 * Beans used: etype/obigd
 * @author Helene Joanin
 */
public abstract class A_EtypeObigdecimal extends JTestCase {

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

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

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

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

    /**
     * findByF1() test
     */
    public void testBigDecimalFindByF1() throws Exception {
        BigDecimal f1 = new BigDecimal("5");
        Collection cBeans = getHome().findByF1(f1);
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Obigdecimal bean = (Obigdecimal) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                                   Obigdecimal.class);
            assertEquals("F1", f1, bean.getF1());
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * getF1() test
     */
    public void testBigDecimalGetF1() throws Exception {
        String pk = "pk2";
        Obigdecimal bean = getHome().findByPrimaryKey(pk);
        BigDecimal f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", new BigDecimal("2"), f1);
    }

    /**
     * getF1Null() test: f1 with a 'null' value
     */
    public void testBigDecimalGetF1Null() throws Exception {
        String pk = "pknull";
        Obigdecimal bean = getHome().findByPrimaryKey(pk);
        BigDecimal f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertNull("F1 not null", f1);
    }

    /**
     * setF1() test
     */
    public void testBigDecimalSetF1() throws Exception {
        String pk = "pk3";
        Obigdecimal bean = getHome().findByPrimaryKey(pk);
        BigDecimal f1 = new BigDecimal("30");
        bean.setF1(f1);
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, bean.getF1());
    }

    /**
     * setF1Null() test: f1 with a 'null' value
     */
    public void testBigDecimalSetF1Null() throws Exception {
        String pk = "pkchangenull";
        Obigdecimal bean = getHome().findByPrimaryKey(pk);
        bean.setF1(null);
        assertEquals("Pk", pk, bean.getPk());
        assertNull("F1", bean.getF1());
    }

    /**
     * create() test
     */
    public void testBigDecimalCreate() throws Exception {
        String pk = "pkcreated";
        BigDecimal f1 = new BigDecimal("1959");
        Obigdecimal bean = getHome().create(pk, f1);
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, bean.getF1());
        bean = getHome().findByPrimaryKey(pk);
        // cleaning
        bean.remove();
    }

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

}
