1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor.joda;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.fail;
20 import static org.supercsv.cellprocessor.joda.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.joda.time.DateTime;
26 import org.joda.time.DateTimeZone;
27 import org.joda.time.Interval;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.supercsv.cellprocessor.ift.CellProcessor;
31 import org.supercsv.cellprocessor.joda.mock.IdentityTransform;
32 import org.supercsv.exception.SuperCsvCellProcessorException;
33
34
35
36
37 public class ParseIntervalTest {
38
39 private static final String INTERVAL_STRING = "2013-10-25T01:02:03.000Z/2014-11-26T02:03:04.000Z";
40 private static final DateTime START = new DateTime(2013, 10, 25, 1, 2, 3,
41 0, DateTimeZone.UTC);
42 private static final DateTime END = new DateTime(2014, 11, 26, 2, 3, 4, 0,
43 DateTimeZone.UTC);
44
45 private ParseInterval processor1;
46 private ParseInterval processorChain1;
47 private List<ParseInterval> processors;
48
49 @Before
50 public void setUp() {
51 processor1 = new ParseInterval();
52 processorChain1 = new ParseInterval(new IdentityTransform());
53 processors = Arrays.asList(processor1, processorChain1);
54 }
55
56 @Test
57 public void testValidInterval() {
58 for (CellProcessor p : processors) {
59 Interval result = (Interval) p.execute(INTERVAL_STRING,
60 ANONYMOUS_CSVCONTEXT);
61 assertEquals(START, result.getStart().withZone(DateTimeZone.UTC));
62 assertEquals(END, result.getEnd().withZone(DateTimeZone.UTC));
63 }
64 }
65
66 @Test
67 public void testNullInput() {
68 for (CellProcessor p : processors) {
69 try {
70 p.execute(null, ANONYMOUS_CSVCONTEXT);
71 fail("expecting SuperCsvCellProcessorException");
72 } catch (SuperCsvCellProcessorException e) {
73 assertEquals(
74 "this processor does not accept null input - "
75 + "if the column is optional then chain an Optional() processor before this one",
76 e.getMessage());
77 }
78 }
79 }
80
81 @Test
82 public void testNonStringInput() {
83 for (CellProcessor p : processors) {
84 try {
85 p.execute(123, ANONYMOUS_CSVCONTEXT);
86 fail("expecting SuperCsvCellProcessorException");
87 } catch (SuperCsvCellProcessorException e) {
88 assertEquals(
89 "the input value should be of type java.lang.String but is java.lang.Integer",
90 e.getMessage());
91 }
92 }
93 }
94
95 @Test
96 public void testUnparsableString() {
97 for (CellProcessor p : processors) {
98 try {
99 p.execute("not valid", ANONYMOUS_CSVCONTEXT);
100 fail("expecting SuperCsvCellProcessorException");
101 } catch (SuperCsvCellProcessorException e) {
102 assertEquals("Failed to parse value as an Interval",
103 e.getMessage());
104 }
105 }
106 }
107
108 @Test(expected = NullPointerException.class)
109 public void testConstructor2WithNullNext() {
110 new ParseInterval((CellProcessor) null);
111 }
112
113 }