package com.greef.ui.calendar;

import junit.framework.TestCase;

import java.util.GregorianCalendar;

/**
 * Tesst <code>{@link DefaultDateSelectionModel}</code>.
 * @author Adrian BER
 */
public class DefaultDateSelectionModelTestCase extends TestCase {

    private DefaultDateSelectionModel x;

    protected void setUp() throws Exception {
        super.setUp();
        x = new DefaultDateSelectionModel();
        x.setSelectionInterval(new GregorianCalendar(2004, 7, 20).getTime(),
            new GregorianCalendar(2004, 7, 25).getTime());
        x.addSelectedInterval(new GregorianCalendar(2004, 6, 1).getTime(),
            new GregorianCalendar(2004, 6, 18).getTime());
        x.addSelectedInterval(new GregorianCalendar(2004, 8, 10).getTime(),
            new GregorianCalendar(2004, 8, 15).getTime());
    }

    public void testContains() {
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 7, 25).getTime()));
        assertFalse("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 7, 26).getTime()));
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 8, 13).getTime()));
    }

    public void testMinSelectedDate() {
        assertEquals("minSelectedDate failed when min inside a selected interval",
            new GregorianCalendar(2004, 7, 23).getTime(),
            x.getMinSelectedDate(new GregorianCalendar(2004, 7, 23).getTime(),
                new GregorianCalendar(2004, 7, 29).getTime()));
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 8, 13).getTime()));
        x.removeAllSelectedIntervals();
        assertEquals("minSelectedDate failed when there's no selected interval", null,
            x.getMinSelectedDate(new GregorianCalendar(2004, 7, 23).getTime(),
                new GregorianCalendar(2004, 7, 29).getTime()));
    }

    public void testMaxSelectedDate() {
        assertEquals("maxSelectedDate failed when max is the limit of a selected interval",
            new GregorianCalendar(2004, 7, 25).getTime(),
            x.getMaxSelectedDate(new GregorianCalendar(2004, 7, 23).getTime(),
                new GregorianCalendar(2004, 7, 29).getTime()));
        x.removeAllSelectedIntervals();
        assertEquals("maxSelectedDate failed when there's no selected interval", null,
            x.getMaxSelectedDate(new GregorianCalendar(2004, 7, 23).getTime(),
                new GregorianCalendar(2004, 7, 29).getTime()));
    }

    public void testRemoveSelectedInterval() {
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 7, 24).getTime()));
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 6, 5).getTime()));
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 6, 10).getTime()));
        x.removeSelectedInterval(new GregorianCalendar(2004, 6, 9).getTime(),
            new GregorianCalendar(2004, 6, 14).getTime());
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 7, 24).getTime()));
        assertTrue("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 6, 5).getTime()));
        assertFalse("isDateSelected failed",
            x.isDateSelected(new GregorianCalendar(2004, 6, 10).getTime()));
    }
}
