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