package com.google.gdata.data; import static org.junit.Assert.*; import org.junit.Test; import com.google.gdata.util.ParseException; public class TestDateTime { private static String invalidDate = "abort"; private static String validDate = "2002-09-24"; private static String validDateWithTimezone = "2002-09-24-00:01"; private static String validDateTime = "2002-09-24T06:00:00.0"; private static String validRfcString = "Wed, 02 Oct 2002 13:00:00 GMT"; @Test public void parseDateTest() { boolean numberFormatException = false; try { DateTime.parseDate(invalidDate); } catch(NumberFormatException e) { numberFormatException = true; } assertTrue(numberFormatException); DateTime dt = DateTime.parseDate(validDate); assertNull(dt.tzShift); dt = DateTime.parseDate(validDateWithTimezone); assertEquals(dt.tzShift.intValue(), -1); } @Test public void parseDateTimeTest() { boolean numberFormatException = false; try { DateTime.parseDateTime(invalidDate); } catch(NumberFormatException e) { numberFormatException = true; } assertTrue(numberFormatException); numberFormatException = false; DateTime dt = null; try { dt = DateTime.parseDateTime(validDate); } catch(NumberFormatException e) { numberFormatException = true; } assertTrue(numberFormatException); assertNull(dt); dt = DateTime.parseDateTime(validDateTime); assertNotNull(dt); } @Test public void parseDateTimeChoiceTest() { boolean numberFormatException = false; try { DateTime.parseDateTimeChoice(invalidDate); } catch(NumberFormatException e) { numberFormatException = true; } assertTrue(numberFormatException); DateTime dt = DateTime.parseDateTimeChoice(validDate); assertNull(dt.tzShift); dt = DateTime.parseDateTimeChoice(validDateTime); assertNotNull(dt); } @Test public void parseRfc822Test() { boolean exception = false; try { DateTime.parseRfc822(validDate); } catch(ParseException e) { exception = true; } assertTrue(exception); exception = false; try { DateTime.parseRfc822(validRfcString); } catch (ParseException e) { exception = true; } assertFalse(exception); } @Test public void compareToTest() { DateTime dt1 = DateTime.parseDate("2002-09-24"); DateTime dt2 = DateTime.parseDate("2002-09-24"); assertTrue(dt1.compareTo(dt2) == 0); dt2 = DateTime.parseDate("2002-09-23"); assertTrue(dt1.compareTo(dt2) > 0); dt2 = DateTime.parseDate("2002-09-26"); assertTrue(dt1.compareTo(dt2) < 0); } @Test public void toUiStringTest() { String dateString = "2002-09-24"; String dateTimeString = "2002-09-24T06:00:00"; String expectedDateTimeString = "2002-09-24 06:00"; DateTime dt = DateTime.parseDate(dateString); assertEquals(dateString, dt.toUiString()); dt = DateTime.parseDateTime(dateTimeString); assertEquals(expectedDateTimeString, dt.toUiString()); } @Test public void toStringRfc822Test() { String dateTimeString = "2002-09-24T06:00:00"; String expectedDateTimeString = "Tue, 24 Sep 2002 06:00:00 +0000"; DateTime dt = DateTime.parseDateTime(dateTimeString); assertEquals(expectedDateTimeString, dt.toStringRfc822()); } }