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