package org.josast.util.date;

import junit.framework.Assert;
import junit.framework.TestCase;

import java.util.GregorianCalendar;

import org.josast.util.date.JulianDay;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision: 1.2 $
 */
public class JulianDayTest extends TestCase {
    //~ Static fields/initializers ----------------------------------------


    private static final int DAY19 = 19;


    private static final int MONTH4 = 4;


    private static final int YEAR1990 = 1990;


    private static final double JULIANDAY1941990 = 3543.0;

    //~ Constructors ------------------------------------------------------

    /**
     * Creates a new JulianDayTest object.
     *
     * @param arg0 DOCUMENT ME!
     */
    public JulianDayTest(String arg0) {
        super(arg0);
    }

    //~ Methods -----------------------------------------------------------

    /**
     * DOCUMENT ME!
     *
     * @throws Exception DOCUMENT ME!
     */
    protected void setUp() throws Exception {
        super.setUp();
    }

    /**
     * DOCUMENT ME!
     *
     * @throws Exception DOCUMENT ME!
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * <br> unit test for Julian day convertion from Gregorian Calendar
     * <br> Date used for the test :
     * <br> 	- 1957 10 4 19 26 24  julian  day value expected 2436116.31
     * <br>		- 333 1 27 12 0 0  julian  day value expected 1842713.0,
     */
    public void testconvertToJulianDayGC() {
        GregorianCalendar valeur = new GregorianCalendar(1957, GregorianCalendar.OCTOBER, 4,
                19, 26, 24);
        GregorianCalendar valeur1 = new GregorianCalendar(333, GregorianCalendar.JANUARY, 27, 12,
                0, 0);
        Assert.assertEquals(1842713.0,
            JulianDay.convertToJulianDay(valeur1), 0);
        Assert.assertEquals(2436116.31,
            JulianDay.convertToJulianDay(valeur), 0);
    }

    /**
     * unit test for Julian day convertion from a given date
     * <br> 333 1 27 12 0 0 julian  day value expected 1842713.0
     * <br> -584  5  28  0  0  0 julian  day value expected  1507899.5
     * <br> 1957 10 4 19 26, julian  day value expected 24 2436116.31
     * <br> 2000 1 1 12 0 0 julian  day value expected 2451545.0
     * <br> -4712 1 1 12 0 0  julian  day value expected 0.0
     * <br> 1999 1 1 0 0 0  julian  day value expected 2451179.5
     * <br> 1987 1 27 0 0 0 julian  day value expected 2446822.5
     */
    public void testconvertToJulianDay() {
        Assert.assertEquals(1842713.0,
            JulianDay.convertToJulianDay(333, 1, 27, 12, 0, 0), 0);
        Assert.assertEquals(1507899.5,
            JulianDay.convertToJulianDay(-584, 5, 28, 0, 0, 0), 0);
        Assert.assertEquals(2436116.31,
            JulianDay.convertToJulianDay(1957, 10, 4, 19, 26, 24),
            0);

        Assert.assertEquals(2451545.0,
            JulianDay.convertToJulianDay(2000, 1, 1, 12, 0, 0), 0);
        Assert.assertEquals(0.0,
            JulianDay.convertToJulianDay(-4712, 1, 1, 12, 0, 0), 0);
        Assert.assertEquals(2451179.5,
            JulianDay.convertToJulianDay(1999, 1, 1, 0, 0, 0), 0);
        Assert.assertEquals(2446822.5,
            JulianDay.convertToJulianDay(1987, 1, 27, 0, 0, 0), 0);
    }

    /**
     * Unit test to addDays
     *  <br> original date 1954 6 28 0, 0, 0
     *  <br> add 1 day
     *  <br> expected results 1954 6 29 0 0 0
     *  
     */
    public void testaddJours() {
        GregorianCalendar valeur = new GregorianCalendar(1954, 6, 29, 0,
                0, 0);
        GregorianCalendar valeur2 = new GregorianCalendar(1954, 6, 28, 0,
                0, 0);
        GregorianCalendar resultat = JulianDay.addDays(valeur2, 1);
        Assert.assertEquals(valeur.getTimeInMillis(),
            resultat.getTimeInMillis());
    }

    /**
     * Unit Tes from a Gregorian value 
     * <br> 333 1 27 12 0 0 julian  day value expected 1842713.0
     * <br> -584  5  28  0  0  0 julian  day value expected  1507899.5
     * <br> 1957 10 4 19 26, julian  day value expected 24 2436116.31
     */
    public void testconvertToGregorianCalendar() {
        GregorianCalendar valeur = new GregorianCalendar(1957, GregorianCalendar.OCTOBER, 4,
                19, 26, 24);
        GregorianCalendar resultat = JulianDay.convertToGregorianCalendar(2436116.31);
        Assert.assertEquals(valeur.getTimeInMillis(),
            resultat.getTimeInMillis());

        GregorianCalendar valeur1 = new GregorianCalendar(333,GregorianCalendar.JANUARY, 27, 12,
                0, 0);
        GregorianCalendar resultat1 = JulianDay.convertToGregorianCalendar(1842713.0);
        Assert.assertEquals(valeur1.getTimeInMillis(),
            resultat1.getTimeInMillis());

        GregorianCalendar valeur2 = new GregorianCalendar(-584, GregorianCalendar.MAY, 28, 0,
                0, 0);
        GregorianCalendar resultat2 = JulianDay.convertToGregorianCalendar(1507899.5);
        Assert.assertEquals(valeur2.getTimeInMillis(),
            resultat2.getTimeInMillis());
    }

    /**
     * unit test  for 
     */
    public void testdiffdate() {
        GregorianCalendar J1 = new GregorianCalendar(1835, 11, 16, 0, 0, 0);
        GregorianCalendar J2 = new GregorianCalendar(1910, MONTH4, 20, 0,
                0, 0);

        Assert.assertEquals(27183, JulianDay.diffdate(J2, J1), 0);

        // new test for bug ? 
        //Le problème rencontré est que lorsque j'essais de soustraire 1940-02-29 et 1940-01-01 j'ai comme résultat 57 alors que je m'attends à avoir 59.
        J1 = new GregorianCalendar(1940, GregorianCalendar.FEBRUARY, 29, 0, 0, 0);
        J2 = new GregorianCalendar(1940, GregorianCalendar.JANUARY, 1, 0, 0, 0);

        Assert.assertEquals(59, JulianDay.diffdate(J1, J2), 0);
    }

    /**
     * DOCUMENT ME!
     */
    public final void testconvertToJulianDay2000() {
        // 19 april 1990, at 0:00 UT
        Assert.assertEquals(-JULIANDAY1941990,
            JulianDay.convertToJulianDay2000(YEAR1990, MONTH4, DAY19, 0,
                0, 0), 0);
    }
}
