package test.at.gv.egovernment.moa.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import junit.framework.TestCase; import at.gv.egovernment.moa.util.DateTimeUtils; /** * @author Patrick Peck * @version $Id$ */ public class DateTimeUtilsTest extends TestCase { /** * Constructor for DateTimeUtilsTest. * @param arg0 */ public DateTimeUtilsTest(String arg0) { super(arg0); } public void testParseDateTimeValid() throws Exception { Date date; DateFormat format = SimpleDateFormat.getDateTimeInstance(); String dateStr; format.setTimeZone(TimeZone.getTimeZone("GMT")); date = DateTimeUtils.parseDateTime("+1971-12-12T06:30:15"); date.setTime(date.getTime() + TimeZone.getDefault().getRawOffset()); dateStr = format.format(date); assertEquals("12.12.1971 06:30:15", dateStr); date = DateTimeUtils.parseDateTime("2000-01-01T23:59:59.012Z"); dateStr = format.format(date); assertEquals("01.01.2000 23:59:59", dateStr); date = DateTimeUtils.parseDateTime("2003-05-20T12:17:30-05:00"); dateStr = format.format(date); assertEquals("20.05.2003 17:17:30", dateStr); date = DateTimeUtils.parseDateTime("2002-02-02T02:02:02.33+04:30"); dateStr = format.format(date); assertEquals("01.02.2002 21:32:02", dateStr); } public void testParseDateTimeInvalid() { try { DateTimeUtils.parseDateTime("+1971-12-12T6:30:15"); fail(); } catch (ParseException e) { } try { DateTimeUtils.parseDateTime("2000-01-0123:59:59.999999Z"); fail(); } catch (ParseException e) { } try { DateTimeUtils.parseDateTime("2003-05-20T12:17:3005:00"); fail(); } catch (ParseException e) { } try { DateTimeUtils.parseDateTime(" 2002-02-02T02:02:02.33+04:00"); fail(); } catch (ParseException e) { } } public void testBuildDateTimeGMTMinus3() { String should = "2002-01-01T01:01:01-03:00"; doTestBuildDateTime(2002, 1, 1, 1, 1, 1, "GMT-03:00", should); } public void testBuildDateTimeMEZSommerzeit() { String should = "2002-07-31T23:59:59+02:00"; doTestBuildDateTime(2002, 7, 31, 23, 59, 59, "GMT+01:00", should); } public void testBuildDateTimeGMT() { String should = "2002-01-01T01:01:01"; doTestBuildDateTime(2002, 1, 1, 1, 1, 1, "GMT+00:00", should); } private void doTestBuildDateTime( int year, int month, int day, int hour, int min, int sec, String timeZone, String dateTimeShould) { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(timeZone)); cal.set(year,month, day, hour, min, sec); cal.set(Calendar.MILLISECOND, 0); String dateTimeBuilt = DateTimeUtils.buildDateTime(cal); assertEquals(dateTimeShould, dateTimeBuilt); } }