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  
25  import org.joda.time.DateTimeZone;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.supercsv.cellprocessor.ift.CellProcessor;
29  import org.supercsv.cellprocessor.joda.mock.IdentityTransform;
30  import org.supercsv.exception.SuperCsvCellProcessorException;
31  
32  /**
33   * Tests the ParseDateTimeZone cell processor.
34   */
35  public class ParseDateTimeZoneTest {
36  
37  	private static final String TIME_ZONE_STRING = "Australia/Brisbane";
38  	private static final DateTimeZone DATE_TIME_ZONE = DateTimeZone.forID("Australia/Brisbane");
39  
40  	private ParseDateTimeZone processor1;
41  	private ParseDateTimeZone processorChain1;
42  	private List<ParseDateTimeZone> processors;
43  
44  	@Before
45  	public void setUp() {
46  		processor1 = new ParseDateTimeZone();
47  		processorChain1 = new ParseDateTimeZone(new IdentityTransform());
48  		processors = Arrays.asList(processor1, processorChain1);
49  	}
50  
51  	@Test
52  	public void testValidDateTimeZone() {
53  		for (CellProcessor p : processors) {
54  			assertEquals(DATE_TIME_ZONE,
55  					p.execute(TIME_ZONE_STRING, ANONYMOUS_CSVCONTEXT));
56  		}
57  	}
58  
59  	@Test
60  	public void testNullInput() {
61  		for (CellProcessor p : processors) {
62  			try {
63  				p.execute(null, ANONYMOUS_CSVCONTEXT);
64  				fail("expecting SuperCsvCellProcessorException");
65  			} catch (SuperCsvCellProcessorException e) {
66  				assertEquals(
67  						"this processor does not accept null input - "
68  								+ "if the column is optional then chain an Optional() processor before this one",
69  						e.getMessage());
70  			}
71  		}
72  	}
73  
74  	@Test
75  	public void testNonStringInput() {
76  		for (CellProcessor p : processors) {
77  			try {
78  				p.execute(123, ANONYMOUS_CSVCONTEXT);
79  				fail("expecting SuperCsvCellProcessorException");
80  			} catch (SuperCsvCellProcessorException e) {
81  				assertEquals(
82  						"the input value should be of type java.lang.String but is java.lang.Integer",
83  						e.getMessage());
84  			}
85  		}
86  	}
87  
88  	@Test
89  	public void testUnparsableString() {
90  		for (CellProcessor p : processors) {
91  			try {
92  				p.execute("not valid", ANONYMOUS_CSVCONTEXT);
93  				fail("expecting SuperCsvCellProcessorException");
94  			} catch (SuperCsvCellProcessorException e) {
95  				assertEquals("Failed to parse value as a DateTimeZone",
96  						e.getMessage());
97  			}
98  		}
99  	}
100 
101 	@Test(expected = NullPointerException.class)
102 	public void testConstructor2WithNullNext() {
103 		new ParseDateTimeZone((CellProcessor) null);
104 	}
105 
106 }