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.util.Locale;
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 ParseLocalDateTest {
41
42 @DataPoints public static final LocalDate[] localDates = { LocalDate.of(2013, 10, 25) };
43 @DataPoints public static final DateTimeFormatter[] formats = { DateTimeFormatter.ISO_LOCAL_DATE,
44 DateTimeFormatter.ofPattern("eee MMM dd yyyy"), DateTimeFormatter.ofPattern("eee MMM dd yyyy", Locale.CHINA) };
45 @DataPoints public static ParseLocalDate[] processors = { new ParseLocalDate(),
46 new ParseLocalDate(DateTimeFormatter.ISO_LOCAL_DATE), new ParseLocalDate(new IdentityTransform()),
47 new ParseLocalDate(DateTimeFormatter.ISO_LOCAL_DATE, new IdentityTransform()) };
48
49 @Rule public ExpectedException exception = ExpectedException.none();
50
51 @Theory
52 public void testValidLocalDate(final ParseLocalDate p, final LocalDate localDate) {
53 assertEquals(localDate, p.execute(localDate.toString(), ANONYMOUS_CSVCONTEXT));
54 }
55
56 @Theory
57 public void testFormats(final LocalDate localDate, final DateTimeFormatter formatter) {
58 final ParseLocalDate p = new ParseLocalDate(formatter);
59 final ParseLocalDate pNext = new ParseLocalDate(formatter, new IdentityTransform());
60 assertEquals(localDate, p.execute(localDate.format(formatter), SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
61 assertEquals(localDate, pNext.execute(localDate.format(formatter), SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
62 }
63
64 @Theory
65 public void testNullInput(final ParseLocalDate 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 testNonStringInput(final ParseLocalDate p) {
74 exception.expect(SuperCsvCellProcessorException.class);
75 exception.expectMessage("the input value should be of type java.lang.String but is java.lang.Integer");
76 p.execute(123, ANONYMOUS_CSVCONTEXT);
77 }
78
79 @Theory
80 public void testUnparsableString(final ParseLocalDate p) {
81 exception.expect(SuperCsvCellProcessorException.class);
82 exception.expectMessage("Failed to parse value");
83 p.execute("not valid", ANONYMOUS_CSVCONTEXT);
84 }
85
86 @Test(expected = NullPointerException.class)
87 public void testConstructor2WithNullNext() {
88 new ParseLocalDate((CellProcessor) null);
89 }
90
91 @Test(expected = NullPointerException.class)
92 public void testConstructor3WithNullFormatter() {
93 new ParseLocalDate((DateTimeFormatter) null);
94 }
95
96 @Test(expected = NullPointerException.class)
97 public void testConstructor4WithNullFormatter() {
98 new ParseLocalDate(null, new IdentityTransform());
99 }
100
101 @Test(expected = NullPointerException.class)
102 public void testConstructor4WithNullNext() {
103 new ParseLocalDate(DateTimeFormatter.ISO_LOCAL_DATE, null);
104 }
105
106 }