@Test
  public void date_assertion_should_support_timestamp_string_representation()
      throws ParseException {
    Date date = DateUtil.newTimestampDateFormat().parse("2015-05-08 11:30:00.560");
    String timestampAsString =
        DateUtil.newTimestampDateFormat().format(new Timestamp(date.getTime()));

    assertThat(date).isEqualTo(timestampAsString);
  }
  @Test
  public void use_custom_date_formats_set_from_Assertions_entry_point() {
    final Date date = DateUtil.parse("2003-04-26");

    registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss");

    try {
      // fail : the registered format does not match the given date
      assertThat(date).isEqualTo("2003/04/26");
      failBecauseExpectedAssertionErrorWasNotThrown();
    } catch (AssertionError e) {
      assertThat(e)
          .hasMessage(
              format(
                  "Failed to parse 2003/04/26 with any of these date formats:%n"
                      + "   [yyyy/MM/dd'T'HH:mm:ss,%n"
                      + "    yyyy-MM-dd'T'HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd'T'HH:mm:ss,%n"
                      + "    yyyy-MM-dd]"));
    }

    // register the expected custom formats, they are used in the order they have been registered.
    registerCustomDateFormat("yyyy/MM/dd");
    assertThat(date).isEqualTo("2003/04/26");
    // another to register a DateFormat
    registerCustomDateFormat(new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss.SSS"));
    // the assertion uses the last custom date format registered.
    assertThat(date).isEqualTo("2003/04/26T00:00:00.000");

    useDefaultDateFormatsOnly();
    assertThat(date).isEqualTo("2003-04-26");
    assertThat(date).isEqualTo("2003-04-26T00:00:00");
    assertThat(date).isEqualTo("2003-04-26T00:00:00.000");
  }
 @Test
 public void
     date_assertion_using_custom_date_string_representation_then_switching_back_to_defaults_date_formats() {
   final Date date = DateUtil.parse("2003-04-26");
   // chained assertions
   assertThat(date)
       .withDateFormat("yyyy/MM/dd")
       .isEqualTo("2003/04/26")
       .withDefaultDateFormatsOnly()
       .isEqualTo("2003-04-26");
   // new assertions
   assertThat(date).withDateFormat("yyyy/MM/dd").isEqualTo("2003/04/26");
   assertThat(date).withDefaultDateFormatsOnly().isEqualTo("2003-04-26");
 }
 @Test
 public void date_assertion_using_default_date_string_representation() {
   // datetime with ms is supported
   final Date date1timeWithMS = parseDatetimeWithMs("2003-04-26T03:01:02.999");
   assertThat(date1timeWithMS).isEqualTo("2003-04-26T03:01:02.999");
   // datetime without ms is supported
   final Date datetime = parseDatetime("2003-04-26T03:01:02");
   assertThat(datetime).isEqualTo("2003-04-26T03:01:02.000");
   assertThat(datetime).isEqualTo("2003-04-26T03:01:02");
   // date is supported
   final Date date = DateUtil.parse("2003-04-26");
   assertThat(date).isEqualTo("2003-04-26");
   assertThat(date).isEqualTo("2003-04-26T00:00:00");
   assertThat(date).isEqualTo("2003-04-26T00:00:00.000");
 }
コード例 #5
0
 @Test
 public void should_create_error_message() {
   Date actual = DateUtil.parseDatetime("2011-01-01T05:01:00");
   Date expected = DateUtil.parseDatetime("2011-01-01T05:01:01");
   String message =
       shouldHaveSameTime(actual, expected)
           .create(new TextDescription("Test"), new StandardRepresentation());
   assertThat(message)
       .isEqualTo(
           format(
               "[Test] %n"
                   + "Expecting%n"
                   + "  <2011-01-01T05:01:00.000>%n"
                   + "to have the same time as:%n"
                   + "  <2011-01-01T05:01:01.000>%n"
                   + "but actual time is%n"
                   + "  <"
                   + actual.getTime()
                   + "L>%n"
                   + "and expected was:%n"
                   + "  <"
                   + expected.getTime()
                   + "L>"));
 }
  @Test
  public void use_custom_date_formats_first_then_defaults_to_parse_a_date() {
    // using default formats should work
    final Date date = DateUtil.parse("2003-04-26");
    assertThat(date).isEqualTo("2003-04-26");

    try {
      // date with a custom format : failure since the default formats don't match.
      assertThat(date).isEqualTo("2003/04/26");
      failBecauseExpectedAssertionErrorWasNotThrown();
    } catch (AssertionError e) {
      assertThat(e)
          .hasMessage(
              format(
                  "Failed to parse 2003/04/26 with any of these date formats:%n"
                      + "   [yyyy-MM-dd'T'HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd'T'HH:mm:ss,%n"
                      + "    yyyy-MM-dd]"));
    }

    // registering a custom date format to make the assertion pass
    registerCustomDateFormat("yyyy/MM/dd");
    assertThat(date).isEqualTo("2003/04/26");
    // the default formats are still available and should work
    assertThat(date).isEqualTo("2003-04-26");
    assertThat(date).isEqualTo("2003-04-26T00:00:00");

    try {
      // but if not format at all matches, it fails.
      assertThat(date).isEqualTo("2003 04 26");
      failBecauseExpectedAssertionErrorWasNotThrown();
    } catch (AssertionError e) {
      assertThat(e)
          .hasMessage(
              format(
                  "Failed to parse 2003 04 26 with any of these date formats:%n"
                      + "   [yyyy/MM/dd,%n"
                      + "    yyyy-MM-dd'T'HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd HH:mm:ss.SSS,%n"
                      + "    yyyy-MM-dd'T'HH:mm:ss,%n"
                      + "    yyyy-MM-dd]"));
    }

    // register a new custom format should work
    registerCustomDateFormat("yyyy MM dd");
    assertThat(date).isEqualTo("2003 04 26");
  }
 @Test
 public void
     should_fail_if_given_date_string_representation_cant_be_parsed_with_any_custom_date_formats() {
   thrown.expectAssertionError(
       "Failed to parse 2003 04 26 with any of these date formats:%n"
           + "   [yyyy/MM/dd'T'HH:mm:ss,%n"
           + "    yyyy/MM/dd,%n"
           + "    yyyy-MM-dd'T'HH:mm:ss.SSS,%n"
           + "    yyyy-MM-dd HH:mm:ss.SSS,%n"
           + "    yyyy-MM-dd'T'HH:mm:ss,%n"
           + "    yyyy-MM-dd]");
   final Date date = DateUtil.parse("2003-04-26");
   registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss");
   // registering again has no effect
   registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss");
   assertThat(date).withDateFormat("yyyy/MM/dd").isEqualTo("2003 04 26");
 }
 @Test
 public void date_assertion_using_custom_date_string_representation() {
   final Date date = DateUtil.parse("2003-04-26");
   assertThat(date).withDateFormat("yyyy/MM/dd").isEqualTo("2003/04/26");
   assertThat(date).isEqualTo("2003/04/26");
 }