/*
 * 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.oobj.Oobj;
import org.objectweb.jonas.jtests.beans.etype.oobj.OobjHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * A_EtypeOobj
 *
 * @author Rafael H. Schloming &lt;rhs@mit.edu&gt;
 **/

public abstract class A_EtypeOobj extends JTestCase {

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

    abstract public OobjHome getHome();

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        useBeans("oobj", false);
        if (!isInit) {
            OobjHome home = getHome();
            // check if tables have been initialized
            try {
                home.findByPrimaryKey("pk1");
            } catch (Exception e) { 
                // init here tables
                try {
                    home.create("pk1", "f_1");
                    home.create("pk2", "f_2");
                    home.create("pk3", "f_3");
                    home.create("pk4", "f_4");
                    home.create("pk5", "f_5");
                    home.create("pk5bis", "f_5");
                    home.create("pktoremove", "f_remove");
                    home.create("pknull", null);
                    home.create("pkchangenull", "f_changenull");
                } catch (Exception i) {
                    fail("InitialState creation problem: " + i);
                }
            }
            isInit = true;
        }
    }

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

    /**
     * getF1() test
     */
    public void testObjectGetF1() throws Exception {
        String pk = "pk2";
        Oobj bean = getHome().findByPrimaryKey(pk);
        String f1 = (String) bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", "f_2", f1);
    }

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

    /**
     * setF1() test
     */
    public void testObjectSetF1() throws Exception {
        String pk = "pk3";
        Oobj bean = getHome().findByPrimaryKey(pk);
        String f1 = "f_3_bis";
        bean.setF1(f1);
        assertEquals("Pk", pk, bean.getPk());
        assertEquals("F1", f1, bean.getF1());
    }

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

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

}
