/*
 * 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.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;

import org.objectweb.jonas.jtests.beans.etype.osqlts.Osqlts;
import org.objectweb.jonas.jtests.beans.etype.osqlts.OsqltsHome;
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.sql.Timestamp field of entity bean .
 * Beans used: etype/osqlts
 * @author Helene Joanin
 */
public abstract class A_EtypeOsqlts extends JTestCase {

    private static final long ONE_HOUR = 60L * 60L * 1000L;
    private static final long ONE_DAY = 24L * ONE_HOUR;

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

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

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

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

    /**
     * findByF1() test
     */
    public void testSqlTsFindByF1() throws Exception {
        Timestamp d5 = new Timestamp(5 * ONE_DAY);
        Collection cBeans = getHome().findByF1(d5);
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Osqlts bean = (Osqlts) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                             Osqlts.class);
            Timestamp f1 = bean.getF1();
            // This assert may fail because of TIME ZONE problem ??
            //   F1 expected:<Tue Jan 06 01:00:00 CET 1970> but was:<1970-01-06>
            //assertEquals("F1", d5, f1);
            if ((f1.getTime() - d5.getTime() > 2 * ONE_HOUR)
                || (f1.getTime() - d5.getTime() < -2 * ONE_HOUR)) {
                // The two dates differs than more 2 HOURS -> error
                assertEquals("F1", d5, f1);
            }
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * getF1() test
     */
    public void testSqlTsGetF1() throws Exception {
        String pk = "pk2";
        Osqlts bean = getHome().findByPrimaryKey(pk);
        Timestamp d2 = new Timestamp(2 * ONE_DAY);
        Timestamp f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        // The following assert may fail because of TIME ZONE problem ??
        //   F1 expected:<Sat Jan 03 01:00:00 CET 1970> but was:<1970-01-03>
        // assertEquals("F1", new Timestamp(2*ONE_DAY), f1);
        if ((f1.getTime() - d2.getTime() > 2 * ONE_HOUR)
            || (f1.getTime() - d2.getTime() < -2 * ONE_HOUR)) {
            // The two dates differs than more 2 HOURS -> error
            assertEquals("F1", d2, f1);
        }
    }

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

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

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

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

}
