package br.com.caelum.vraptor.converter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.fail;
import java.time.LocalDateTime;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
/**
* Tests to {@link LocalDateTimeConverter}.
*/
public class LocalDateTimeConverterTest {
private LocalDateTimeConverter converter;
@Before
public void setup() {
converter = new LocalDateTimeConverter(new Locale("pt", "BR"));
}
@Test
public void shouldBeAbleToConvert() {
assertThat(converter.convert("05/06/2010 03:38:01", LocalDateTime.class),
is(equalTo(LocalDateTime.of(2010, 6, 5, 3, 38, 1))));
}
@Test
public void shouldBeAbleToConvertEmpty() {
assertThat(converter.convert("", LocalDateTime.class), is(nullValue()));
}
@Test
public void shouldBeAbleToConvertNull() {
assertThat(converter.convert(null, LocalDateTime.class), is(nullValue()));
}
@Test
public void shouldThrowExceptionWhenUnableToParse() {
try {
converter.convert("a,10/06/2008/a/b/c", LocalDateTime.class);
fail("Should throw an exception");
} catch (ConversionException e) {
e.getValidationMessage().setBundle(new MockResourceBundle());
assertThat(e.getValidationMessage().getMessage(), is("is_not_a_valid_datetime"));
}
}
}