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.Period;
26  import org.joda.time.format.ISOPeriodFormat;
27  import org.joda.time.format.PeriodFormatter;
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 FmtPeriod cell processor.
36   */
37  public class FmtPeriodTest {
38  
39  	private static final String PERIOD_STRING = "P1Y2M3DT4H5M6S";
40  	private static final Period PERIOD = new Period(1, 2, 0, 3, 4, 5, 6, 0);
41  
42  	private FmtPeriod processor1;
43  	private FmtPeriod processor2;
44  	private FmtPeriod processorChain1;
45  	private FmtPeriod processorChain2;
46  	private List<FmtPeriod> processors;
47  	private PeriodFormatter formatter;
48  
49  	@Before
50  	public void setUp() {
51  		formatter = ISOPeriodFormat.standard();
52  		processor1 = new FmtPeriod();
53  		processor2 = new FmtPeriod(formatter);
54  		processorChain1 = new FmtPeriod(new IdentityTransform());
55  		processorChain2 = new FmtPeriod(formatter, new IdentityTransform());
56  		processors = Arrays.asList(processor1, processor2, processorChain1,
57  				processorChain2);
58  	}
59  
60  	@Test
61  	public void testValidPeriodString() {
62  		for (CellProcessor p : processors) {
63  			assertEquals(PERIOD_STRING, p.execute(PERIOD, ANONYMOUS_CSVCONTEXT));
64  		}
65  	}
66  
67  	@Test
68  	public void testNullInput() {
69  		for (CellProcessor p : processors) {
70  			try {
71  				p.execute(null, ANONYMOUS_CSVCONTEXT);
72  				fail("expecting SuperCsvCellProcessorException");
73  			} catch (SuperCsvCellProcessorException e) {
74  				assertEquals(
75  						"this processor does not accept null input - "
76  								+ "if the column is optional then chain an Optional() processor before this one",
77  						e.getMessage());
78  			}
79  		}
80  	}
81  
82  	@Test
83  	public void testNonPeriodInput() {
84  		for (CellProcessor p : processors) {
85  			try {
86  				p.execute(123, ANONYMOUS_CSVCONTEXT);
87  				fail("expecting SuperCsvCellProcessorException");
88  			} catch (SuperCsvCellProcessorException e) {
89  				assertEquals(
90  						"the input value should be of type org.joda.time.Period but is java.lang.Integer",
91  						e.getMessage());
92  			}
93  		}
94  	}
95  
96  	@Test(expected = NullPointerException.class)
97  	public void testConstructor2WithNullNext() {
98  		new FmtPeriod((CellProcessor) null);
99  	}
100 
101 	@Test(expected = NullPointerException.class)
102 	public void testConstructor3WithNullFormatter() {
103 		new FmtPeriod((PeriodFormatter) null);
104 	}
105 
106 	@Test(expected = NullPointerException.class)
107 	public void testConstructor4WithNullFormatter() {
108 		new FmtPeriod((PeriodFormatter) null, new IdentityTransform());
109 	}
110 
111 	@Test(expected = NullPointerException.class)
112 	public void testConstructor4WithNullNext() {
113 		new FmtPeriod(formatter, null);
114 	}
115 
116 }