package io.ljunggren.csv.parser.mapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import io.ljunggren.csv.parser.annotation.CSVDate;
public class DateMapperTest {
@CSVDate(pattern = "MM-dd-yyyy")
Date date;
@SuppressWarnings("unused")
String name;
@Test
public void mapTest() throws Exception {
Field field = this.getClass().getDeclaredField("date");
Object object = new DateMapper().map(field, "05-04-2021");
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) object);
assertEquals(4, calendar.get(Calendar.DATE));
assertEquals(2021, calendar.get(Calendar.YEAR));
}
@Test(expected = Exception.class)
public void mapExceptionTest() throws Exception {
Field field = this.getClass().getDeclaredField("date");
new DateMapper().map(field, "not a date");
}
@Test
public void delegatesToNextChainWhenAnnotationAbsent() throws Exception {
Field field = this.getClass().getDeclaredField("name");
DateMapper mapper = new DateMapper();
mapper.nextChain(new CatchAllMapper());
Object result = mapper.map(field, "hello");
assertEquals("hello", result);
}
@Test
public void returnsNullWhenAnnotationAbsentAndNoNextChain() throws Exception {
Field field = this.getClass().getDeclaredField("name");
Object result = new DateMapper().map(field, "ignored");
assertNull(result);
}
}