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

import org.objectweb.jonas.jtests.beans.etype.outildate.Outildate;
import org.objectweb.jonas.jtests.beans.etype.outildate.OutildateHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This set of test are common to CMP version 1 and CMP version 2
 * NOTE: In fact, this type is not supported in CMP1.
 * These are tests about java.lang.Utildate field of entity bean .
 * Beans used: etype/outildate
 * @author Helene Joanin
 */
public abstract class A_EtypeOutildate extends JTestCase {

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

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

    protected void setUp() {
        super.setUp();
    }

    /**
     * Return OutildateHome.
     */
    abstract public OutildateHome getHome();

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

    /**
     * findByF1() test
     */
    public void testUtilDateFindByF1() throws Exception {
        Date d5 = new Date(5 * ONE_DAY);
        Collection cBeans = getHome().findByF1(d5);
        int nb = 0;
        Iterator iBeans = cBeans.iterator();
        while (iBeans.hasNext()) {
            Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                               Outildate.class);
            Date 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 testUtilDateGetF1() throws Exception {
        String pk = "pk2";
        Outildate bean = getHome().findByPrimaryKey(pk);
        Date d2 = new Date(2 * ONE_DAY);
        Date 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 Date(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 testUtilDateGetF1Null() throws Exception {
        String pk = "pknull";
        Outildate bean = getHome().findByPrimaryKey(pk);
        Date f1 = bean.getF1();
        assertEquals("Pk", pk, bean.getPk());
        assertNull("F1 not null", f1);
    }

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

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

    /**
     * create() test
     */
    public void testUtilDateCreate() throws Exception {
        String pk = "pkcreated";
        Date f1 = new Date(1959 * ONE_DAY);
        Outildate 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 testUtilDateRemove() throws Exception {
        String pk = "pktoremove";
        Outildate bean = getHome().findByPrimaryKey(pk);
        Date f1 = bean.getF1();
        bean.remove();
        try {
            getHome().findByPrimaryKey(pk);
            fail("not removed");
        } catch (FinderException e) {
            // ok
        }
        // cleaning
        getHome().create(pk, f1);
    }
    /**
     * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_outildate o WHERE o.f1 < ?1
     */
    public void testUtilDateLessThen() throws Exception {
        Date d3 = new Date(3*ONE_DAY);
        Collection cBeans = getHome().findByF1LessThen(d3);
        Iterator iBeans = cBeans.iterator();
        int nb=0;
        while(iBeans.hasNext()) {
            Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                               Outildate.class);
            Date f = bean.getF1();
            assertTrue("Pk="+bean.getPk()+",f1="+f, f.compareTo(d3) < 0);
            nb++;
        }
        assertEquals("Beans number: ", 2, nb);
    }

    /**
     * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_outildate o WHERE o.f1 <= ?1
     */
    public void testUtilDateLessOrEqualThen() throws Exception {
        Date d3 = new Date(3*ONE_DAY);
        Collection cBeans = getHome().findByF1LessOrEqualThen(d3);
        Iterator iBeans = cBeans.iterator();
        int nb=0;
        while(iBeans.hasNext()) {
            Outildate bean = (Outildate) javax.rmi.PortableRemoteObject.narrow(iBeans.next(),
                                                                               Outildate.class);
            Date f = bean.getF1();
            assertTrue("Pk="+bean.getPk()+",f1="+f, f.compareTo(d3) <= 0);
            nb++;
        }
        assertEquals("Beans number: ", 3, nb);
    }

    /**
     * Test the EJB-QL: SELECT OBJECT(o) FROM jt2_outildate o
     *    WHERE (?1 is null or o.f1 < ?1)
     *   and (?2 is null or o.f1 > ?2)
     *   and (?3 is null or o.pk = ?3)
     *   must reproduce bug #306178
     */
    public void testUtilDateWithThreeArgs() throws Exception {
        Date d5 = new Date(5*ONE_DAY);
        Date d1 = new Date(ONE_DAY);
        Collection cBeans = getHome().findWithThreeArgs(d5,d1,"pk4");
        assertEquals("Beans number: ", 1, cBeans.size() );
    }

}
