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.junit.Assert.fail;
20  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  import static org.supercsv.SuperCsvTestUtils.date;
22  
23  import java.text.SimpleDateFormat;
24  import java.util.Arrays;
25  import java.util.Date;
26  import java.util.Locale;
27  
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.supercsv.cellprocessor.ift.CellProcessor;
31  import org.supercsv.exception.SuperCsvCellProcessorException;
32  import org.supercsv.mock.IdentityTransform;
33  
34  /**
35   * Tests the ParseDate processor. As ParseDate uses the default locale, this test must be written in a locale
36   * independent way (so the test will pass). The test can be run in a different local by specifying the
37   * <tt>user.language</tt> and <tt>user.country</tt> JVM properties (i.e. <tt>-Duser.language=de -Duser.country=DE</tt>).
38   * 
39   * @author Kasper B. Graversen
40   * @author James Bassett
41   */
42  public class ParseDateTest {
43  	
44  	private static final Date DATE = date(2011, 12, 25);
45  	private static final String DATE_FORMAT = "dd/MM/yyyy";
46  	private static final String FORMATTED_DATE = "25/12/2011";
47  	private static final String DATE_FORMAT2 = "EEE, MMM d, ''yy";
48  	
49  	// locale-independent ("Sun, Dec 25, '11" in en)
50  	private static final String FORMATTED_DATE2 = new SimpleDateFormat(DATE_FORMAT2).format(DATE);
51  	
52  	private static final String GERMAN_DATE = new SimpleDateFormat(DATE_FORMAT2, Locale.GERMAN).format(DATE);
53  	
54  	private CellProcessor processor;
55  	private CellProcessor processor2;
56  	private CellProcessor processor3;
57  	private CellProcessor processorChain;
58  	private CellProcessor processorChain2;
59  	private CellProcessor processorChain3;
60  	
61  	/**
62  	 * Sets up the processors for the test using all constructor combinations.
63  	 */
64  	@Before
65  	public void setUp() {
66  		processor = new ParseDate(DATE_FORMAT);
67  		processor2 = new ParseDate(DATE_FORMAT, true);
68  		processor3 = new ParseDate(DATE_FORMAT, false);
69  		processorChain = new ParseDate(DATE_FORMAT, new IdentityTransform());
70  		processorChain2 = new ParseDate(DATE_FORMAT, true, new IdentityTransform());
71  		processorChain3 = new ParseDate(DATE_FORMAT, false, new IdentityTransform());
72  	}
73  	
74  	/**
75  	 * Tests unchained/chained execution with various date formats.
76  	 */
77  	@Test
78  	public void testValidDate() {
79  		// first date format
80  		assertEquals(DATE, processor.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
81  		assertEquals(DATE, processor2.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
82  		assertEquals(DATE, processor3.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
83  		assertEquals(DATE, processorChain.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
84  		assertEquals(DATE, processorChain2.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
85  		assertEquals(DATE, processorChain3.execute(FORMATTED_DATE, ANONYMOUS_CSVCONTEXT));
86  		
87  	}
88  	
89  	/**
90  	 * Tests unchained/chained execution with a different date format.
91  	 */
92  	@Test
93  	public void testValidDateDifferentFormat() {
94  		
95  		final CellProcessor differentFormat = new ParseDate(DATE_FORMAT2);
96  		final CellProcessor differentFormatChain = new ParseDate(DATE_FORMAT2, new IdentityTransform());
97  		
98  		// try a different date format
99  		assertEquals(DATE, differentFormat.execute(FORMATTED_DATE2, ANONYMOUS_CSVCONTEXT));
100 		assertEquals(DATE, differentFormatChain.execute(FORMATTED_DATE2, ANONYMOUS_CSVCONTEXT));
101 	}
102 	
103 	/**
104 	 * Tests parsing a Date formatted in a different locale.
105 	 */
106 	@Test
107 	public void testDifferentLocale() {
108 		final CellProcessor germanProcessor = new ParseDate(DATE_FORMAT2, false, Locale.GERMAN);
109 		assertEquals(DATE, germanProcessor.execute(GERMAN_DATE, ANONYMOUS_CSVCONTEXT));
110 		
111 		final CellProcessor germanProcessorChain = new ParseDate(DATE_FORMAT2, false, Locale.GERMAN,
112 			new IdentityTransform());
113 		assertEquals(DATE, germanProcessorChain.execute(GERMAN_DATE, ANONYMOUS_CSVCONTEXT));
114 	}
115 	
116 	/**
117 	 * Tests execution with an invalid date (doesn't match format), should throw an exception.
118 	 */
119 	@Test(expected = SuperCsvCellProcessorException.class)
120 	public void testBadlyFormattedDate() {
121 		processor.execute("2011-12-25", ANONYMOUS_CSVCONTEXT);
122 	}
123 	
124 	/**
125 	 * Tests execution with an invalid date (matches format, but data invalid) and non-lenient processors (should throw
126 	 * an exception).
127 	 */
128 	@Test
129 	public void testInvalidDateWithNonLenient() {
130 		final String dodgyDate = "30/02/2012";
131 		for( final CellProcessor cp : Arrays.asList(processor, processor3, processorChain, processorChain3) ) {
132 			try {
133 				cp.execute(dodgyDate, ANONYMOUS_CSVCONTEXT);
134 				fail("should have thrown a SuperCsvCellProcessorException");
135 			}
136 			catch(SuperCsvCellProcessorException e) {}
137 		}
138 	}
139 	
140 	/**
141 	 * Tests execution with an invalid date (matches format, but data invalid) and lenient processors (should work!).
142 	 */
143 	@Test
144 	public void testInvalidDateWithLenient() {
145 		final String dodgyDate = "30/02/2012";
146 		final Date expectedDate = date(2012, 3, 1);
147 		assertEquals(expectedDate, processor2.execute(dodgyDate, ANONYMOUS_CSVCONTEXT));
148 		assertEquals(expectedDate, processorChain2.execute(dodgyDate, ANONYMOUS_CSVCONTEXT));
149 	}
150 	
151 	/**
152 	 * Tests execution with a non String input (should throw an exception).
153 	 */
154 	@Test(expected = SuperCsvCellProcessorException.class)
155 	public void testWithNonCharInput() {
156 		processor.execute(1, ANONYMOUS_CSVCONTEXT);
157 	}
158 	
159 	/**
160 	 * Tests execution with a null input (should throw an Exception).
161 	 */
162 	@Test(expected = SuperCsvCellProcessorException.class)
163 	public void testWithNull() {
164 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
165 	}
166 	
167 	/**
168 	 * Tests construction with a null date format (should throw an Exception).
169 	 */
170 	@Test(expected = NullPointerException.class)
171 	public void testConstructorWithNullDateFormat() {
172 		new ParseDate(null);
173 	}
174 	
175 	/**
176 	 * Tests construction (using the Locale constructor) with a null date format (should throw an Exception).
177 	 */
178 	@Test(expected = NullPointerException.class)
179 	public void testLocaleConstructorWithNullDateFormat() {
180 		new ParseDate(null, false, Locale.GERMAN);
181 	}
182 	
183 	/**
184 	 * Tests construction (using the Locale constructor) with a null locale (should throw an Exception).
185 	 */
186 	@Test(expected = NullPointerException.class)
187 	public void testLocaleConstructorWithNullLocale() {
188 		new ParseDate(DATE_FORMAT, false, (Locale) null);
189 	}
190 	
191 }