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