/*
 * 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 junit.framework.TestCase;

import org.treesched.Interval;
import org.treesched.Schedule;

/**
 *
 * @author Rob Gordon.
 */
public class OccurenceScheduleTest 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 testBasic() throws ParseException {
        OccurrenceSchedule schedule = new OccurrenceSchedule();
        schedule.setOccurrence("2");

        MockSchedule child = new MockSchedule(); 
        child.setResults(new Interval[] {
                new Interval(
                        inputFormat.parse("10-feb-2004 10:00"),
                        inputFormat.parse("10-feb-2004 14:00")),
                new Interval(
                        inputFormat.parse("11-feb-2004 10:00"), 
                        inputFormat.parse("11-feb-2004 14:00")) 
            });        
        schedule.addChildSchedule(child);
        Date now1 = inputFormat.parse("10-feb-2004 12:30");
        Interval expected = new Interval(
                        inputFormat.parse("11-feb-2004 10:00"), 
                        inputFormat.parse("11-feb-2004 14:00")); 
        Interval actual = schedule.nextDue(now1);
        assertTrue("[" + actual+ "] wrong.", 
        expected.equals(actual));
    }
    
    public void testOutsideLimits() throws ParseException {
        OccurrenceSchedule schedule = new OccurrenceSchedule();
        schedule.setOccurrence("2");

        MockSchedule child = new MockSchedule(); 
        child.setResults(new Interval[] {
                new Interval(
                        inputFormat.parse("10-feb-2004 10:00"),
                        inputFormat.parse("10-feb-2004 14:00")),
                new Interval(
                        inputFormat.parse("11-feb-2004 10:00"), 
                        inputFormat.parse("11-feb-2004 14:00")) 
            });
        schedule.addChildSchedule(child);
        Date now1 = inputFormat.parse("10-feb-2004 12:30");
        Interval expected = null;
        schedule.setLimits(new Interval(
                inputFormat.parse("10-feb-2004 09:00"), 
                inputFormat.parse("10-feb-2004 15:00")));
        Interval actual = schedule.nextDue(now1);
        assertTrue("[" + actual+ "] wrong.", 
                actual == expected);
    }    
    
    private static class MockSchedule implements Schedule {
        Interval[] results;
        int next;
        public void setResults(Interval[] results) {
            this.results = results;
            next = 0;
        }
        public void setLimits(Interval limits) {}
        public Interval nextDue(Date now) {
            if (results == null) {
                return null;
            }
            if (next < results.length) {
                return results[next++];
            }
            return null;
        }
        public void reset() {}
    }
}
