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.ZoneOffset;
22  import java.time.ZonedDateTime;
23  import java.time.format.DateTimeFormatter;
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 ParseZonedDateTime cell processor.
38   */
39  @RunWith(Theories.class)
40  public class ParseZonedDateTimeTest {
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 ParseZonedDateTime[] processors = { new ParseZonedDateTime(),
45  		new ParseZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME), new ParseZonedDateTime(new IdentityTransform()),
46  		new ParseZonedDateTime(DateTimeFormatter.ISO_ZONED_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 testValidDateTime(final ParseZonedDateTime p, final ZonedDateTime dateTime) {
55  		assertEquals(dateTime, p.execute(dateTime.toString(), ANONYMOUS_CSVCONTEXT));
56  	}
57  
58  	@Theory
59  	public void testFormats(final ZonedDateTime dateTime, final DateTimeFormatter formatter) {
60  		final ParseZonedDateTime p = new ParseZonedDateTime(formatter);
61  		assertEquals(dateTime, p.execute(dateTime.format(formatter), ANONYMOUS_CSVCONTEXT));
62  	}
63  
64  	@Theory
65  	public void testNullInput(final ParseZonedDateTime 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 testNonStringInput(final ParseZonedDateTime p) {
74  		exception.expect(SuperCsvCellProcessorException.class);
75  		exception.expectMessage("the input value should be of type java.lang.String but is java.lang.Integer");
76  		p.execute(123, ANONYMOUS_CSVCONTEXT);
77  	}
78  
79  	@Theory
80  	public void testUnparsableString(final ParseZonedDateTime p) {
81  		exception.expect(SuperCsvCellProcessorException.class);
82  		exception.expectMessage("Failed to parse value");
83  		p.execute("not valid", ANONYMOUS_CSVCONTEXT);
84  	}
85  
86  	@Test
87  	public void testConstructor2WithNullNext() {
88  		exception.expect(NullPointerException.class);
89  		new ParseZonedDateTime((CellProcessor) null);
90  	}
91  
92  	@Test
93  	public void testConstructor3WithNullFormatter() {
94  		exception.expect(NullPointerException.class);
95  		new ParseZonedDateTime((DateTimeFormatter) null);
96  	}
97  
98  	@Test
99  	public void testConstructor4WithNullFormatter() {
100 		exception.expect(NullPointerException.class);
101 		new ParseZonedDateTime(null, new IdentityTransform());
102 	}
103 
104 	@Test
105 	public void testConstructor4WithNullNext() {
106 		exception.expect(NullPointerException.class);
107 		new ParseZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME, null);
108 	}
109 
110 }