package com.greef.ui.calendar;

import junit.framework.TestCase;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Tests {@link CalendarTableModel}.
 * @author Adrian BER
 */
public class CalendarTableModelTestCase extends TestCase {

    private CalendarTableModel model;

    public CalendarTableModelTestCase() {}

    protected void setUp() throws Exception {
        model = new CalendarTableModel();
    }

    public void testGetRows() {
        assertEquals("Number of weeks in month", 6, model.getRowCount());
        assertEquals("Maximum number of weeks in month", 6, model.getMaxRowCount());
    }

    public void testGetDayOfWeek() {
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        assertEquals("Day of week error with first day of week Sunday",
            Calendar.SUNDAY, model.getDayOfWeek(0));
        assertEquals("Day of week error with first day of week Sunday",
            2, model.getDayOfWeek(1));
        assertEquals("Day of week error with first day of week Sunday",
            3, model.getDayOfWeek(2));
        assertEquals("Day of week error with first day of week Sunday",
            4, model.getDayOfWeek(3));
        assertEquals("Day of week error with first day of week Sunday",
            5, model.getDayOfWeek(4));
        assertEquals("Day of week error with first day of week Sunday",
            6, model.getDayOfWeek(5));
        assertEquals("Day of week error with first day of week Sunday",
            7, model.getDayOfWeek(6));
        model.setFirstDayOfWeek(Calendar.WEDNESDAY);
        assertEquals("Day of week error with first day of week Saturday",
            Calendar.WEDNESDAY, model.getDayOfWeek(0));
        assertEquals("Day of week error with first day of week Saturday",
            5, model.getDayOfWeek(1));
        assertEquals("Day of week error with first day of week Saturday",
            6, model.getDayOfWeek(2));
        assertEquals("Day of week error with first day of week Saturday",
            7, model.getDayOfWeek(3));
        assertEquals("Day of week error with first day of week Saturday",
            1, model.getDayOfWeek(4));
        assertEquals("Day of week error with first day of week Saturday",
            2, model.getDayOfWeek(5));
        assertEquals("Day of week error with first day of week Saturday",
            3, model.getDayOfWeek(6));
        model.setFirstDayOfWeek(Calendar.SATURDAY);
        assertEquals("Day of week error with first day of week Saturday",
            Calendar.SATURDAY, model.getDayOfWeek(0));
        assertEquals("Day of week error with first day of week Saturday",
            1, model.getDayOfWeek(1));
        assertEquals("Day of week error with first day of week Saturday",
            2, model.getDayOfWeek(2));
        assertEquals("Day of week error with first day of week Saturday",
            3, model.getDayOfWeek(3));
        assertEquals("Day of week error with first day of week Saturday",
            4, model.getDayOfWeek(4));
        assertEquals("Day of week error with first day of week Saturday",
            5, model.getDayOfWeek(5));
        assertEquals("Day of week error with first day of week Saturday",
            6, model.getDayOfWeek(6));
    }

    public void testGetValue() {
        model.setDate(3, 2004);
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        assertEquals("Previous month days error", new Integer(30), model.getValueAt(0, 2));
        assertEquals("Current month days error", new Integer(7), model.getValueAt(1, 3));
        assertEquals("Current month days error", new Integer(23), model.getValueAt(3, 5));
        assertEquals("Next month days error", new Integer(1), model.getValueAt(4, 6));
    }

    public void testGetValue6() {
        model.setDate(3, 2004);
        model.setFirstDayOfWeek(Calendar.SATURDAY);
        assertEquals("Previous month days error with Saturday first",
                new Integer(29), model.getValueAt(0, 2));
        assertEquals("Current month days error with Saturday first",
                new Integer(6), model.getValueAt(1, 3));
        assertEquals("Current month days error with Saturday first",
                new Integer(22), model.getValueAt(3, 5));
        assertEquals("Next month days error with Saturday first",
                new Integer(30), model.getValueAt(4, 6));
    }

    public void testGetValueDates() {
        model.setDate(0, 2004);
        model.setFirstDayOfWeek(Calendar.TUESDAY);
        assertEquals("01/2004, Tuesday first",
                new Integer(1), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.WEDNESDAY);
        assertEquals("01/2004, Wednesday first",
                new Integer(2), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.FRIDAY);
        assertEquals("01/2004, Friday first",
                new Integer(28), model.getValueAt(0, 2));

        model.setDate(2, 2004);
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        assertEquals("03/2004, Sunday first",
                new Integer(2), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.MONDAY);
        assertEquals("03/2004, Monday first",
                new Integer(3), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.SATURDAY);
        assertEquals("03/2004, Saturday first",
                new Integer(1), model.getValueAt(0, 2));

        model.setDate(3, 2004);
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        assertEquals("04/2004, Sunday first",
                new Integer(30), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.MONDAY);
        assertEquals("04/2004, Monday first",
                new Integer(31), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.TUESDAY);
        assertEquals("04/2004, Tuesday first",
                new Integer(1), model.getValueAt(0, 2));
        model.setFirstDayOfWeek(Calendar.SATURDAY);
        assertEquals("04/2004, Saturday first",
                new Integer(29), model.getValueAt(0, 2));
    }

    public void testGetPositionFor() {
        model.setDate(3, 2004);
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        Calendar calendar = new GregorianCalendar(2004, 3, 7);
        assertEquals("getPositionFor error", 10, model.getPositionFor(calendar));

        calendar = new GregorianCalendar(2004, 2, 29);
        assertEquals("getPositionFor error", 1, model.getPositionFor(calendar));

        calendar = new GregorianCalendar(2004, 2, 21);
        assertEquals("getPositionFor error for not visible values",
                -1, model.getPositionFor(calendar));

        calendar = new GregorianCalendar(2004, 4, 11);
        assertEquals("getPositionFor error for not visible values",
                -1, model.getPositionFor(calendar));
    }

    public void testGetCalendarAt1() {
        model.setFirstDayOfWeek(Calendar.SUNDAY);
        model.setDate(5, 2004);
        assertEquals(new GregorianCalendar(2004, 5, 3).getTime(),
                model.getCalendarAt(4).getTime());
    }
}
