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.time;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.time.Period;
21  
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.experimental.theories.DataPoints;
25  import org.junit.experimental.theories.Theories;
26  import org.junit.experimental.theories.Theory;
27  import org.junit.rules.ExpectedException;
28  import org.junit.runner.RunWith;
29  import org.supercsv.cellprocessor.time.mock.IdentityTransform;
30  import org.supercsv.exception.SuperCsvCellProcessorException;
31  
32  /**
33   * Tests the FmtPeriod cell processor.
34   */
35  @RunWith(Theories.class)
36  public class FmtPeriodTest {
37  
38  	@DataPoints public static final FmtPeriod[] processors = { new FmtPeriod(),
39  		new FmtPeriod(new IdentityTransform()) };
40  	@DataPoints public static final Period[] periods = { Period.of(1, 2, 3), Period.of(0, 0, 0), Period.of(4, 0, 8) };
41  
42  	@Rule public ExpectedException exception = ExpectedException.none();
43  
44  	@Theory
45  	public void testValidPeriodString(final Period period, final FmtPeriod processor) {
46  		assertEquals(period.toString(), processor.execute(period, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
47  	}
48  
49  	@Theory
50  	public void testNullInput(final FmtPeriod p) {
51  		exception.expect(SuperCsvCellProcessorException.class);
52  		exception.expectMessage("this processor does not accept null input - "
53  			+ "if the column is optional then chain an Optional() processor before this one");
54  		p.execute(null, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
55  	}
56  
57  	@Theory
58  	public void testNonPeriodInput(final FmtPeriod p) {
59  		exception.expect(SuperCsvCellProcessorException.class);
60  		exception.expectMessage("the input value should be of type java.time.Period but is java.lang.Integer");
61  		p.execute(123, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
62  	}
63  
64  	@Test
65  	public void testConstructor2WithNullNext() {
66  		exception.expect(NullPointerException.class);
67  		new FmtPeriod(null);
68  	}
69  }