Package com.skaringa.util.test

Source Code of com.skaringa.util.test.ISO8601DateTimeFormatTestClass

package com.skaringa.util.test;

import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.skaringa.util.ISO8601DateTimeFormat;

/**
* JUnit test case for ISO8601DateTimeFormat.
*/
public final class ISO8601DateTimeFormatTestClass extends TestCase {
  private static final Class THIS = ISO8601DateTimeFormatTestClass.class;

  /**
   * @see TestCase#TestCase(java.lang.String)
   */
  public ISO8601DateTimeFormatTestClass(String name) {
    super(name);
  }

  /**
   * @return TestSuite
   */
  public static Test suite() {
    return new TestSuite(THIS);
  }

  /**
   * Test the formatting.
   */
  public void testFormat() {
    TimeZone tz = TimeZone.getTimeZone("GMT+01");
    Calendar cal = Calendar.getInstance(tz);
    cal.set(1962, Calendar.JANUARY, 19, 8, 40, 0);
    ISO8601DateTimeFormat formatter = new ISO8601DateTimeFormat(tz);

    String res = formatter.format(cal.getTime());

    assertEquals(
      "ISO 8601 date formatter produces wrong output",
      "1962-01-19T08:40:00+01:00",
      res);
  }

  /**
   * Test the formatting and parsing
   */
  public void testFormatAndParse() {
    Calendar cal = new GregorianCalendar(1962, 0, 19, 8, 40);
    Date myBirthDay = cal.getTime();
    ISO8601DateTimeFormat formatter = new ISO8601DateTimeFormat();
    String serial = formatter.format(myBirthDay);

    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",
      myBirthDay,
      parsedDate);
  }

  /**
   * Test the parsing of several dateTime strings.
   * @throws Exception If the test failes.
   */
  public void testParse() throws Exception {
    Calendar cal = Calendar.getInstance();

    ISO8601DateTimeFormat formatter = new ISO8601DateTimeFormat();

    // east
    Date parsedDate = formatter.parse("1970-01-01T01:00:00+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));
    assertEquals(1, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, cal.get(Calendar.MINUTE));
    assertEquals(0, cal.get(Calendar.SECOND));

    // west
    parsedDate = formatter.parse("1969-12-31T19:00:02-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));
    assertEquals(19, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, cal.get(Calendar.MINUTE));
    assertEquals(2, cal.get(Calendar.SECOND));

    // GMT
    parsedDate = formatter.parse("2003-03-18T10:30:02Z");
    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));
    assertEquals(10, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(30, cal.get(Calendar.MINUTE));
    assertEquals(2, cal.get(Calendar.SECOND));

    // default TZ
    parsedDate = formatter.parse("1999-07-28T19:27:02");
    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));
    assertEquals(19, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(27, cal.get(Calendar.MINUTE));
    assertEquals(2, cal.get(Calendar.SECOND));
  }

  /**
   * Test the parsing of dateTimes without seconds.
   * @throws Exception If the test failes.
   */
  public void testParseWithoutSeconds() throws Exception {
    Calendar cal = Calendar.getInstance();

    ISO8601DateTimeFormat formatter = new ISO8601DateTimeFormat();

    // GMT
    Date parsedDate = formatter.parse("2003-04-01T12:34Z");
    cal.setTime(parsedDate);
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    assertEquals(2003, cal.get(Calendar.YEAR));
    assertEquals(Calendar.APRIL, cal.get(Calendar.MONTH));
    assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
    assertEquals(12, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(34, cal.get(Calendar.MINUTE));
    assertEquals(0, cal.get(Calendar.SECOND));

    // east
    parsedDate = formatter.parse("2003-04-01T12:34-05:00");
    cal.setTime(parsedDate);
    cal.setTimeZone(TimeZone.getTimeZone("GMT-05"));
    assertEquals(2003, cal.get(Calendar.YEAR));
    assertEquals(Calendar.APRIL, cal.get(Calendar.MONTH));
    assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
    assertEquals(12, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(34, cal.get(Calendar.MINUTE));
    assertEquals(0, cal.get(Calendar.SECOND));

    // default TZ
    parsedDate = formatter.parse("2003-04-01T12:34");
    cal.setTime(parsedDate);
    cal.setTimeZone(TimeZone.getDefault());
    assertEquals(2003, cal.get(Calendar.YEAR));
    assertEquals(Calendar.APRIL, cal.get(Calendar.MONTH));
    assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
    assertEquals(12, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(34, cal.get(Calendar.MINUTE));
    assertEquals(0, cal.get(Calendar.SECOND));
  }

  /**
   * Test reporting of parsing error
   */
  public void testParseException() {
    String wrong = "1999-mar-01T00:00:00Z";
    ISO8601DateTimeFormat formatter = new ISO8601DateTimeFormat();

    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());
  }

  /**
   * Test handling of time zones.
   * @throws Exception If the test failes.
   */
  public void testTimeZone() throws Exception {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+00"));
    cal.set(2000, 0, 1, 0, 0, 0);
    Date gmt = cal.getTime();
    ISO8601DateTimeFormat formatter =
      new ISO8601DateTimeFormat(TimeZone.getTimeZone("GMT+00"));
    String gmtStr = formatter.format(gmt);
    assertEquals(
      "ISO 8601 date handling of timezones failed.",
      "2000-01-01T00:00:00Z",
      gmtStr);

    ISO8601DateTimeFormat parser =
      new ISO8601DateTimeFormat(TimeZone.getTimeZone("GMT+01"));
    Date met = parser.parse(gmtStr);
    String metStr = parser.format(met);

    assertEquals(
      "ISO 8601 date handling of timezones failed.",
      "2000-01-01T01:00:00+01:00",
      metStr);
  }
}
TOP

Related Classes of com.skaringa.util.test.ISO8601DateTimeFormatTestClass

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.