示例#1
0
void
MonthDayRuleTest::test_utilities()
{
	int yearIndex;
	SetDate date;
	Year year ( DateUtil::year());

	Date newYearDate (year, Month(1), DayOfMonth(1));
	Date july4Date (year, Month(7), DayOfMonth(4));
	Date christmasDate(year, Month(12), DayOfMonth(25));


	//test: MontDayRule::virtual Date calcDate(const Year& year) const ;
	MonthDayRule newYearRule ("Rule", Month(1), DayOfMonth(1));
	date = newYearRule.calcDate(year);
	CPPUNIT_ASSERT(newYearDate == *date.begin());
	for (yearIndex = year.getValue(); yearIndex <= year.getValue() + 50; ++yearIndex){
		date = newYearRule.calcDate(Year(yearIndex));
		CPPUNIT_ASSERT(newYearDate == *date.begin());
		newYearDate.addYear(1);
	}
	
	MonthDayRule july4Rule ("July4Rule", Month(7), DayOfMonth(4));
	date = july4Rule.calcDate(year);
	CPPUNIT_ASSERT(july4Date == *date.begin());

	MonthDayRule christmasRule("ChristmasRule", Month(12), DayOfMonth(25));
	date = christmasRule.calcDate(year);
	CPPUNIT_ASSERT(christmasDate == *date.begin());

	//Test for weekendAdjustment
	WeekendRuleSharedPtr weekendRulePtr(new WeekendRule("WeekendRule"));
	weekendRulePtr->addDay(WeekDay::Saturday);
	weekendRulePtr->addDay(WeekDay::Sunday);
	MonthDayRule adjustingNewYearRule ("AdjustingNewYear", Month(1), DayOfMonth(1));
	adjustingNewYearRule.setStartEffectiveDate(20110101);
	adjustingNewYearRule.setWeekendAdjustment(WeekendAdjustment::ClosestWeekDay);
	adjustingNewYearRule.setWeekendRule(weekendRulePtr);

	//First check for Saturday -> prior Friday
	date = adjustingNewYearRule.calcDate(Year(2011));
	CPPUNIT_ASSERT(Date(20101231) == *date.begin());

	//First check for Sunday -> next Monday
	date = adjustingNewYearRule.calcDate(Year(2012));
	CPPUNIT_ASSERT(Date(20120102) == *date.begin());

	//Check for no adjustment is required
	date = adjustingNewYearRule.calcDate(Year(2013));
	CPPUNIT_ASSERT(Date(20130101) == *date.begin());


	//Rule is disabled
	try{
		newYearRule.setEnabledFlag(false);
		newYearRule.calcDate(year);
	}
	catch (BaseException& ex){
		CAUGHT_EXCEPTION(ex,"Calculation called on disabled rule");
	}
	newYearRule.setEnabledFlag(true);
	 

	//Rule start date year > calc year
	try{
		newYearRule.calcDate(Year(2010));
	}
	catch (BaseException& ex){
		CAUGHT_EXCEPTION(ex,"Calculation called for year before  rule is in effect");
	}

	//Rule calc year >  rule end date year
	try{
		newYearRule.setEndEffectiveDate(20121231);
		newYearRule.calcDate(Year(2030));
	}
	catch (BaseException& ex){
		CAUGHT_EXCEPTION(ex,"Calculation called for year after  rule is in effect");
	}

}