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.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  import java.util.Locale;
25  
26  import org.joda.time.DateTime;
27  import org.joda.time.DateTimeZone;
28  import org.joda.time.format.DateTimeFormatter;
29  import org.joda.time.format.ISODateTimeFormat;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.supercsv.cellprocessor.ift.CellProcessor;
33  import org.supercsv.cellprocessor.joda.mock.IdentityTransform;
34  import org.supercsv.exception.SuperCsvCellProcessorException;
35  
36  /**
37   * Tests the FmtDateTime cell processor.
38   */
39  public class FmtDateTimeTest {
40  
41  	private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSSZZ";
42  	private static final String DATE_TIME_STRING = "2013-10-25T01:02:03.000+10:00";
43  	private static final DateTime DATE_TIME = new DateTime(2013, 10, 25, 1, 2,
44  			3, 0, DateTimeZone.forOffsetHours(10));
45  
46  	private FmtDateTime processor1;
47  	private FmtDateTime processor2;
48  	private FmtDateTime processor3;
49  	private FmtDateTime processor4;
50  	private FmtDateTime processorChain1;
51  	private FmtDateTime processorChain2;
52  	private FmtDateTime processorChain3;
53  	private FmtDateTime processorChain4;
54  	private List<FmtDateTime> processors;
55  	private DateTimeFormatter formatter;
56  
57  	@Before
58  	public void setUp() {
59  		formatter = ISODateTimeFormat.dateTime();
60  		processor1 = new FmtDateTime();
61  		processor2 = new FmtDateTime(formatter);
62  		processor3 = new FmtDateTime(DATE_TIME_FORMAT);
63  		processor4 = new FmtDateTime(DATE_TIME_FORMAT, Locale.ENGLISH);
64  		processorChain1 = new FmtDateTime(new IdentityTransform());
65  		processorChain2 = new FmtDateTime(formatter, new IdentityTransform());
66  		processorChain3 = new FmtDateTime(DATE_TIME_FORMAT,
67  				new IdentityTransform());
68  		processorChain4 = new FmtDateTime(DATE_TIME_FORMAT, Locale.ENGLISH,
69  				new IdentityTransform());
70  		processors = Arrays.asList(processor1, processor2, processor3,
71  				processor4, processorChain1, processorChain2, processorChain3,
72  				processorChain4);
73  	}
74  
75  	@Test
76  	public void testValidDateTimeString() {
77  		for (CellProcessor p : processors) {
78  			assertEquals(DATE_TIME_STRING,
79  					p.execute(DATE_TIME, ANONYMOUS_CSVCONTEXT));
80  		}
81  	}
82  
83  	@Test
84  	public void testNullInput() {
85  		for (CellProcessor p : processors) {
86  			try {
87  				p.execute(null, ANONYMOUS_CSVCONTEXT);
88  				fail("expecting SuperCsvCellProcessorException");
89  			} catch (SuperCsvCellProcessorException e) {
90  				assertEquals(
91  						"this processor does not accept null input - "
92  								+ "if the column is optional then chain an Optional() processor before this one",
93  						e.getMessage());
94  			}
95  		}
96  	}
97  
98  	@Test
99  	public void testNonDateTimeInput() {
100 		for (CellProcessor p : processors) {
101 			try {
102 				p.execute(123, ANONYMOUS_CSVCONTEXT);
103 				fail("expecting SuperCsvCellProcessorException");
104 			} catch (SuperCsvCellProcessorException e) {
105 				assertEquals(
106 						"the input value should be of type org.joda.time.DateTime but is java.lang.Integer",
107 						e.getMessage());
108 			}
109 		}
110 	}
111 
112 	@Test
113 	public void testInvalidDateFormat() {
114 		final CellProcessor p = new FmtDateTime("not valid");
115 		try {
116 			p.execute(DATE_TIME, ANONYMOUS_CSVCONTEXT);
117 			fail("expecting SuperCsvCellProcessorException");
118 		} catch (SuperCsvCellProcessorException e) {
119 			assertEquals("Failed to format value as a DateTime", e.getMessage());
120 		}
121 	}
122 
123 	@Test(expected = NullPointerException.class)
124 	public void testConstructor2WithNullNext() {
125 		new FmtDateTime((CellProcessor) null);
126 	}
127 
128 	@Test(expected = NullPointerException.class)
129 	public void testConstructor3WithNullFormatter() {
130 		new FmtDateTime((DateTimeFormatter) null);
131 	}
132 
133 	@Test(expected = NullPointerException.class)
134 	public void testConstructor4WithNullFormatter() {
135 		new FmtDateTime((DateTimeFormatter) null, new IdentityTransform());
136 	}
137 
138 	@Test(expected = NullPointerException.class)
139 	public void testConstructor4WithNullNext() {
140 		new FmtDateTime(formatter, null);
141 	}
142 
143 	@Test(expected = NullPointerException.class)
144 	public void testConstructor5WithNullPattern() {
145 		new FmtDateTime((String) null);
146 	}
147 
148 	@Test(expected = NullPointerException.class)
149 	public void testConstructor6WithNullPattern() {
150 		new FmtDateTime((String) null, new IdentityTransform());
151 	}
152 
153 	@Test(expected = NullPointerException.class)
154 	public void testConstructor6WithNullNext() {
155 		new FmtDateTime(DATE_TIME_FORMAT, (CellProcessor) null);
156 	}
157 
158 	@Test(expected = NullPointerException.class)
159 	public void testConstructor7WithNullPattern() {
160 		new FmtDateTime((String) null, Locale.ENGLISH, new IdentityTransform());
161 	}
162 
163 	@Test(expected = NullPointerException.class)
164 	public void testConstructor7WithNullNext() {
165 		new FmtDateTime(DATE_TIME_FORMAT, Locale.ENGLISH, (CellProcessor) null);
166 	}
167 
168 }