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.LocalTime;
22  import java.time.format.DateTimeFormatter;
23  import java.time.format.FormatStyle;
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.ift.CellProcessor;
33  import org.supercsv.cellprocessor.time.mock.IdentityTransform;
34  import org.supercsv.exception.SuperCsvCellProcessorException;
35  
36  /**
37   * Tests the FmtLocalTime cell processor.
38   */
39  @RunWith(Theories.class)
40  public class FmtLocalTimeTest {
41  
42  	@DataPoints public static final LocalTime[] localTimes = { LocalTime.of(1, 2, 3, 0) };
43  
44  	@DataPoints public static FmtLocalTime[] processors = { new FmtLocalTime(),
45  		new FmtLocalTime(DateTimeFormatter.ISO_LOCAL_TIME), new FmtLocalTime(new IdentityTransform()),
46  		new FmtLocalTime(DateTimeFormatter.ISO_LOCAL_TIME, new IdentityTransform()) };
47  
48  	@DataPoints public static final DateTimeFormatter[] formats = {
49  		DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM), DateTimeFormatter.ofPattern("mm:ss") };
50  
51  	@Rule public ExpectedException exception = ExpectedException.none();
52  
53  	@Theory
54  	public void testValidDateTimeString(final FmtLocalTime p, final LocalTime localTime) {
55  		assertEquals(localTime.toString(), p.execute(localTime, ANONYMOUS_CSVCONTEXT));
56  	}
57  
58  	@Theory
59  	public void testFormats(final DateTimeFormatter formatter, final LocalTime localTime) {
60  		final FmtLocalTime p = new FmtLocalTime(formatter);
61  		assertEquals(localTime.format(formatter), p.execute(localTime, ANONYMOUS_CSVCONTEXT));
62  	}
63  
64  	@Theory
65  	public void testNullInput(final FmtLocalTime p) {
66  		exception.expect(SuperCsvCellProcessorException.class);
67  		exception.expectMessage("this processor does not accept null input - "
68  			+ "if the column is optional then chain an Optional() processor before this one");
69  		p.execute(null, ANONYMOUS_CSVCONTEXT);
70  	}
71  
72  	@Theory
73  	public void testNonLocalDateTimeInput(final FmtLocalTime p) {
74  		exception.expect(SuperCsvCellProcessorException.class);
75  		exception.expectMessage("the input value should be of type java.time.LocalTime but is java.lang.Integer");
76  		p.execute(123, ANONYMOUS_CSVCONTEXT);
77  	}
78  
79  	@Test
80  	public void testConstructor2WithNullNext() {
81  		exception.expect(NullPointerException.class);
82  		new FmtLocalTime((CellProcessor) null);
83  	}
84  
85  	@Test
86  	public void testConstructor3WithNullFormatter() {
87  		exception.expect(NullPointerException.class);
88  		new FmtLocalTime((DateTimeFormatter) null);
89  	}
90  
91  	@Test
92  	public void testConstructor4WithNullFormatter() {
93  		exception.expect(NullPointerException.class);
94  		new FmtLocalTime(null, new IdentityTransform());
95  	}
96  
97  	@Test
98  	public void testConstructor4WithNullNext() {
99  		exception.expect(NullPointerException.class);
100 		new FmtLocalTime(DateTimeFormatter.ISO_LOCAL_TIME, null);
101 	}
102 }