package com.instantbank.common.utilcomponents;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import junit.framework.TestCase;

/**
 * @author CincoSoft
 * @j.created Feb 16, 2004
 */
public class IbCalendarTest extends TestCase {
  private IbCalendar ibc = new IbCalendar();
  SimpleDateFormat sdfn = new SimpleDateFormat("dd-MM-yyyy");
  SimpleDateFormat sdfs = new SimpleDateFormat("dd-MMM-yyyy");
  
  /**
   * Next date having a fixed month day.
   *
   */
  public void testNextDateWithMonthDay() {
    try {
        
      Date next;
      Date first = sdfn.parse("02-02-2004");
      next = ibc.getNextDateWithMonthDay(first,30);
      assertEquals(next,sdfn.parse("30-03-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDay(first,30);
      assertEquals(next,sdfn.parse("30-03-2004"));
            
      first = sdfn.parse("25-01-2004");
      next = ibc.getNextDateWithMonthDay(first,31);
      assertEquals(next,sdfn.parse("31-01-2004"));
      
      first = sdfn.parse("25-01-2004");
      next = ibc.getNextDateWithMonthDay(first,16);
      assertEquals(next,sdfn.parse("16-02-2004"));

    }
    catch (Exception e) {
      fail(e.getMessage());
    }
  }
  
  /**
   * Next date having a fixed month day or truncated to last day of the
   * month y the day is not present in the next month.
   *
   */
  public void testNextDateWithMonthDayTruncated() {
        
    try {
      Date next;
      Date first = sdfn.parse("02-02-2004");
      next = ibc.getNextDateWithMonthDayTruncated(first,30);
      assertEquals(next,sdfn.parse("30-03-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDayTruncated(first,30);
      assertEquals(next,sdfn.parse("29-02-2004"));
            
      first = sdfn.parse("25-01-2004");
      next = ibc.getNextDateWithMonthDayTruncated(first,31);
      assertEquals(next,sdfn.parse("31-01-2004"));
      
      first = sdfn.parse("25-01-2004");
      next = ibc.getNextDateWithMonthDayTruncated(first,16);
      assertEquals(next,sdfn.parse("16-02-2004"));
    }
    catch (ParseException e) {
      fail(e.getMessage());
    }

  }
  
  public void testNextDateWithMonthDayAndGap(){
    try {
      Date first,next;
      
      first = sdfn.parse("25-12-2003");
      next = ibc.getNextDateWithMonthDayAndGap(first,5,IbCalendar.MONTH);
      assertEquals(next,sdfn.parse("05-01-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDayAndGap(first,30,IbCalendar.MONTH);
      assertEquals(next,sdfn.parse("29-02-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDayAndGap(first,30,IbCalendar.QUARTER);
      assertEquals(next,sdfn.parse("30-04-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDayAndGap(first,30,IbCalendar.SEMESTER);
      assertEquals(next,sdfn.parse("30-07-2004"));
      
      first = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithMonthDayAndGap(first,5,IbCalendar.SEMESTER);
      assertEquals(next,sdfn.parse("05-07-2004"));
     
    }
    catch (ParseException e) {
      fail(e.getMessage());
    }
  }
  
  public void testGetNextDateWithWeekDayAndGap(){
    Date start, next;
    try {
      start = sdfn.parse("31-01-2004");
      next = ibc.getNextDateWithWeekDayAndGap(start,Calendar.SUNDAY);
      assertEquals(next,sdfn.parse("01-02-2004"));
      
      start = sdfn.parse("29-01-2004");
      next = ibc.getNextDateWithWeekDayAndGap(start,Calendar.THURSDAY);
      assertEquals(next,sdfn.parse("05-02-2004"));
      
      start = sdfn.parse("29-01-2004");
      next = ibc.getNextDateWithWeekDayAndGap(start,Calendar.SATURDAY);
      assertEquals(next,sdfn.parse("07-02-2004"));
      
      start = sdfn.parse("27-12-2004");
      next = ibc.getNextDateWithWeekDayAndGap(start,Calendar.MONDAY);
      assertEquals(next,sdfn.parse("03-01-2005"));
            
    }
    catch (ParseException e) {
      fail(e.getMessage());
    }
    
  }
  
  /**
   * Tests how to calculate the number of days in year.
   *
   */
  public void testNumDaysInYear(){
    Date d;
    int maxYearDay;
    try {

      d = sdfn.parse("09-03-2004");
      assertTrue(ibc.numOfDaysInYear(d)==366);

      d = sdfn.parse("09-03-2003");
      assertTrue(ibc.numOfDaysInYear(d)==365);
      
    }
    catch (ParseException e) {
      fail(e.getMessage());
    }
  }
  
  public void testDifferenceInDays(){
    Date base, goal;
    
    try{
      base = sdfn.parse("26-03-2004");
      goal = sdfn.parse("20-03-2004");
      assertTrue(ibc.differenceInDays(base,goal)==6);
      assertTrue(ibc.differenceInDays(goal,base)==-6);

      base = sdfn.parse("26-03-2004");
      goal = sdfn.parse("26-03-2003");
      assertTrue(ibc.differenceInDays(base,goal)==366);
      assertTrue(ibc.differenceInDays(goal,base)==-366);
      
      base = sdfn.parse("26-02-2004");
      goal = sdfn.parse("26-02-2003");
      assertTrue(ibc.differenceInDays(base,goal)==365);
      assertTrue(ibc.differenceInDays(goal,base)==-365);
      
    }
    catch(ParseException e){
      fail(e.getMessage());
    }
  }
  

}