package org.treesched;

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

import junit.framework.TestCase;

/**
 * 
 */
public class DateUtilsTest extends TestCase {

	static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
	static SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MMM-yy HH:mm");

	/**
	 * Constructor for DateUtilsTest.
	 * @param arg0
	 */
	public DateUtilsTest(String arg0) {
		super(arg0);
	}

	static Date parseDate(String s) {
		try {
			return dateFormat.parse(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static Date parseTime(String s) {		
		try {
			return timeFormat.parse(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 * Test for Date startOfDay(Date)
	 */
	final public void testStartOfDayDate() {
		assertTrue(DateUtils.startOfDay(
			parseTime("11-jul-03 12:27")).equals(parseDate("11-jul-03")));
	}

	/*
	 * Test for Date endOfDay(Date)
	 */
	final public void testEndOfDayDate() {
		assertTrue(DateUtils.endOfDay(
			parseTime("11-jul-03 12:27")).equals(
				DateUtils.oneMillisBefore(parseDate("12-jul-03"))));
	}

	/*
	 * Test for int dayOfWeek(Date)
	 */
	final public void testDayOfWeekDate() {

		assertTrue(DateUtils.dayOfWeek(
			parseTime("11-jul-03 12:27")) == 6);
	}

	/*
	 * Test for int dayOfMonth(Date)
	 */
	final public void testDayOfMonthDate() {

		assertTrue(DateUtils.dayOfMonth(
			parseTime("11-jul-03 12:27")) == 11);
	}

	/*
	 * Test for Date startOfMonth(Date)
	 */
	final public void testStartOfMonthDate() {
		assertTrue(DateUtils.startOfMonth(
			parseTime("11-jul-03 12:27")).equals(
				parseDate("1-jul-03")));
	}


	/*
	 * Test for Date endOfMonth(Date)
	 */
	final public void testEndOfMonthDate() {
		assertTrue(DateUtils.endOfMonth(
			parseTime("11-jul-03 12:27")).equals(
				DateUtils.oneMillisBefore(parseDate("1-aug-03"))));
	}

	/*
	 * Test for Date startOfYear(Date)
	 */
	final public void testStartOfYearDate() {
		assertTrue(DateUtils.startOfYear(
			parseTime("11-jul-03 12:27")).equals(
				parseDate("1-jan-03")));
	}


	/*
	 * Test for Date endOfMonth(Date)
	 */
	final public void testEndOfYearDate() {
		assertTrue(DateUtils.endOfYear(
			parseTime("11-jul-03 12:27")).equals(
				DateUtils.oneMillisBefore(parseDate("1-jan-04"))));
	}

}
