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.Period;
22
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.experimental.theories.DataPoints;
26 import org.junit.experimental.theories.Theories;
27 import org.junit.experimental.theories.Theory;
28 import org.junit.rules.ExpectedException;
29 import org.junit.runner.RunWith;
30 import org.supercsv.cellprocessor.time.mock.IdentityTransform;
31 import org.supercsv.exception.SuperCsvCellProcessorException;
32
33
34
35
36 @RunWith(Theories.class)
37 public class ParsePeriodTest {
38
39 @DataPoints public static ParsePeriod[] processors = { new ParsePeriod(),
40 new ParsePeriod(new IdentityTransform()) };
41
42 @DataPoints public static Period[] periods = { Period.of(1, 2, 3), Period.of(3, 0, 23) };
43 @Rule public ExpectedException exception = ExpectedException.none();
44
45 @Theory
46 public void testValidPeriod(final ParsePeriod processor, final Period period) {
47 assertEquals(period, processor.execute(period.toString(), ANONYMOUS_CSVCONTEXT));
48 }
49
50 @Theory
51 public void testNullInput(final ParsePeriod p) {
52 exception.expect(SuperCsvCellProcessorException.class);
53 exception.expectMessage("this processor does not accept null input - "
54 + "if the column is optional then chain an Optional() processor before this one");
55 p.execute(null, ANONYMOUS_CSVCONTEXT);
56 }
57
58 @Theory
59 public void testNonStringInput(final ParsePeriod p) {
60 exception.expect(SuperCsvCellProcessorException.class);
61 exception.expectMessage("the input value should be of type java.lang.String but is java.lang.Integer");
62 p.execute(123, ANONYMOUS_CSVCONTEXT);
63 }
64
65 @Theory
66 public void testUnparsableString(final ParsePeriod p) {
67 exception.expect(SuperCsvCellProcessorException.class);
68 exception.expectMessage("Failed to parse value as a Period");
69 p.execute("not valid", ANONYMOUS_CSVCONTEXT);
70 }
71
72 @Test
73 public void testConstructor2WithNullNext() {
74 exception.expect(NullPointerException.class);
75 new ParsePeriod(null);
76 }
77
78 }