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.joda;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  import static org.supercsv.cellprocessor.joda.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.joda.time.LocalTime;
27  import org.joda.time.format.DateTimeFormatter;
28  import org.joda.time.format.ISODateTimeFormat;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.supercsv.cellprocessor.ift.CellProcessor;
32  import org.supercsv.cellprocessor.joda.mock.IdentityTransform;
33  import org.supercsv.exception.SuperCsvCellProcessorException;
34  
35  /**
36   * Tests the FmtLocalTime cell processor.
37   */
38  public class FmtLocalTimeTest {
39  
40  	private static final String LOCAL_TIME_FORMAT = "hh:mm:ss.SSS";
41  	private static final String LOCAL_TIME_STRING = "01:02:03.000";
42  	private static final LocalTime LOCAL_TIME = new LocalTime(1, 2, 3, 0);
43  
44  	private FmtLocalTime processor1;
45  	private FmtLocalTime processor2;
46  	private FmtLocalTime processor3;
47  	private FmtLocalTime processor4;
48  	private FmtLocalTime processorChain1;
49  	private FmtLocalTime processorChain2;
50  	private FmtLocalTime processorChain3;
51  	private FmtLocalTime processorChain4;
52  	private List<FmtLocalTime> processors;
53  	private DateTimeFormatter formatter;
54  
55  	@Before
56  	public void setUp() {
57  		formatter = ISODateTimeFormat.hourMinuteSecondMillis();
58  		processor1 = new FmtLocalTime();
59  		processor2 = new FmtLocalTime(formatter);
60  		processor3 = new FmtLocalTime(LOCAL_TIME_FORMAT);
61  		processor4 = new FmtLocalTime(LOCAL_TIME_FORMAT, Locale.ENGLISH);
62  		processorChain1 = new FmtLocalTime(new IdentityTransform());
63  		processorChain2 = new FmtLocalTime(formatter, new IdentityTransform());
64  		processorChain3 = new FmtLocalTime(LOCAL_TIME_FORMAT,
65  				new IdentityTransform());
66  		processorChain4 = new FmtLocalTime(LOCAL_TIME_FORMAT, Locale.ENGLISH,
67  				new IdentityTransform());
68  		processors = Arrays.asList(processor1, processor2, processor3,
69  				processor4, processorChain1, processorChain2, processorChain3,
70  				processorChain4);
71  	}
72  
73  	@Test
74  	public void testValidDateTimeString() {
75  		for (CellProcessor p : processors) {
76  			assertEquals(LOCAL_TIME_STRING,
77  					p.execute(LOCAL_TIME, ANONYMOUS_CSVCONTEXT));
78  		}
79  	}
80  
81  	@Test
82  	public void testNullInput() {
83  		for (CellProcessor p : processors) {
84  			try {
85  				p.execute(null, ANONYMOUS_CSVCONTEXT);
86  				fail("expecting SuperCsvCellProcessorException");
87  			} catch (SuperCsvCellProcessorException e) {
88  				assertEquals(
89  						"this processor does not accept null input - "
90  								+ "if the column is optional then chain an Optional() processor before this one",
91  						e.getMessage());
92  			}
93  		}
94  	}
95  
96  	@Test
97  	public void testNonLocalDateTimeInput() {
98  		for (CellProcessor p : processors) {
99  			try {
100 				p.execute(123, ANONYMOUS_CSVCONTEXT);
101 				fail("expecting SuperCsvCellProcessorException");
102 			} catch (SuperCsvCellProcessorException e) {
103 				assertEquals(
104 						"the input value should be of type org.joda.time.LocalTime but is java.lang.Integer",
105 						e.getMessage());
106 			}
107 		}
108 	}
109 
110 	@Test
111 	public void testInvalidDateFormat() {
112 		final CellProcessor p = new FmtLocalTime("not valid");
113 		try {
114 			p.execute(LOCAL_TIME, ANONYMOUS_CSVCONTEXT);
115 			fail("expecting SuperCsvCellProcessorException");
116 		} catch (SuperCsvCellProcessorException e) {
117 			assertEquals("Failed to format value as a LocalTime",
118 					e.getMessage());
119 		}
120 	}
121 
122 	@Test(expected = NullPointerException.class)
123 	public void testConstructor2WithNullNext() {
124 		new FmtLocalTime((CellProcessor) null);
125 	}
126 
127 	@Test(expected = NullPointerException.class)
128 	public void testConstructor3WithNullFormatter() {
129 		new FmtLocalTime((DateTimeFormatter) null);
130 	}
131 
132 	@Test(expected = NullPointerException.class)
133 	public void testConstructor4WithNullFormatter() {
134 		new FmtLocalTime((DateTimeFormatter) null, new IdentityTransform());
135 	}
136 
137 	@Test(expected = NullPointerException.class)
138 	public void testConstructor4WithNullNext() {
139 		new FmtLocalTime(formatter, null);
140 	}
141 
142 	@Test(expected = NullPointerException.class)
143 	public void testConstructor5WithNullPattern() {
144 		new FmtLocalTime((String) null);
145 	}
146 
147 	@Test(expected = NullPointerException.class)
148 	public void testConstructor6WithNullPattern() {
149 		new FmtLocalTime((String) null, new IdentityTransform());
150 	}
151 
152 	@Test(expected = NullPointerException.class)
153 	public void testConstructor6WithNullNext() {
154 		new FmtLocalTime(LOCAL_TIME_FORMAT, (CellProcessor) null);
155 	}
156 
157 	@Test(expected = NullPointerException.class)
158 	public void testConstructor7WithNullPattern() {
159 		new FmtLocalTime((String) null, Locale.ENGLISH, new IdentityTransform());
160 	}
161 
162 	@Test(expected = NullPointerException.class)
163 	public void testConstructor7WithNullNext() {
164 		new FmtLocalTime(LOCAL_TIME_FORMAT, Locale.ENGLISH,
165 				(CellProcessor) null);
166 	}
167 
168 }