/*
 * 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 org.objectweb.jonas.jtests.beans.etype.oser.Name;
import org.objectweb.jonas.jtests.beans.etype.oser.Oser;
import org.objectweb.jonas.jtests.beans.etype.oser.OserHome;
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.Boolean field of entity bean .
 * Beans used: etype/oser
 * @author Helene Joanin
 */
public abstract class A_EtypeOser extends JTestCase {


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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        useBeans("oser", true);
        if (!isInit) {
            OserHome home = getHome();
            // check if tables have been initialized
            try {
                home.findByPrimaryKey("pk1");
            } catch (Exception e) { 
                // init here tables
                try {
                    home.create("pk1", new Name("fn_1", "ln_1"));
                    home.create("pk2", new Name("fn_2", "ln_2"));
                    home.create("pk3", new Name("fn_3", "ln_3"));
                    home.create("pk4", new Name("fn_4", "ln_4"));
                    home.create("pk5", new Name("fn_5", "ln_5"));
                    home.create("pk5bis", new Name("fn_5", "ln_5"));
                    home.create("pktoremove", new Name("fn_remove", "ln_remove"));
                    home.create("pknull", (Name) null);
                    home.create("pkchangenull", new Name("fn_changenull", "ln_changenull"));
                } catch (Exception i) {
                    fail("InitialState creation problem: " + i);
                }
            }
            isInit = true;
        }
    }

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

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

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

    /**
     * getF1() test
     */
    public void testSerializableGetF1() throws Exception {
        String pk = "pk2";
        Oser bean = getHome().findByPrimaryKey(pk);
        Name f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", new Name("fn_2", "ln_2"), f1);
    }

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

    /**
     * setF1() test
     */
    public void testSerializableSetF1() throws Exception {
        String pk = "pk3";
        Oser bean = getHome().findByPrimaryKey(pk);
        Name f1 = new Name("fn_3_bis", "ln_3_bis");
        bean.setF1(f1);
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, bean.getF1());
    }

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

    /**
     * create() test
     */
    public void testSerializableCreate() throws Exception {
        String pk = "pkcreated";
        Name f1 = new Name("fn_created", "ln_created");
        Oser 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 testSerializableRemove() throws Exception {
        String pk = "pktoremove";
        Oser bean = getHome().findByPrimaryKey(pk);
        Name f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }

}
