1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  }