1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor.time;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.supercsv.cellprocessor.time.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20
21 import java.time.LocalDate;
22 import java.time.format.DateTimeFormatter;
23 import java.time.format.FormatStyle;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.experimental.theories.DataPoints;
28 import org.junit.experimental.theories.Theories;
29 import org.junit.experimental.theories.Theory;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.supercsv.cellprocessor.ift.CellProcessor;
33 import org.supercsv.cellprocessor.time.mock.IdentityTransform;
34 import org.supercsv.exception.SuperCsvCellProcessorException;
35
36
37
38
39 @RunWith(Theories.class)
40 public class FmtLocalDateTest {
41
42 @DataPoints public static final LocalDate[] localDates = { LocalDate.of(2013, 10, 25) };
43
44 @DataPoints public static FmtLocalDate[] processors = { new FmtLocalDate(),
45 new FmtLocalDate(DateTimeFormatter.ISO_LOCAL_DATE), new FmtLocalDate(new IdentityTransform()),
46 new FmtLocalDate(DateTimeFormatter.ISO_LOCAL_DATE, new IdentityTransform()) };
47
48 @DataPoints public static final DateTimeFormatter[] formats = {
49 DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM), DateTimeFormatter.ofPattern("dd MM yyyy") };
50
51 @Rule public ExpectedException exception = ExpectedException.none();
52
53 @Theory
54 public void testValidDateTimeString(final FmtLocalDate p, final LocalDate localDate) {
55 assertEquals(localDate.toString(), p.execute(localDate, ANONYMOUS_CSVCONTEXT));
56 }
57
58 @Theory
59 public void testFormats(final DateTimeFormatter formatter, final LocalDate localTime) {
60 final FmtLocalDate p = new FmtLocalDate(formatter);
61 assertEquals(localTime.format(formatter), p.execute(localTime, ANONYMOUS_CSVCONTEXT));
62 }
63
64 @Theory
65 public void testNullInput(final FmtLocalDate p) {
66 exception.expect(SuperCsvCellProcessorException.class);
67 exception.expectMessage("this processor does not accept null input - "
68 + "if the column is optional then chain an Optional() processor before this one");
69 p.execute(null, ANONYMOUS_CSVCONTEXT);
70 }
71
72 @Theory
73 public void testNonLocalDateInput(final FmtLocalDate p) {
74 exception.expect(SuperCsvCellProcessorException.class);
75 exception.expectMessage("the input value should be of type java.time.LocalDate but is java.lang.Integer");
76 p.execute(123, ANONYMOUS_CSVCONTEXT);
77 }
78
79 @Test
80 public void testConstructor2WithNullNext() {
81 exception.expect(NullPointerException.class);
82 new FmtLocalDate((CellProcessor) null);
83 }
84
85 @Test
86 public void testConstructor3WithNullFormatter() {
87 exception.expect(NullPointerException.class);
88 new FmtLocalDate((DateTimeFormatter) null);
89 }
90
91 @Test
92 public void testConstructor4WithNullFormatter() {
93 exception.expect(NullPointerException.class);
94 new FmtLocalDate(null, new IdentityTransform());
95 }
96
97 @Test
98 public void testConstructor4WithNullNext() {
99 exception.expect(NullPointerException.class);
100 new FmtLocalDate(DateTimeFormatter.ISO_LOCAL_DATE, null);
101 }
102 }