/*
 * 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.schedules.DayOfWeekSchedule;

import junit.framework.TestCase;

/**
 *
 * @author Rob Gordon.
 */
public class DayOfWeekScheduleTest 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 {
        DayOfWeekSchedule schedule = new DayOfWeekSchedule();
        schedule.setFrom("TUE");
        schedule.setTo("WED");
 
        // 10th feb 2004 was a tuesday
        Date now1 = inputFormat.parse("10-feb-2004 12:30");
        Date expectedStart
                = checkFormat.parse("10-feb-2004 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("11-feb-2004 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));
    }
    
    public void testAfter() throws ParseException {
        DayOfWeekSchedule schedule = new DayOfWeekSchedule();
        schedule.setFrom("TUE");
        schedule.setTo("WED");
        
        Date now1 = inputFormat.parse("14-feb-2004 12:30");
        Date expectedStart
                = checkFormat.parse("17-feb-2004 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("18-feb-2004 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));
    }
    
    public void testOverBoundry() throws ParseException {
        DayOfWeekSchedule schedule = new DayOfWeekSchedule();
        schedule.setFrom("FRI");
        schedule.setTo("MON");
        
        Date now1 = inputFormat.parse("11-feb-2004 12:30");
        Date expectedStart
                = checkFormat.parse("13-feb-2004 00:00:00:000");
        Date actualStart1 = schedule.getStartDate(now1);
        assertTrue("Start [" + actualStart1 + "] wrong.", 
                expectedStart.equals(actualStart1));
        Date expectedEnd
                = checkFormat.parse("16-feb-2004 23:59:59:999");
        Date actualEnd1 = schedule.getEndDate(now1);
        assertTrue("End [" + actualEnd1 + "] wrong.", 
                expectedEnd.equals(actualEnd1));

        Date now2 = inputFormat.parse("14-feb-2004 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("16-feb-2004 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));
    }

    
    
}
