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.ZoneOffset;
21  import java.time.ZonedDateTime;
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 FmtZonedDateTime cell processor.
37   */
38  @RunWith(Theories.class)
39  public class FmtZonedDateTimeTest {
40  
41  	@DataPoints public static final ZonedDateTime[] dateTimes = {
42  		ZonedDateTime.of(2013, 10, 25, 1, 2, 3, 0, ZoneOffset.ofHours(10)) };
43  
44  	@DataPoints public static FmtZonedDateTime[] processor = { new FmtZonedDateTime(),
45  		new FmtZonedDateTime(DateTimeFormatter.ISO_DATE_TIME), new FmtZonedDateTime(new IdentityTransform()),
46  		new FmtZonedDateTime(DateTimeFormatter.ISO_DATE_TIME, new IdentityTransform()) };
47  
48  	@DataPoints public static final DateTimeFormatter[] formats = { DateTimeFormatter.ISO_ZONED_DATE_TIME,
49  		DateTimeFormatter.ofPattern("eee MMM dd HH:mm:ss zzz yyyy") };
50  
51  	@Rule public ExpectedException exception = ExpectedException.none();
52  
53  	@Theory
54  	public void testValidDateTimeString(final FmtZonedDateTime p, final ZonedDateTime dateTime) {
55  		assertEquals(dateTime.toString(), p.execute(dateTime, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
56  	}
57  
58  	@Theory
59  	public void testFormats(final DateTimeFormatter formatter, final ZonedDateTime dateTime) {
60  		final FmtZonedDateTime p = new FmtZonedDateTime(formatter);
61  		assertEquals(dateTime.format(formatter), p.execute(dateTime, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT));
62  	}
63  
64  	@Theory
65  	public void testNullInput(final FmtZonedDateTime 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, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
70  	}
71  
72  	@Theory
73  	public void testNonDateTimeInput(final FmtZonedDateTime p) {
74  		exception.expect(SuperCsvCellProcessorException.class);
75  		exception.expectMessage("the input value should be of type java.time.ZonedDateTime but is java.lang.Integer");
76  		p.execute(123, SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
77  	}
78  
79  	@Test
80  	public void testConstructor2WithNullNext() {
81  		exception.expect(NullPointerException.class);
82  		new FmtZonedDateTime((CellProcessor) null);
83  	}
84  
85  	@Test
86  	public void testConstructor3WithNullFormatter() {
87  		exception.expect(NullPointerException.class);
88  		new FmtZonedDateTime((DateTimeFormatter) null);
89  	}
90  
91  	@Test
92  	public void testConstructor4WithNullFormatter() {
93  		exception.expect(NullPointerException.class);
94  		new FmtZonedDateTime(null, new IdentityTransform());
95  	}
96  
97  	@Test
98  	public void testConstructor4WithNullNext() {
99  		exception.expect(NullPointerException.class);
100 		new FmtZonedDateTime(DateTimeFormatter.ISO_DATE_TIME, null);
101 	}
102 
103 }