/*
 * 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.pshort.Pshort;
import org.objectweb.jonas.jtests.beans.etype.pshort.PshortHome;
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 'short' field of entity bean .
 * Beans used: etype/pshort
 * @author Helene Joanin
 */
public abstract class A_EtypePshort extends JTestCase {

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

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

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

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

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

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

    /**
     * getF1() test
     */
    public void testPrimShortGetF1() throws Exception {
        String pk = "pk2";
        Pshort bean = getHome().findByPrimaryKey(pk);
        short f1 = bean.getF1();
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", (short) 2, f1);
    }

    /**
     * setF1() test
     */
    public void testPrimShortSetF1() throws Exception {
        String pk = "pk3";
        Pshort bean = getHome().findByPrimaryKey(pk);
        short f1 = (short) 30;
        bean.setF1(f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", f1, bean.getF1());
    }

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

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

}
