package org.mifos.schedule;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.joda.time.DateTime;
import org.junit.Test;
import org.mifos.calendar.DayOfWeek;
import org.mifos.domain.builders.ScheduledEventBuilder;

public class MonthlyOnWeekAndWeekDayScheduledEventTest {

    private ScheduledEvent scheduledEvent;

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingInSameWeekButOneDayBehind() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(1).on(DayOfWeek.tuesday()).build();

        DateTime firstMondayOfMay2010 = new DateTime().withYear(2010).withMonthOfYear(5).withDayOfMonth(3).toDateMidnight().toDateTime();

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(firstMondayOfMay2010);

        assertThat(result, is(firstMondayOfMay2010.plusDays(1)));
    }

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingOneWeekAndDayBehind() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(2).on(DayOfWeek.tuesday()).build();

        DateTime firstMondayOfMay2010 = new DateTime().withYear(2010).withMonthOfYear(5).withDayOfMonth(3).toDateMidnight().toDateTime();

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(firstMondayOfMay2010);

        assertThat(result, is(firstMondayOfMay2010.plusDays(1).plusWeeks(1)));
    }

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingOneWeekAndDayAhead() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(1).on(DayOfWeek.tuesday()).build();

        DateTime wednesday7thOfApril = new DateTime().withDayOfMonth(7).withMonthOfYear(4).withYear(2010).withDayOfWeek(DayOfWeek.wednesday())
                .toDateMidnight().toDateTime();

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(wednesday7thOfApril);

        assertThat(result, is(tuesdayOnFirstWeekOneMonthFrom(wednesday7thOfApril)));
    }

    private DateTime tuesdayOnFirstWeekOneMonthFrom(final DateTime from) {
        return from.plusMonths(1).withDayOfMonth(7).withDayOfWeek(DayOfWeek.tuesday());
    }
}
@@ -1,8 +1,791 @@
+/*%0D%0A * Copyright (c) 2005-2010 Grameen Foundation USA%0D%0A * All rights reserved.%0D%0A *%0D%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0D%0A * you may not use this file except in compliance with the License.%0D%0A * You may obtain a copy of the License at%0D%0A *%0D%0A *     http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or%0D%0A * implied. See the License for the specific language governing%0D%0A * permissions and limitations under the License.%0D%0A *%0D%0A * See also http://www.apache.org/licenses/LICENSE-2.0.html for an%0D%0A * explanation of the license and how it is applied.%0D%0A */%0D%0A%0D%0A
 package 
@@ -3079,8 +3079,10 @@
     %7D%0D%0A%7D
+%0D%0A

use boilerplate Mifos licensing text from http://mifos.org/developers/wiki/MifosLicensing