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.time.format.TextStyle;
23  import java.util.Locale;
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.time.mock.IdentityTransform;
33  import org.supercsv.exception.SuperCsvCellProcessorException;
34  
35  /**
36   * Tests the FmtZoneId cell processor.
37   */
38  @RunWith(Theories.class)
39  public class FmtZoneIdTest {
40  
41  	@DataPoints public static final ZoneId[] zoneIds = { ZoneId.of("Europe/Vienna"), ZoneId.of("UTC") };
42  
43  	@DataPoints public static FmtZoneId[] processors = { new FmtZoneId(), new FmtZoneId(new IdentityTransform()) };
44  
45  	@DataPoints public static final Locale[] locales = { Locale.CHINA, Locale.ENGLISH, Locale.GERMANY };
46  
47  	@DataPoints public static final TextStyle[] textStyles = { TextStyle.FULL, TextStyle.SHORT };
48  
49  	@Rule public ExpectedException exception = ExpectedException.none();
50  
51  	@Theory
52  	public void testValidZoneIdString(final FmtZoneId p, final ZoneId zoneId) {
53  		assertEquals(zoneId.toString(), p.execute(zoneId, ANONYMOUS_CSVCONTEXT));
54  	}
55  
56  	@Theory
57  	public void testFormats(final TextStyle textStyle, final Locale locale, final ZoneId zoneId) {
58  		final FmtZoneId p = new FmtZoneId(textStyle, locale);
59  		assertEquals(zoneId.getDisplayName(textStyle, locale), p.execute(zoneId, ANONYMOUS_CSVCONTEXT));
60  	}
61  
62  	@Theory
63  	public void testNullInput(final FmtZoneId p) {
64  		exception.expect(SuperCsvCellProcessorException.class);
65  		exception.expectMessage("this processor does not accept null input - "
66  			+ "if the column is optional then chain an Optional() processor before this one");
67  		p.execute(null, ANONYMOUS_CSVCONTEXT);
68  	}
69  
70  	@Theory
71  	public void testNonZoneIdInput(final FmtZoneId p) {
72  		exception.expect(SuperCsvCellProcessorException.class);
73  		exception.expectMessage("the input value should be of type java.time.ZoneId but is java.lang.Integer");
74  		p.execute(123, ANONYMOUS_CSVCONTEXT);
75  	}
76  
77  	@Test
78  	public void testConstructor2WithNullNext() {
79  		exception.expect(NullPointerException.class);
80  		new FmtZoneId(null);
81  	}
82  
83  	@Test
84  	public void testConstructor3WithNullTestStyle() {
85  		exception.expect(NullPointerException.class);
86  		new FmtZoneId(null, Locale.ENGLISH);
87  	}
88  
89  	@Test
90  	public void testConstructor3WithNullLocale() {
91  		exception.expect(NullPointerException.class);
92  		new FmtZoneId(TextStyle.FULL, null);
93  	}
94  
95  	@Test
96  	public void testConstructor4WithNullLocale() {
97  		exception.expect(NullPointerException.class);
98  		new FmtZoneId(TextStyle.FULL, null, new IdentityTransform());
99  	}
100 
101 	@Test
102 	public void testConstructor4WithNullTestStyle() {
103 		exception.expect(NullPointerException.class);
104 		new FmtZoneId(null, Locale.ENGLISH, new IdentityTransform());
105 	}
106 
107 	@Test
108 	public void testConstructor4WithNullNext() {
109 		exception.expect(NullPointerException.class);
110 		new FmtZoneId(TextStyle.FULL, Locale.ENGLISH, null);
111 	}
112 }