View Javadoc
1   package org.supercsv.features;
2   
3   import org.junit.Assert;
4   import org.junit.Ignore;
5   import org.junit.Test;
6   import org.supercsv.cellprocessor.ParseBigDecimal;
7   import org.supercsv.cellprocessor.ParseDate;
8   import org.supercsv.cellprocessor.ParseInt;
9   import org.supercsv.cellprocessor.Trim;
10  import org.supercsv.cellprocessor.constraint.NotNull;
11  import org.supercsv.cellprocessor.ift.CellProcessor;
12  import org.supercsv.comment.CommentMatches;
13  import org.supercsv.io.CsvBeanReader;
14  import org.supercsv.io.CsvListReader;
15  import org.supercsv.io.CsvMapReader;
16  import org.supercsv.prefs.CsvPreference;
17  import org.supercsv.prefs.CsvPreference.Builder;
18  
19  import java.io.IOException;
20  import java.io.StringReader;
21  import java.lang.reflect.Method;
22  import java.lang.reflect.Type;
23  import java.math.BigDecimal;
24  import java.math.MathContext;
25  import java.text.DecimalFormatSymbols;
26  import java.text.SimpleDateFormat;
27  import java.util.List;
28  import java.util.Map;
29  
30  import static org.supercsv.prefs.CsvPreference.STANDARD_PREFERENCE;
31  
32  /**
33   * Test class which checks all features listed on <a href="http://csveed.org/comparison-matrix.html">comparison page</a>
34   * for "read" operations.
35   *
36   * @author MichaƂ Ziober
37   */
38  public class ReadingFeaturesTest {
39  	
40  	private DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
41  	
42  	@Test
43  	public void testCustomSeparator() throws IOException {
44  		String csv = "John+Connor";
45  		CellProcessor[] processors = { new NotNull(), new NotNull() };
46  		
47  		char customSeparator = '+';
48  		CsvPreference customPreference = new Builder('"', customSeparator, "").build();
49  		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
50  		List<Object> result = listReader.read(processors);
51  		
52  		Assert.assertNotNull(result);
53  		Assert.assertEquals(2, result.size());
54  		Assert.assertEquals("John", result.get(0));
55  		Assert.assertEquals("Connor", result.get(1));
56  	}
57  	
58  	@Test
59  	public void testCustomQuote() throws IOException {
60  		String csv = "|John  Connor|";
61  		CellProcessor[] processors = { new NotNull() };
62  		
63  		char customQuote = '|';
64  		CsvPreference customPreference = new Builder(customQuote, ',', "").build();
65  		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
66  		List<Object> result = listReader.read(processors);
67  		
68  		Assert.assertNotNull(result);
69  		Assert.assertEquals(1, result.size());
70  		Assert.assertEquals("John  Connor", result.get(0));
71  	}
72  	
73  	@Ignore
74  	@Test
75  	public void testCustomEscape() throws IOException {
76  		throw new UnsupportedOperationException("Not implemented yet!");
77  	}
78  	
79  	@Test
80  	public void testCustomEOL() throws IOException {
81  		String csv = "John,Connor\r>\n";
82  		CellProcessor[] processors = { new NotNull(), new NotNull() };
83  		
84  		String customEndOfLine = "\r>\n";
85  		CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
86  		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
87  		List<Object> result = listReader.read(processors);
88  		
89  		Assert.assertNotNull(result);
90  		Assert.assertEquals(2, result.size());
91  		Assert.assertEquals("John", result.get(0));
92  		Assert.assertEquals("Connor", result.get(1));
93  	}
94  	
95  	@Test
96  	public void testNewLineInDelimitedField() throws IOException {
97  		String csv = "\"Jo\nhn\",\"Con\nnor\"\n";
98  		CellProcessor[] processors = { new NotNull(), new NotNull() };
99  		
100 		CsvPreference customPreference = new Builder('"', ',', "\n").build();
101 		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
102 		List<Object> result = listReader.read(processors);
103 		
104 		Assert.assertNotNull(result);
105 		Assert.assertEquals(2, result.size());
106 		Assert.assertEquals("Jo\nhn", result.get(0));
107 		Assert.assertEquals("Con\nnor", result.get(1));
108 	}
109 	
110 	@Test
111 	public void testEscapedQuoteInQuotedField() throws IOException {
112 		String csv = "\"Joh\"\"n\",\"Con\"\"nor\"";
113 		CellProcessor[] processors = { new NotNull(), new NotNull() };
114 		
115 		CsvPreference customPreference = new Builder('"', ',', "").build();
116 		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
117 		List<Object> result = listReader.read(processors);
118 		
119 		Assert.assertNotNull(result);
120 		Assert.assertEquals(2, result.size());
121 		Assert.assertEquals("Joh\"n", result.get(0));
122 		Assert.assertEquals("Con\"nor", result.get(1));
123 	}
124 	
125 	@Ignore
126 	@Test
127 	public void testDifferentEscapeAndQuote() throws IOException {
128 		throw new UnsupportedOperationException("Not implemented yet!");
129 	}
130 	
131 	@Test
132 	public void testDealWithLeadingTrailingWhitespace() throws IOException {
133 		String csv = "     John    ,     Connor   ";
134 		CellProcessor[] processors = { new Trim(), new Trim() };
135 		
136 		char customQuote = '"';
137 		CsvPreference customPreference = new Builder(customQuote, ',', "").build();
138 		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
139 		List<Object> result = listReader.read(processors);
140 		
141 		Assert.assertNotNull(result);
142 		Assert.assertEquals(2, result.size());
143 		Assert.assertEquals("John", result.get(0));
144 		Assert.assertEquals("Connor", result.get(1));
145 	}
146 	
147 	@Test
148 	public void testGetByColumnIndex() throws IOException {
149 		String csv = "John,Connor";
150 		
151 		CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
152 		List<String> line = listReader.read();
153 		
154 		Assert.assertNotNull(line);
155 		Assert.assertEquals(2, line.size());
156 		Assert.assertEquals("John", line.get(0));
157 		Assert.assertEquals("Connor", line.get(1));
158 	}
159 	
160 	@Test
161 	public void testGetByColumnName() throws IOException {
162 		String csv = "John,Connor";
163 		String[] mapping = { "firstName", "lastName" };
164 		
165 		CsvMapReader mapReader = new CsvMapReader(new StringReader(csv), STANDARD_PREFERENCE);
166 		Map<String, String> line = mapReader.read(mapping);
167 		
168 		Assert.assertNotNull(line);
169 		Assert.assertEquals(2, line.size());
170 		Assert.assertEquals("John", line.get("firstName"));
171 		Assert.assertEquals("Connor", line.get("lastName"));
172 	}
173 	
174 	@Test
175 	public void testReadSingleLineStreaming() throws IOException {
176 		String csv = "Sarah,Connor\r\nJohn,Connor\r\nKyle,Reese";
177 		
178 		CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
179 		// skip first line
180 		listReader.read();
181 		// read second line
182 		List<String> line = listReader.read();
183 		
184 		Assert.assertNotNull(line);
185 		Assert.assertEquals(2, line.size());
186 		Assert.assertEquals("John", line.get(0));
187 		Assert.assertEquals("Connor", line.get(1));
188 	}
189 	
190 	@Ignore
191 	@Test
192 	public void testReadAllLines() throws IOException {
193 		throw new UnsupportedOperationException("'reader.readAll()' not implemented yet!");
194 	}
195 	
196 	@Test
197 	public void testSkipCommentLines() throws IOException {
198 		String csv = "<!--Sarah,Connor-->\r\nJohn,Connor\r\n<!--Kyle,Reese-->";
199 		
200 		CsvPreference customPreference = new Builder(STANDARD_PREFERENCE).skipComments(new CommentMatches("<!--.*-->"))
201 			.build();
202 		CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
203 		List<String> line = listReader.read();
204 		List<String> emptyLine = listReader.read();
205 		
206 		Assert.assertNotNull(line);
207 		Assert.assertEquals(2, line.size());
208 		Assert.assertEquals("John", line.get(0));
209 		Assert.assertEquals("Connor", line.get(1));
210 		Assert.assertNull(emptyLine);
211 	}
212 	
213 	@Test
214 	public void testIgnoreEmptyLines() throws IOException {
215 		String csv = "\r\n\n\nJohn,Connor\r\n\r\n";
216 		
217 		CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
218 		List<String> line = listReader.read();
219 		List<String> emptyLine = listReader.read();
220 		
221 		Assert.assertNotNull(line);
222 		Assert.assertEquals(2, line.size());
223 		Assert.assertEquals("John", line.get(0));
224 		Assert.assertEquals("Connor", line.get(1));
225 		Assert.assertNull(emptyLine);
226 	}
227 	
228 	@Ignore
229 	@Test
230 	public void testColumnIndexBasedMapping() throws IOException {
231 		throw new UnsupportedOperationException("not implemented yet!");
232 	}
233 	
234 	@Test
235 	public void testColumnNameBasedMapping() throws IOException {
236 		String csv = "Connor,John\r\n";
237 		String[] mapping = { "lastName", "firstName" };
238 		
239 		CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
240 		FeatureBean character = beanReader.read(FeatureBean.class, mapping);
241 		
242 		Assert.assertNotNull(character);
243 		Assert.assertEquals("John", character.getFirstName());
244 		Assert.assertEquals("Connor", character.getLastName());
245 	}
246 	
247 	@Ignore
248 	@Test
249 	public void testSupportsAnnotations() throws IOException {
250 		throw new UnsupportedOperationException("Annotations are not implemented yet!");
251 	}
252 	
253 	@Test
254 	public void testConvertsToPrimitives() throws IOException {
255 		String csv = "Connor,John,16\r\n";
256 		String[] mapping = { "lastName", "firstName", "age" };
257 		CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt() };
258 		
259 		CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
260 		FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
261 		
262 		Assert.assertNotNull(character);
263 		Assert.assertEquals("John", character.getFirstName());
264 		Assert.assertEquals("Connor", character.getLastName());
265 		Assert.assertEquals(16, character.getAge());
266 	}
267 	
268 	@Test
269 	public void testConvertsToBasicObjects() throws Exception {
270 		String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
271 		String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
272 		CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
273 			new ParseBigDecimal(decimalFormatSymbols) };
274 		
275 		CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
276 		CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
277 		FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
278 		
279 		Assert.assertNotNull(character);
280 		Assert.assertEquals("John", character.getFirstName());
281 		Assert.assertEquals("Connor", character.getLastName());
282 		Assert.assertEquals(16, character.getAge());
283 		Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
284 		Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
285 	}
286 	
287 	@Test
288 	public void testConverterSupport() throws Exception {
289 		String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
290 		String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
291 		CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
292 			new ParseBigDecimal(decimalFormatSymbols) };
293 		
294 		CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
295 		CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
296 		FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
297 		
298 		Assert.assertNotNull(character);
299 		Assert.assertEquals("John", character.getFirstName());
300 		Assert.assertEquals("Connor", character.getLastName());
301 		Assert.assertEquals(16, character.getAge());
302 		Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
303 		Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
304 	}
305 	
306 	@Test
307 	public void testDateSupport() throws Exception {
308 		String csv = "1999-07-12";
309 		String[] mapping = { "birthDate" };
310 		CellProcessor[] processors = { new ParseDate("yyyy-MM-dd") };
311 		
312 		CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
313 		FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
314 		
315 		Assert.assertNotNull(character);
316 		Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
317 	}
318 	
319 	@Test
320 	public void testReturnsTypedBean() throws Exception {
321 		int methodCounter = 0;
322 		Method[] methods = CsvBeanReader.class.getMethods();
323 		for( Method method : methods ) {
324 			if( method.getName().equals("read") ) {
325 				try {
326 					Type genericReturnType = method.getGenericReturnType();
327 					Assert.assertNotNull(genericReturnType);
328 					Assert.assertEquals("T", genericReturnType.getTypeName());
329 					methodCounter++;
330 				}
331 				catch(Exception e) {
332 					Assert.fail(e.getMessage());
333 				}
334 			}
335 		}
336 		Assert.assertTrue(methodCounter > 0);
337 	}
338 	
339 	@Test
340 	public void testDeepConversion() {
341 		Assert.assertNotNull("See org.supercsv.example.dozer.Reading class!");
342 	}
343 	
344 	@Ignore
345 	@Test
346 	public void testSplitCellToMultipleProperties() {
347 		throw new UnsupportedOperationException("Not implemented yet!");
348 	}
349 	
350 	@Ignore
351 	@Test
352 	public void testJoinMultipleCellsIntoOneProperty() {
353 		throw new UnsupportedOperationException("Not implemented yet!");
354 	}
355 }