package com.skaringa.util.test;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.TimeZone;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.skaringa.util.ISO8601DateFormat;
/**
* JUnit test case for ISO8601DateFormat.
*/
public final class ISO8601DateFormatTestClass extends TestCase {
private static final Class THIS = ISO8601DateFormatTestClass.class;
/**
* @see TestCase#TestCase(java.lang.String)
* @param name
*/
public ISO8601DateFormatTestClass(String name) {
super(name);
}
/**
* @return TestSuite.
*/
public static Test suite() {
return new TestSuite(THIS);
}
/**
* Test the formatting
*/
public void testFormat() {
Calendar cal = new GregorianCalendar(1962, 0, 19);
cal.setTimeZone(TimeZone.getTimeZone("GMT+01"));
ISO8601DateFormat formatter = new ISO8601DateFormat();
String res = formatter.format(cal.getTime());
assertEquals(
"ISO 8601 date formatter produces wrong output",
"1962-01-19",
res);
}
/**
* Test the formatting and parsing of some dates
*/
public void testFormatAndParse() {
Collection calendars = new ArrayList();
calendars.add(new GregorianCalendar(1962, 0, 19));
calendars.add(new GregorianCalendar(20, 1, 1));
calendars.add(new GregorianCalendar(2003, 10, 1));
ISO8601DateFormat formatter = new ISO8601DateFormat();
Iterator it = calendars.iterator();
while (it.hasNext()) {
Calendar cal = (Calendar) it.next();
Date date = cal.getTime();
String serial = formatter.format(date);
ParsePosition pos = new ParsePosition(0);
Date parsedDate = formatter.parse(serial, pos);
assertNotNull(
"Parsing of " + serial + " failed at position " + pos.getIndex(),
parsedDate);
assertEquals(
"ISO 8601 date parser produces wrong result",
date,
parsedDate);
}
}
/**
* Test the parsing of several date strings.
* @throws Exception If the test failes.
*/
public void testParse() throws Exception {
Calendar cal = Calendar.getInstance();
ISO8601DateFormat formatter = new ISO8601DateFormat();
// east
Date parsedDate = formatter.parse("1970-01-01+01:00");
cal.setTime(parsedDate);
cal.setTimeZone(TimeZone.getTimeZone("GMT+01"));
assertEquals(1970, cal.get(Calendar.YEAR));
assertEquals(Calendar.JANUARY, cal.get(Calendar.MONTH));
assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
// west
parsedDate = formatter.parse("1969-12-31-05:00");
cal.setTime(parsedDate);
cal.setTimeZone(TimeZone.getTimeZone("GMT-05"));
assertEquals(1969, cal.get(Calendar.YEAR));
assertEquals(Calendar.DECEMBER, cal.get(Calendar.MONTH));
assertEquals(31, cal.get(Calendar.DAY_OF_MONTH));
// GMT
parsedDate = formatter.parse("2003-03-18Z");
cal.setTime(parsedDate);
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
assertEquals(2003, cal.get(Calendar.YEAR));
assertEquals(Calendar.MARCH, cal.get(Calendar.MONTH));
assertEquals(18, cal.get(Calendar.DAY_OF_MONTH));
// no TZ
parsedDate = formatter.parse("1999-07-28");
cal.setTime(parsedDate);
cal.setTimeZone(TimeZone.getDefault());
assertEquals(1999, cal.get(Calendar.YEAR));
assertEquals(Calendar.JULY, cal.get(Calendar.MONTH));
assertEquals(28, cal.get(Calendar.DAY_OF_MONTH));
}
/**
* Test reporting of parsing error.
*/
public void testParseException() {
String wrong = "1999-mar-01";
ISO8601DateFormat formatter = new ISO8601DateFormat();
ParsePosition pos = new ParsePosition(0);
Date res = formatter.parse(wrong, pos);
assertNull("A null value should have been returned.", res);
assertEquals(
"The wrong parse position is reported in case of error.",
5,
pos.getErrorIndex());
}
}