/*
 * 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 org.objectweb.jonas.jtests.beans.annuaire.Personne;
import org.objectweb.jonas.jtests.beans.annuaire.PersonneHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This is test of the TimerService.
 * Beans used: annuaire
 * @author Philippe Durieux (jonas team)
 */
public abstract class A_Timer extends JTestCase {

    String mynum = "638";
    String myname = "Philippe Durieux";

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

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

    /**
     * return PersonneHome, that can be either CMP v1 or CMP v2 bean.
     */
    abstract public PersonneHome getHome();

    /**
     * Test simple timer
     */
    public void testTimer1() throws Exception {
        int duration = 5;
        Personne p = getHome().findByPrimaryKey(myname);
        int oldval = p.getTimerCount();
        int id = p.setTimer(duration, 0);
        assertEquals("timer expired too quickly", oldval, p.getTimerCount());
        sleep(1000 * (duration + 1));
        assertEquals("timer did not expired", oldval + 1, p.getTimerCount());
    }

    /**
     * test getTimeRemaining on a Timer
     */
    public void testTimeRemaining() throws Exception {
        int duration1 = 5000;
        int duration2 = 2000;
        Personne p = getHome().findByPrimaryKey(myname);
        int id1 = p.setTimer(duration1, 0);
        int id2 = p.setTimer(duration2, 0);
        long t1 = p.getTimeRemaining(id1) / 1000;
        long t2 = p.getTimeRemaining(id2) / 1000;
        sleep(1000);
        assertTrue(t1 < duration1);
        assertTrue(t2 < duration2);
        assertTrue(t1 > duration1 - 2);
        assertTrue(t2 > duration2 - 2);
    }

    /**
     * Test du cancel Timer
     */
    public void testCancel1() throws Exception {
        int duration = 2;
        Personne p = getHome().findByPrimaryKey(myname);
        int oldval = p.getTimerCount();
        int id = p.setTimer(duration, 0);
        assertEquals("timer expired too quickly", oldval, p.getTimerCount());
        p.cancelTimer(id);
        sleep(1000 * (duration + 1));
        assertEquals("timer did expired", oldval, p.getTimerCount());
    }

}
