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;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  import static org.supercsv.SuperCsvTestUtils.date;
21  
22  import java.util.Date;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.supercsv.cellprocessor.ift.CellProcessor;
27  import org.supercsv.exception.SuperCsvCellProcessorException;
28  import org.supercsv.mock.IdentityTransform;
29  
30  /**
31   * Tests the FmtDate processor.
32   * 
33   * @author Dominique De Vito
34   * @author James Bassett
35   */
36  public class FmtDateTest {
37  	
38  	private static final String DATE_FORMAT = "dd/MM/yyyy";
39  	private static final Date DATE = date(2011, 12, 25);
40  	private static final String FORMATTED_DATE = "25/12/2011";
41  	
42  	private CellProcessor processor;
43  	private CellProcessor processorChain;
44  	
45  	/**
46  	 * Sets up the processors for the test using all constructor combinations.
47  	 */
48  	@Before
49  	public void setUp() {
50  		processor = new FmtDate(DATE_FORMAT);
51  		processorChain = new FmtDate(DATE_FORMAT, new IdentityTransform());
52  	}
53  	
54  	/**
55  	 * Tests unchained/chained execution with a valid date.
56  	 */
57  	@Test
58  	public void testWithValidDate() {
59  		assertEquals(FORMATTED_DATE, processor.execute(DATE, ANONYMOUS_CSVCONTEXT));
60  		assertEquals(FORMATTED_DATE, processorChain.execute(DATE, ANONYMOUS_CSVCONTEXT));
61  	}
62  	
63  	/**
64  	 * Tests execution with a null input (should throw an Exception).
65  	 */
66  	@Test(expected = SuperCsvCellProcessorException.class)
67  	public void testWithNull() {
68  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
69  	}
70  	
71  	/**
72  	 * Tests execution with a non-Date input (should throw an Exception).
73  	 */
74  	@Test(expected = SuperCsvCellProcessorException.class)
75  	public void testWithNonDate() {
76  		processor.execute(123, ANONYMOUS_CSVCONTEXT);
77  	}
78  	
79  	/**
80  	 * Tests execution with a invalid date format (should throw an Exception).
81  	 */
82  	@Test(expected = SuperCsvCellProcessorException.class)
83  	public void testWithInvalidDateFormat() {
84  		CellProcessor invalidDateFormatProcessor = new FmtDate("abcd");
85  		invalidDateFormatProcessor.execute(DATE, ANONYMOUS_CSVCONTEXT);
86  	}
87  	
88  	/**
89  	 * Tests construction of the processor with a null date format (should throw an Exception).
90  	 */
91  	@Test(expected = NullPointerException.class)
92  	public void testWithNullDateFormat() {
93  		new FmtDate(null);
94  	}
95  }