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
20 import java.time.LocalDateTime;
21 import java.time.format.DateTimeFormatter;
22 import java.util.Locale;
23
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.experimental.theories.DataPoints;
27 import org.junit.experimental.theories.Theories;
28 import org.junit.experimental.theories.Theory;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.supercsv.cellprocessor.ift.CellProcessor;
32 import org.supercsv.cellprocessor.time.mock.IdentityTransform;
33 import org.supercsv.exception.SuperCsvCellProcessorException;
34
35
36
37
38 @RunWith(Theories.class)
39 public class ParseLocalDateTimeTest {
40
41 @Rule public ExpectedException exception = ExpectedException.none();
42
43 @DataPoints public static final LocalDateTime[] localDateTimes = { LocalDateTime.of(2013, 10, 25, 1, 2, 3) };
44
45 @DataPoints public static final DateTimeFormatter[] formats = { DateTimeFormatter.ISO_LOCAL_DATE_TIME,
46 DateTimeFormatter.ofPattern("eee MMM dd yyyy HH:mm:ss"),
47 DateTimeFormatter.ofPattern("eee MMM dd yyyy HH:mm:ss", Locale.CHINA) };
48
49 @DataPoints public static ParseLocalDateTime[] processors = { new ParseLocalDateTime(),
50 new ParseLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME, new IdentityTransform()),
51 new ParseLocalDateTime(new IdentityTransform()),
52 new ParseLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME) };
53
54 @Theory
55 public void testValidLocalDateTime(final ParseLocalDateTime p, final LocalDateTime localDateTime) {
56 assertEquals(localDateTime, p.execute(localDateTime.toString(), SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
57 }
58
59 @Theory
60 public void testFormats(final LocalDateTime localDateTime, final DateTimeFormatter formatter) {
61 final ParseLocalDateTime p = new ParseLocalDateTime(formatter);
62 assertEquals(localDateTime, p.execute(localDateTime.format(formatter), SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
63 }
64
65 @Theory
66 public void testNullInput(final ParseLocalDateTime p) {
67 exception.expect(SuperCsvCellProcessorException.class);
68 exception.expectMessage("this processor does not accept null input - "
69 + "if the column is optional then chain an Optional() processor before this one");
70 p.execute(null, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
71 }
72
73 @Theory
74 public void testNonStringInput(final ParseLocalDateTime p) {
75 exception.expect(SuperCsvCellProcessorException.class);
76 exception.expectMessage("the input value should be of type java.lang.String but is java.lang.Integer");
77 p.execute(123, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
78 }
79
80 @Theory
81 public void testUnparsableString(final ParseLocalDateTime p) {
82 exception.expect(SuperCsvCellProcessorException.class);
83 exception.expectMessage("Failed to parse value");
84 p.execute("not valid", SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
85 }
86
87 @Test
88 public void testConstructor2WithNullNext() {
89 exception.expect(NullPointerException.class);
90 new ParseLocalDateTime((CellProcessor) null);
91 }
92
93 @Test
94 public void testConstructor3WithNullFormatter() {
95 exception.expect(NullPointerException.class);
96 new ParseLocalDateTime((DateTimeFormatter) null);
97 }
98
99 @Test
100 public void testConstructor4WithNullFormatter() {
101 exception.expect(NullPointerException.class);
102 new ParseLocalDateTime(null, new IdentityTransform());
103 }
104
105 @Test
106 public void testConstructor4WithNullNext() {
107 exception.expect(NullPointerException.class);
108 new ParseLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME, null);
109 }
110
111 }