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.ZoneId;
22  import java.util.Map;
23  import java.util.TreeMap;
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 ParseZoneId cell processor.
38   */
39  @RunWith(Theories.class)
40  public class ParseZoneIdTest {
41  
42  	@DataPoints public static final ZoneId[] zoneIds = { ZoneId.of("Europe/Vienna"), ZoneId.of("Asia/Shanghai"),
43  		ZoneId.of("UTC") };
44  
45  	@DataPoints public static ParseZoneId[] processors = { new ParseZoneId(), new ParseZoneId(new IdentityTransform()),
46  		new ParseZoneId(ZoneId.SHORT_IDS), new ParseZoneId(ZoneId.SHORT_IDS, new IdentityTransform()) };
47  
48  	@DataPoints public static final String[] shortIds = ZoneId.SHORT_IDS.keySet()
49  		.toArray(new String[ZoneId.SHORT_IDS.size()]);
50  
51  	@Rule public ExpectedException exception = ExpectedException.none();
52  
53  	@Theory
54  	public void testValidDateTimeZone(final ParseZoneId processor, final ZoneId zoneId) {
55  		assertEquals(zoneId, processor.execute(zoneId.toString(), ANONYMOUS_CSVCONTEXT));
56  	}
57  
58  	@Theory
59  	public void testShortIds(final String shortId) {
60  		final ParseZoneId processor = new ParseZoneId(ZoneId.SHORT_IDS);
61  		assertEquals(ZoneId.of(shortId, ZoneId.SHORT_IDS), processor.execute(shortId, ANONYMOUS_CSVCONTEXT));
62  	}
63  
64  	@Theory
65  	public void testNullInput(final ParseZoneId 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 ParseZoneId 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 testBadStringFormat(final ParseZoneId p) {
81  		exception.expect(SuperCsvCellProcessorException.class);
82  		exception.expectMessage("Failed to parse value as a ZoneId");
83  		p.execute("not valid", ANONYMOUS_CSVCONTEXT);
84  	}
85  
86  	@Theory
87  	public void testUnknownZone(final ParseZoneId p) {
88  		exception.expect(SuperCsvCellProcessorException.class);
89  		exception.expectMessage("Failed to parse value as a ZoneId");
90  		p.execute("Europe/Atlantis", ANONYMOUS_CSVCONTEXT);
91  	}
92  
93  	@Test
94  	public void testConstructor2WithNullNext() {
95  		exception.expect(NullPointerException.class);
96  		new ParseZoneId((CellProcessor) null);
97  	}
98  
99  	@Test
100 	public void testConstructor3WithNullMapping() {
101 		exception.expect(NullPointerException.class);
102 		new ParseZoneId((Map<String, String>) null);
103 	}
104 
105 	@Test
106 	public void testConstructor4WithNullNext() {
107 		exception.expect(NullPointerException.class);
108 		new ParseZoneId(new TreeMap<>(), null);
109 	}
110 
111 	@Test
112 	public void testConstructor4WithNullMapping() {
113 		exception.expect(NullPointerException.class);
114 		new ParseZoneId(null, new IdentityTransform());
115 	}
116 
117 	@Test
118 	public void testConstructor4WithNull() {
119 		exception.expect(NullPointerException.class);
120 		new ParseZoneId(null, null);
121 	}
122 }