/**
 * 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 javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.etype.osqlts4pk.Osqlts4pk;
import org.objectweb.jonas.jtests.beans.etype.osqlts4pk.Osqlts4pkHome;
import org.objectweb.jonas.jtests.beans.etype.osqlts4pk.Pk;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This is a test suite about java.sql.Timestamp field of entity bean CMP
 * version 2. Beans used: etype/osqlts4pk
 * @author Helene Joanin
 */

public class F_EtypeOsqlts4pkEC2 extends JTestCase {

    private static String BEAN_HOME = "EtypeOsqlts4pkEC2Home";

    protected static Osqlts4pkHome home = null;

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

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

    protected void setUp() {
        super.setUp();
        useBeans("osqlts4pk", false);
    }

    public Osqlts4pkHome getHome() {
        if (home == null) {
            try {
                home = (Osqlts4pkHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME), Osqlts4pkHome.class);
            } catch (NamingException e) {
                fail("Cannot get bean home: " + BEAN_HOME);
            }
            // init here tables if CMP2
            try {
                home.create(new Timestamp(ONE_DAY), "pk1");
                home.create(new Timestamp(2 * ONE_DAY), "pk2");
                home.create(new Timestamp(3 * ONE_DAY), "pk3");
                home.create(new Timestamp(4 * ONE_DAY), "pk4");
                home.create(new Timestamp(5 * ONE_DAY), "pk5");
                home.create(new Timestamp(12 * ONE_DAY), "pktoremove");
            } catch (Exception i) {
            }
        }
        return home;
    }
    /**
     * findByPrimaryKey() test
     */
    public void testSqlTs4pkFindByPk() throws Exception {
        Pk pk = new Pk(new Timestamp(ONE_DAY));
        Osqlts4pk bean = getHome().findByPrimaryKey(pk);
        assertEquals("Pk", pk.id, bean.getId());
    }

    /**
     * findByDate() test
     */
    public void testSqlTs4pkFindDate() throws Exception {
        Pk pk = new Pk(new Timestamp(5 * ONE_DAY));
        Osqlts4pk bean = getHome().findByPrimaryKey(pk);
        assertEquals("Pk", pk.id, bean.getId());
    }

    /**
     * create() test
     */
    public void testSqlTs4pkCreate() throws Exception {
        Timestamp pk = new Timestamp(1959 * ONE_DAY);
        String f1 = "pkcreated";
        Osqlts4pk bean = getHome().create(pk, f1);
        assertEquals("Pk", pk, bean.getId());
        assertEquals("F1", f1, bean.getF1());
        // cleaning
        bean.remove();
    }

    public static Test suite() {
        return new TestSuite(F_EtypeOsqlts4pkEC2.class);
    }

    public static void main(String args[]) {
        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            Integer i_arg;
            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_EtypeOsqlts4pkEC2(testtorun));
        }
    }
}