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.LocalDateTime;
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 FmtLocalDateTime cell processor.
38   */
39  @RunWith(Theories.class)
40  public class FmtLocalDateTimeTest {
41  
42  	@DataPoints public static final LocalDateTime[] localDateTimes = { LocalDateTime.of(2013, 10, 25, 1, 2, 3, 0) };
43  
44  	@DataPoints public static FmtLocalDateTime[] processors = { new FmtLocalDateTime(),
45  		new FmtLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME), new FmtLocalDateTime(new IdentityTransform()),
46  		new FmtLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME, new IdentityTransform()) };
47  
48  	@DataPoints public static final DateTimeFormatter[] formats = {
49  		DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM), DateTimeFormatter.ofPattern("dd MM mm:ss") };
50  
51  	@Rule public ExpectedException exception = ExpectedException.none();
52  
53  	@Theory
54  	public void testValidDateTimeString(final FmtLocalDateTime p, final LocalDateTime localDateTime) {
55  		assertEquals(localDateTime.toString(), p.execute(localDateTime, ANONYMOUS_CSVCONTEXT));
56  	}
57  
58  	@Theory
59  	public void testFormats(final DateTimeFormatter formatter, final LocalDateTime localDateTime) {
60  		final FmtLocalDateTime p = new FmtLocalDateTime(formatter);
61  		assertEquals(localDateTime.format(formatter), p.execute(localDateTime, ANONYMOUS_CSVCONTEXT));
62  	}
63  
64  	@Theory
65  	public void testNullInput(final FmtLocalDateTime 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 FmtLocalDateTime p) {
74  		exception.expect(SuperCsvCellProcessorException.class);
75  		exception.expectMessage("the input value should be of type java.time.LocalDateTime 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 FmtLocalDateTime((CellProcessor) null);
83  	}
84  
85  	@Test()
86  	public void testConstructor3WithNullFormatter() {
87  		exception.expect(NullPointerException.class);
88  		new FmtLocalDateTime((DateTimeFormatter) null);
89  	}
90  
91  	@Test()
92  	public void testConstructor4WithNullFormatter() {
93  		exception.expect(NullPointerException.class);
94  		new FmtLocalDateTime(null, new IdentityTransform());
95  	}
96  
97  	@Test()
98  	public void testConstructor4WithNullNext() {
99  		exception.expect(NullPointerException.class);
100 		new FmtLocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME, null);
101 	}
102 
103 }