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