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