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.LocalTime;
26 import org.joda.time.format.DateTimeFormatter;
27 import org.joda.time.format.ISODateTimeFormat;
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 ParseLocalTimeTest {
38
39 private static final String LOCAL_TIME_STRING = "01:02:03";
40 private static final LocalTime LOCAL_TIME = new LocalTime(1, 2, 3, 0);
41
42 private ParseLocalTime processor1;
43 private ParseLocalTime processor2;
44 private ParseLocalTime processorChain1;
45 private ParseLocalTime processorChain2;
46 private List<ParseLocalTime> processors;
47 private DateTimeFormatter formatter;
48
49 @Before
50 public void setUp() {
51 formatter = ISODateTimeFormat.localTimeParser();
52 processor1 = new ParseLocalTime();
53 processor2 = new ParseLocalTime(formatter);
54 processorChain1 = new ParseLocalTime(new IdentityTransform());
55 processorChain2 = new ParseLocalTime(formatter, new IdentityTransform());
56 processors = Arrays.asList(processor1, processor2, processorChain1,
57 processorChain2);
58 }
59
60 @Test
61 public void testValidLocalTime() {
62 for (CellProcessor p : processors) {
63 assertEquals(LOCAL_TIME,
64 p.execute(LOCAL_TIME_STRING, ANONYMOUS_CSVCONTEXT));
65 }
66 }
67
68 @Test
69 public void testNullInput() {
70 for (CellProcessor p : processors) {
71 try {
72 p.execute(null, ANONYMOUS_CSVCONTEXT);
73 fail("expecting SuperCsvCellProcessorException");
74 } catch (SuperCsvCellProcessorException e) {
75 assertEquals(
76 "this processor does not accept null input - "
77 + "if the column is optional then chain an Optional() processor before this one",
78 e.getMessage());
79 }
80 }
81 }
82
83 @Test
84 public void testNonStringInput() {
85 for (CellProcessor p : processors) {
86 try {
87 p.execute(123, ANONYMOUS_CSVCONTEXT);
88 fail("expecting SuperCsvCellProcessorException");
89 } catch (SuperCsvCellProcessorException e) {
90 assertEquals(
91 "the input value should be of type java.lang.String but is java.lang.Integer",
92 e.getMessage());
93 }
94 }
95 }
96
97 @Test
98 public void testUnparsableString() {
99 for (CellProcessor p : processors) {
100 try {
101 p.execute("not valid", ANONYMOUS_CSVCONTEXT);
102 fail("expecting SuperCsvCellProcessorException");
103 } catch (SuperCsvCellProcessorException e) {
104 assertEquals("Failed to parse value", e.getMessage());
105 }
106 }
107 }
108
109 @Test(expected = NullPointerException.class)
110 public void testConstructor2WithNullNext() {
111 new ParseLocalTime((CellProcessor) null);
112 }
113
114 @Test(expected = NullPointerException.class)
115 public void testConstructor3WithNullFormatter() {
116 new ParseLocalTime((DateTimeFormatter) null);
117 }
118
119 @Test(expected = NullPointerException.class)
120 public void testConstructor4WithNullFormatter() {
121 new ParseLocalTime((DateTimeFormatter) null, new IdentityTransform());
122 }
123
124 @Test(expected = NullPointerException.class)
125 public void testConstructor4WithNullNext() {
126 new ParseLocalTime(formatter, null);
127 }
128
129 }