/*
 * 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.pboolean.Pboolean;
import org.objectweb.jonas.jtests.beans.etype.pboolean.PbooleanHome;
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 'boolean' field of entity bean .
 * Beans used: etype/pboolean
 * @author Helene Joanin
 */
public abstract class A_EtypePboolean extends JTestCase {

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

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

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

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

    /**
     * findByF1() test
     */
    public void testPrimBooleanFindByF1() throws Exception {
        boolean f1 = true;
        Collection cBeans = getHome().findByF1(f1);
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Pboolean bean = (Pboolean) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                             Pboolean.class);
            Assert.assertEquals("F1", f1, bean.getF1());
        }
    }

    /**
     * findByF1LiteralTrue() test
     */
    public void testPrimBooleanFindByF1LiteralTrue() throws Exception {
        Collection cBeans = getHome().findByF1LiteralTrue();
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Pboolean bean = (Pboolean) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                             Pboolean.class);
            Assert.assertEquals("F1 of " + bean.getPk() + ": "
                                , true, bean.getF1());
        }
    }

    /**
     * getF1() test
     */
    public void testPrimBooleanGetF1() throws Exception {
        String pk = "pk2";
        Pboolean bean = getHome().findByPrimaryKey(pk);
        boolean f1 = bean.getF1();
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", true, f1);
    }

    /**
     * setF1() test
     */
    public void testPrimBooleanSetF1() throws Exception {
        String pk = "pk3";
        Pboolean bean = getHome().findByPrimaryKey(pk);
        boolean f1 = true;
        bean.setF1(f1);
        Assert.assertEquals("Pk", pk, bean.getPk());
        Assert.assertEquals("F1", f1, bean.getF1());
    }

    /**
     * create() test
     */
    public void testPrimBooleanCreate() throws Exception {
        String pk = "pkcreated";
        boolean f1 = false;
        Pboolean 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 testPrimBooleanRemove() throws Exception {
        String pk = "pktoremove";
        Pboolean bean = getHome().findByPrimaryKey(pk);
        boolean f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }

}
