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  
17  package org.supercsv.io;
18  
19  import static org.junit.Assert.*;
20  import static org.supercsv.SuperCsvTestUtils.CSV_FILE;
21  import static org.supercsv.SuperCsvTestUtils.HEADER;
22  import static org.supercsv.SuperCsvTestUtils.CUSTOMERS;
23  import static org.supercsv.SuperCsvTestUtils.STRING_CUSTOMERS;
24  import static org.supercsv.SuperCsvTestUtils.WRITE_PROCESSORS;
25  import static org.supercsv.SuperCsvTestUtils.date;
26  
27  import java.io.IOException;
28  import java.io.StringWriter;
29  import java.io.Writer;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import org.junit.Before;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.supercsv.cellprocessor.FmtDate;
36  import org.supercsv.cellprocessor.ift.CellProcessor;
37  import org.supercsv.exception.SuperCsvCellProcessorException;
38  import org.supercsv.mock.CustomerBean;
39  import org.supercsv.mock.CustomerStringBean;
40  import org.supercsv.mock.ResultSetMock;
41  import org.supercsv.prefs.CsvPreference;
42  
43  /**
44   * Tests the CsvResultSetWriter class
45   * 
46   * @author SingularityFX
47   * 
48   */
49  public class CsvResultSetWriterTest {
50  	
51  	private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
52  	public static Object[][] TEST_DATA_VARIOUS_TYPES;
53  	public static Object[][] TEST_DATA_STRINGS;
54  	
55  	private Writer writer;
56  	private CsvResultSetWriter csvResultSetWriter;
57  		
58  	@BeforeClass
59  	public static void beforeClass() {
60  		TEST_DATA_VARIOUS_TYPES = setUpTestDataCustomerBeans();
61  		TEST_DATA_STRINGS = setUpTestDataCustomerStringBeans();
62  	}
63  	
64  	private static Object[][] setUpTestDataCustomerBeans() {
65  		final Object[][] testData = new Object[10][10];
66  		for (int i = 0; i < 10; i++) {
67  			CustomerBean bean = CUSTOMERS.get(i);
68  			testData[i][0] = bean.getCustomerNo();
69  			testData[i][1] = bean.getFirstName();
70  			testData[i][2] = bean.getLastName();
71  			testData[i][3] = bean.getBirthDate();
72  			testData[i][4] = bean.getMailingAddress();
73  			testData[i][5] = bean.getMarried();
74  			testData[i][6] = bean.getNumberOfKids();
75  			testData[i][7] = bean.getFavouriteQuote();
76  			testData[i][8] = bean.getEmail();
77  			testData[i][9] = bean.getLoyaltyPoints();
78  		}
79  		return testData;
80  	}
81  	
82  	private static Object[][] setUpTestDataCustomerStringBeans() {
83  		final Object[][] testData = new Object[10][10];
84  		for (int i = 0; i < 10; i++) {
85  			CustomerStringBean bean = STRING_CUSTOMERS.get(i);
86  			testData[i][0] = bean.getCustomerNo();
87  			testData[i][1] = bean.getFirstName();
88  			testData[i][2] = bean.getLastName();
89  			testData[i][3] = bean.getBirthDate();
90  			testData[i][4] = bean.getMailingAddress();
91  			testData[i][5] = bean.getMarried();
92  			testData[i][6] = bean.getNumberOfKids();
93  			testData[i][7] = bean.getFavouriteQuote();
94  			testData[i][8] = bean.getEmail();
95  			testData[i][9] = bean.getLoyaltyPoints();
96  		}
97  		return testData;
98  	}
99  	
100 	@Before
101 	public void setUp() {
102 		writer = new StringWriter();
103 		csvResultSetWriter = new CsvResultSetWriter(writer, PREFS);
104 	}
105 	
106 	/**
107 	 * Tests writing ResultSet to a CSV file (no CellProcessors)
108 	 * @throws SQLException 
109 	 */
110 	@Test
111 	public void testWrite() throws IOException, SQLException {
112 		final ResultSet resultSetMock = new ResultSetMock(TEST_DATA_STRINGS, HEADER);
113 		csvResultSetWriter.write(resultSetMock);
114 		csvResultSetWriter.flush();
115 		assertEquals(CSV_FILE, writer.toString());
116 	}
117 	
118 	/**
119 	 * Test writing ResultSet to a CSV file with CellProcessors
120 	 * @throws IOException 
121 	 * @throws SQLException 
122 	 */
123 	@Test
124 	public void testWriteWithProcessors() throws SQLException, IOException {
125 		final ResultSet resultSetMock = new ResultSetMock(TEST_DATA_VARIOUS_TYPES, HEADER);
126 		csvResultSetWriter.write(resultSetMock, WRITE_PROCESSORS);
127 		csvResultSetWriter.flush();
128 		assertEquals(CSV_FILE, writer.toString());
129 	}
130 	
131 	// Tests for NullPointerException follow
132 	
133 	/**
134 	 * Tests the constructor with a null writer
135 	 */
136 	@SuppressWarnings("resource")
137 	@Test(expected = NullPointerException.class)
138 	public void testConstructorWithNullWriter() {
139 		new CsvResultSetWriter(null, PREFS);
140 	}
141 	
142 	/**
143 	 * Tests the constructor with a null CsvPreference
144 	 */
145 	@SuppressWarnings("resource")
146 	@Test(expected = NullPointerException.class)
147 	public void testConstructorWithNullCsvPreference() {
148 		new CsvResultSetWriter(writer, null);
149 	}
150 
151 	/**
152 	 * Tests the write() method with null ResultSet
153 	 * @throws SQLException
154 	 * @throws IOException
155 	 */
156 	@Test(expected = NullPointerException.class)
157 	public void testWriteNullResultSet() throws SQLException, IOException {
158 		csvResultSetWriter.write(null);
159 	}
160 	
161 	/**
162 	 * Test the write() method (with processors) with null ResultSet
163 	 * @throws IOException 
164 	 * @throws SQLException 
165 	 */
166 	@Test(expected = NullPointerException.class)
167 	public void testWriteWithProcessorsNullResultSet() throws SQLException, IOException {
168 		csvResultSetWriter.write(null, WRITE_PROCESSORS);
169 	}
170 	
171 	/**
172 	 * Tests the write() method (with processors) with a null cell processor array
173 	 * @throws IOException 
174 	 * @throws SQLException 
175 	 */
176 	@Test(expected = NullPointerException.class)
177 	public void testWriteNullProcessors() throws SQLException, IOException {
178 
179 		final ResultSet resultSet = new ResultSetMock(TEST_DATA_VARIOUS_TYPES, HEADER);
180 		csvResultSetWriter.write(resultSet, null);
181 	}
182 
183 	/**
184 	 * Test that row/line numbers reported during exception are determined correctly
185 	 * @throws IOException 
186 	 * @throws SQLException 
187 	 */
188 	@Test(expected = SuperCsvCellProcessorException.class)
189 	public void testRowLineNumberCorrectness() throws SQLException, IOException {
190 		final int LINE_NUMBER = 5;
191 		final int ROW_NUMBER = 4;
192 		final Object[][] causesException = {
193 			{"1", "Alexander\r\nGraham", date(1945, 6, 13), },
194 			{"2", "Bob", date(1919, 2, 25), }, 
195 			{"3", "Alice", "CAUSES EXCEPTION", },
196 			{"4", "Bill", date(1973, 7, 10), },
197 			{"5", "Miranda", date(1999, 1, 3), },
198 		};
199 		final String[] headers = {"customerNo", "firstName", "birthDate"};
200 		final ResultSet resultSet = new ResultSetMock(causesException, headers);
201 		final CellProcessor[] cellProcessors = {null, null, new FmtDate("dd/MM/yyyy")};
202 		try {
203 			csvResultSetWriter.write(resultSet, cellProcessors);
204 		} catch(SuperCsvCellProcessorException e) {
205 			final int actualLineNumber = e.getCsvContext().getLineNumber();
206 			final int actualRowNumber = e.getCsvContext().getRowNumber();
207 			assertEquals("line number not correct", LINE_NUMBER, actualLineNumber);
208 			assertEquals("row number not correct", ROW_NUMBER, actualRowNumber);
209 			throw e;
210 		}
211 	}
212 }