View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.cellprocessor.joda;
17  
18  import static org.supercsv.cellprocessor.joda.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
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.format.DateTimeFormatter;
28  import org.joda.time.format.ISODateTimeFormat;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.supercsv.cellprocessor.ift.CellProcessor;
32  import org.supercsv.cellprocessor.joda.mock.IdentityTransform;
33  import org.supercsv.exception.SuperCsvCellProcessorException;
34  
35  /**
36   * Tests the ParseDateTime cell processor.
37   */
38  public class ParseDateTimeTest {
39  
40  	private static final String DATE_TIME_STRING = "2013-10-25T01:02:03.000+10";
41  	private static final DateTime DATE_TIME = new DateTime(2013, 10, 25, 1, 2,
42  			3, 0, DateTimeZone.forOffsetHours(10));
43  
44  	private ParseDateTime processor1;
45  	private ParseDateTime processor2;
46  	private ParseDateTime processorChain1;
47  	private ParseDateTime processorChain2;
48  	private List<ParseDateTime> processors;
49  	private DateTimeFormatter formatter;
50  
51  	@Before
52  	public void setUp() {
53  		formatter = ISODateTimeFormat.dateTime().withOffsetParsed();
54  		processor1 = new ParseDateTime();
55  		processor2 = new ParseDateTime(formatter);
56  		processorChain1 = new ParseDateTime(new IdentityTransform());
57  		processorChain2 = new ParseDateTime(formatter, new IdentityTransform());
58  		processors = Arrays.asList(processor1, processor2, processorChain1,
59  				processorChain2);
60  	}
61  
62  	@Test
63  	public void testValidDateTime() {
64  		for (CellProcessor p : processors) {
65  			assertEquals(DATE_TIME,
66  					p.execute(DATE_TIME_STRING, ANONYMOUS_CSVCONTEXT));
67  		}
68  	}
69  
70  	@Test
71  	public void testNullInput() {
72  		for (CellProcessor p : processors) {
73  			try {
74  				p.execute(null, ANONYMOUS_CSVCONTEXT);
75  				fail("expecting SuperCsvCellProcessorException");
76  			} catch (SuperCsvCellProcessorException e) {
77  				assertEquals(
78  						"this processor does not accept null input - "
79  								+ "if the column is optional then chain an Optional() processor before this one",
80  						e.getMessage());
81  			}
82  		}
83  	}
84  
85  	@Test
86  	public void testNonStringInput() {
87  		for (CellProcessor p : processors) {
88  			try {
89  				p.execute(123, ANONYMOUS_CSVCONTEXT);
90  				fail("expecting SuperCsvCellProcessorException");
91  			} catch (SuperCsvCellProcessorException e) {
92  				assertEquals(
93  						"the input value should be of type java.lang.String but is java.lang.Integer",
94  						e.getMessage());
95  			}
96  		}
97  	}
98  
99  	@Test
100 	public void testUnparsableString() {
101 		for (CellProcessor p : processors) {
102 			try {
103 				p.execute("not valid", ANONYMOUS_CSVCONTEXT);
104 				fail("expecting SuperCsvCellProcessorException");
105 			} catch (SuperCsvCellProcessorException e) {
106 				assertEquals("Failed to parse value", e.getMessage());
107 			}
108 		}
109 	}
110 
111 	@Test(expected = NullPointerException.class)
112 	public void testConstructor2WithNullNext() {
113 		new ParseDateTime((CellProcessor) null);
114 	}
115 
116 	@Test(expected = NullPointerException.class)
117 	public void testConstructor3WithNullFormatter() {
118 		new ParseDateTime((DateTimeFormatter) null);
119 	}
120 
121 	@Test(expected = NullPointerException.class)
122 	public void testConstructor4WithNullFormatter() {
123 		new ParseDateTime((DateTimeFormatter) null, new IdentityTransform());
124 	}
125 
126 	@Test(expected = NullPointerException.class)
127 	public void testConstructor4WithNullNext() {
128 		new ParseDateTime(formatter, null);
129 	}
130 
131 }