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.Interval;
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 FmtInterval cell processor.
36   */
37  public class FmtIntervalTest {
38  
39  	private static final String INTERVAL_STRING = "2013-10-25T01:02:03.000Z/2014-11-26T02:03:04.000Z";
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 Interval INTERVAL = new Interval(START, END);
45  
46  	private FmtInterval processor1;
47  	private FmtInterval processorChain1;
48  	private List<FmtInterval> processors;
49  
50  	@Before
51  	public void setUp() {
52  		processor1 = new FmtInterval();
53  		processorChain1 = new FmtInterval(new IdentityTransform());
54  		processors = Arrays.asList(processor1, processorChain1);
55  	}
56  
57  	@Test
58  	public void testValidIntervalString() {
59  		for (CellProcessor p : processors) {
60  			assertEquals(INTERVAL_STRING,
61  					p.execute(INTERVAL, 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 testNonIntervalInput() {
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 org.joda.time.Interval but is java.lang.Integer",
89  						e.getMessage());
90  			}
91  		}
92  	}
93  
94  	@Test(expected = NullPointerException.class)
95  	public void testConstructor2WithNullNext() {
96  		new FmtInterval((CellProcessor) null);
97  	}
98  
99  }