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