/*
 * Copyright (c) 2004, Rob Gordon.
 */
package org.treesched.schedules;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.treesched.Interval;
import org.treesched.schedules.DayOfMonthSchedule;

import junit.framework.TestCase;

/**
 *
 * @author Rob Gordon.
 */
public class DayOfMonthScheduleTest extends TestCase {

    static DateFormat checkFormat = new SimpleDateFormat("dd-MMM-yy HH:mm:ss:SSS");
    static DateFormat inputFormat = new SimpleDateFormat("dd-MMM-yy HH:mm");
    
    public void testFromAndTo() throws ParseException {
        DayOfMonthSchedule schedule = new DayOfMonthSchedule();
        schedule.setFrom("5");
        schedule.setTo("25");
        
        Date now1 = inputFormat.parse("10-feb-2003 12:30");
        Date expectedStart
                = checkFormat.parse("05-feb-2003 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("25-feb-2003 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));
    }
    
    public void testAfter() throws ParseException {
        DayOfMonthSchedule schedule = new DayOfMonthSchedule();
        schedule.setFrom("5");
        schedule.setTo("25");
        
        Date now1 = inputFormat.parse("26-feb-2003 12:30");
        Date expectedStart
                = checkFormat.parse("05-mar-2003 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("25-mar-2003 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));
    }
    
    public void testOverBoundry() throws ParseException {
        DayOfMonthSchedule schedule = new DayOfMonthSchedule();
        schedule.setFrom("25");
        schedule.setTo("5");
        
        Date now1 = inputFormat.parse("26-feb-2003 12:30");
        Date expectedStart
                = checkFormat.parse("25-feb-2003 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("5-mar-2003 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));

        Date now2 = inputFormat.parse("24-feb-2003 12:30");
        Date actualStart2 = schedule.getStartDate(now2);
        assertTrue("Start [" + actualStart2 + "] wrong.", 
                expectedStart.equals(actualStart2));
        Date actualEnd2 = schedule.getEndDate(now2);
        assertTrue("End [" + actualEnd2 + "] wrong.", 
                expectedEnd.equals(actualEnd2));

        Date now3 = inputFormat.parse("1-mar-2003 12:30");
        Date actualStart3 = schedule.getStartDate(now3);
        assertTrue("Start [" + actualStart3 + "] wrong.", 
                expectedStart.equals(actualStart3));
        Date actualEnd3 = schedule.getEndDate(now3);
        assertTrue("End [" + expectedEnd + "] wrong.", 
                expectedEnd.equals(actualEnd3));
    }

    public void testLastDay() throws ParseException {
        DayOfMonthSchedule schedule = new DayOfMonthSchedule();
        schedule.setFrom("5");
        schedule.setTo("0");
        
        Date now1 = inputFormat.parse("02-mar-2003 12:30");
        Interval interval1 = schedule.nextDue(now1);
        Interval expected1 = new Interval(
                checkFormat.parse("05-mar-2003 00:00:00:000"),
                checkFormat.parse("31-mar-2003 23:59:59:999"));
        System.out.println("interval1: " + interval1);
        assertTrue("interval as expected", interval1.equals(expected1));

        schedule.setTo("-1");
        Interval interval2 = schedule.nextDue(now1);
        Interval expected2 = new Interval(
                checkFormat.parse("05-mar-2003 00:00:00:000"),
                checkFormat.parse("30-mar-2003 23:59:59:999"));
        System.out.println("interval2: " + interval2);
        assertTrue("interval as expected", interval2.equals(expected2));        
    }
    
    
}
