package test;

import java.util.HashMap;
import java.util.Map;

import name.jenkins.john.joda.time.format.ISOW3CDateTimeFormat;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

/**
 * <p>An implementation of the W3C's specification of the ISO8601 date-time
 * format.</p>
 * 
 * <p>Optimally, these would be wrapped into their own DateTimeFormat and
 * DateTimeFormatter classes; however, I did not have time to do this and had
 * to use this for the time being.</p>
 *
 * @author John Jenkins
 */
public class Test {
	/**
	 * Tests that the parse() function works for all W3C date-time formats.
	 * 
	 * @param args Ignored.
	 * 
	 * @throws Exception IllegalStateException One of the tests failed.
	 */
	public static void test(final DateTimeFormatter formatter) {
		DateTimeZone dateTimeZoneUtc = DateTimeZone.UTC;
		DateTimeZone dateTimeZoneLocal = 
			DateTimeZone.forOffsetHoursMinutes(-7, 0);
		
		Map<String, DateTime> tests = new HashMap<String, DateTime>();
		tests.put("1970", new DateTime(0, dateTimeZoneUtc));
		tests.put("1970-01", new DateTime(0, dateTimeZoneUtc));
		tests.put("1970-01-01", new DateTime(0, dateTimeZoneUtc));
		tests
			.put(
				"1969-12-31T17:00-07:00", 
				new DateTime(0, dateTimeZoneLocal));
		tests
			.put(
				"1969-12-31T17:00:00-07:00", 
				new DateTime(0, dateTimeZoneLocal));
		tests
			.put(
				"1969-12-31T17:00:00.000-07:00", 
				new DateTime(0, dateTimeZoneLocal));
		
		DateTimeFormatter printer = ISODateTimeFormat.dateTime();
		for(String test : tests.keySet()) {
			DateTime expected = tests.get(test);
			DateTime actual = formatter.parseDateTime(test);
			
			if(expected.getMillis() != actual.getMillis()) {
				throw 
					new IllegalStateException(
						"Fail (different moments in time): " + test + "\n" +
							"\tExpected: " + printer.print(expected) + 
								" (" + expected.getMillis() + ")\n" +
							"\tActual: " + printer.print(actual) +
								" (" + actual.getMillis() + ")\n");
			}
			else if(expected.getZone() != actual.getZone()) {
				throw 
					new IllegalStateException(
						"Fail (different time zones): " + test + "\n" +
							"\tExpected: " + printer.print(expected) + 
								" (" + expected.getZone() + ")\n" +
							"\tActual: " + printer.print(actual) +
								" (" + actual.getZone() + ")\n");
			}
		}
		
		// The test completed successfully.
	}
	
	public static void main(String args[]) {
		// TODO: Creating tests for all of the formatters.
		test(ISOW3CDateTimeFormat.any());
	}
}