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