package org.test4j.hamcrest.matcher.calendar;
import java.util.Calendar;
import org.test4j.module.Test4JException;
import org.test4j.testng.Test4J;
import org.test4j.tools.commons.DateUtilTest;
import org.testng.annotations.Test;
import ext.test4j.hamcrest.MatcherAssert;
@Test(groups = { "test4j", "assertion" })
@SuppressWarnings("unchecked")
public class DateFormatMatcherTest extends Test4J {
static Calendar cal = DateUtilTest.mockCalendar(2010, 1, 3);
public void test_DateFormat() {
DateFormatMatcher matcher = new DateFormatMatcher("yyyy-MM-dd", "2010-01-03");
MatcherAssert.assertThat(cal, matcher);
}
public void test_DateFormat_Message() {
DateFormatMatcher matcher = new DateFormatMatcher("yyyy-MM-dd", "2010-01-02");
try {
MatcherAssert.assertThat(cal, matcher);
want.fail("之前应该已经抛出异常");
} catch (Throwable e) {
String error = e.getMessage();
want.string(error).contains("2010-01-02").contains("yyyy-MM-dd").contains("2010-01-03");
}
}
@Test(expectedExceptions = Test4JException.class)
public void test_ExpectedNull() {
new DateFormatMatcher("yyyy-MM-dd", null);
}
@Test(expectedExceptions = Test4JException.class)
public void test_ActualIsNull() {
DateFormatMatcher matcher = new DateFormatMatcher("yyyy-MM-dd", "2010-01-02");
MatcherAssert.assertThat(null, matcher);
}
public void test_fomat_Exception() {
try {
new DateFormatMatcher("yyyy-xx-dd", "2010-01-02");
want.fail("之前应该已经抛出异常");
} catch (Throwable e) {
String message = e.getMessage();
want.string(message).isEqualTo("illegal date fomat[yyyy-xx-dd].");
}
}
}