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.io;
17  
18  import static org.junit.Assert.assertArrayEquals;
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import static org.supercsv.SuperCsvTestUtils.CSV_FILE;
22  import static org.supercsv.SuperCsvTestUtils.CUSTOMERS;
23  import static org.supercsv.SuperCsvTestUtils.HEADER;
24  import static org.supercsv.SuperCsvTestUtils.PARTIAL_HEADER;
25  import static org.supercsv.SuperCsvTestUtils.READ_PROCESSORS;
26  import static org.supercsv.SuperCsvTestUtils.STRING_CUSTOMERS;
27  
28  import java.io.IOException;
29  import java.io.Reader;
30  import java.io.StringReader;
31  import java.util.Map;
32  
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.supercsv.prefs.CsvPreference;
37  
38  /**
39   * Tests the CsvMapReader class.
40   * 
41   * @author James Bassett
42   */
43  public class CsvMapReaderTest {
44  	
45  	private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
46  	
47  	private Reader reader;
48  	
49  	private CsvMapReader mapReader;
50  	
51  	private CsvMapReader tokenizerMapReader;
52  	
53  	/**
54  	 * Sets up the reader for the tests.
55  	 */
56  	@Before
57  	public void setUp() {
58  		reader = new StringReader(CSV_FILE);
59  		mapReader = new CsvMapReader(reader, PREFS);
60  		
61  		final Tokenizer tokenizer = new Tokenizer(reader, PREFS);
62  		tokenizerMapReader = new CsvMapReader(tokenizer, PREFS);
63  	}
64  	
65  	/**
66  	 * Closes the readers after the test.
67  	 */
68  	@After
69  	public void tearDown() throws IOException {
70  		mapReader.close();
71  		tokenizerMapReader.close();
72  	}
73  	
74  	/**
75  	 * Tests the read() method.
76  	 */
77  	@Test
78  	public void testRead() throws IOException {
79  		
80  		final String[] header = mapReader.getHeader(true);
81  		assertArrayEquals(HEADER, header);
82  		
83  		int i = 0;
84  		Map<String, String> customer;
85  		while( (customer = mapReader.read(header)) != null ) {
86  			assertEquals(STRING_CUSTOMERS.get(i).getCustomerNo(), customer.get("customerNo"));
87  			assertEquals(STRING_CUSTOMERS.get(i).getFirstName(), customer.get("firstName"));
88  			assertEquals(STRING_CUSTOMERS.get(i).getLastName(), customer.get("lastName"));
89  			assertEquals(STRING_CUSTOMERS.get(i).getBirthDate(), customer.get("birthDate"));
90  			assertEquals(STRING_CUSTOMERS.get(i).getMailingAddress(), customer.get("mailingAddress"));
91  			assertEquals(STRING_CUSTOMERS.get(i).getMarried(), customer.get("married"));
92  			assertEquals(STRING_CUSTOMERS.get(i).getNumberOfKids(), customer.get("numberOfKids"));
93  			assertEquals(STRING_CUSTOMERS.get(i).getFavouriteQuote(), customer.get("favouriteQuote"));
94  			assertEquals(STRING_CUSTOMERS.get(i).getEmail(), customer.get("email"));
95  			assertEquals(STRING_CUSTOMERS.get(i).getLoyaltyPoints(), customer.get("loyaltyPoints"));
96  			i++;
97  		}
98  		
99  		assertEquals(STRING_CUSTOMERS.size() + 1, mapReader.getRowNumber());
100 	}
101 	
102 	/**
103 	 * Tests the read() method, but only mapping a few columns.
104 	 */
105 	@Test
106 	public void testPartialRead() throws IOException {
107 		
108 		assertArrayEquals(HEADER, mapReader.getHeader(true));
109 		
110 		int i = 0;
111 		Map<String, String> customer;
112 		while( (customer = mapReader.read(PARTIAL_HEADER)) != null ) {
113 			assertNull(customer.get("customerNo"));
114 			assertEquals(STRING_CUSTOMERS.get(i).getFirstName(), customer.get("firstName"));
115 			assertEquals(STRING_CUSTOMERS.get(i).getLastName(), customer.get("lastName"));
116 			assertNull(customer.get("birthDate"));
117 			assertNull(customer.get("mailingAddress"));
118 			assertNull(customer.get("married"));
119 			assertNull(customer.get("numberOfKids"));
120 			assertNull(customer.get("favouriteQuote"));
121 			assertEquals(STRING_CUSTOMERS.get(i).getEmail(), customer.get("email"));
122 			assertNull(customer.get("loyaltyPoints"));
123 			i++;
124 		}
125 		
126 		assertEquals(STRING_CUSTOMERS.size() + 1, mapReader.getRowNumber());
127 	}
128 	
129 	/**
130 	 * Tests the read() method with processors.
131 	 */
132 	@Test
133 	public void testReadWithProcessors() throws IOException {
134 		
135 		final String[] header = mapReader.getHeader(true);
136 		assertArrayEquals(HEADER, header);
137 		
138 		int i = 0;
139 		Map<String, Object> customer;
140 		while( (customer = mapReader.read(header, READ_PROCESSORS)) != null ) {
141 			assertEquals(CUSTOMERS.get(i).getCustomerNo(), customer.get("customerNo"));
142 			assertEquals(CUSTOMERS.get(i).getFirstName(), customer.get("firstName"));
143 			assertEquals(CUSTOMERS.get(i).getLastName(), customer.get("lastName"));
144 			assertEquals(CUSTOMERS.get(i).getBirthDate(), customer.get("birthDate"));
145 			assertEquals(CUSTOMERS.get(i).getMailingAddress(), customer.get("mailingAddress"));
146 			assertEquals(CUSTOMERS.get(i).getMarried(), customer.get("married"));
147 			assertEquals(CUSTOMERS.get(i).getNumberOfKids(), customer.get("numberOfKids"));
148 			assertEquals(CUSTOMERS.get(i).getFavouriteQuote(), customer.get("favouriteQuote"));
149 			assertEquals(CUSTOMERS.get(i).getEmail(), customer.get("email"));
150 			assertEquals(CUSTOMERS.get(i).getLoyaltyPoints(), customer.get("loyaltyPoints"));
151 			i++;
152 		}
153 		
154 		assertEquals(CUSTOMERS.size() + 1, mapReader.getRowNumber());
155 	}
156 	
157 	/**
158 	 * Tests the read() method with processors, but only mapping a few columns.
159 	 */
160 	@Test
161 	public void testPartialReadWithProcessors() throws IOException {
162 		
163 		assertArrayEquals(HEADER, mapReader.getHeader(true));
164 		
165 		int i = 0;
166 		Map<String, Object> customer;
167 		while( (customer = mapReader.read(PARTIAL_HEADER, READ_PROCESSORS)) != null ) {
168 			assertNull(customer.get("customerNo"));
169 			assertEquals(CUSTOMERS.get(i).getFirstName(), customer.get("firstName"));
170 			assertEquals(CUSTOMERS.get(i).getLastName(), customer.get("lastName"));
171 			assertNull(customer.get("birthDate"));
172 			assertNull(customer.get("mailingAddress"));
173 			assertNull(customer.get("married"));
174 			assertNull(customer.get("numberOfKids"));
175 			assertNull(customer.get("favouriteQuote"));
176 			assertEquals(CUSTOMERS.get(i).getEmail(), customer.get("email"));
177 			assertNull(customer.get("loyaltyPoints"));
178 			i++;
179 		}
180 		
181 		assertEquals(CUSTOMERS.size() + 1, mapReader.getRowNumber());
182 	}
183 	
184 	/**
185 	 * Tests the read() method using the tokenizer version of CsvMapReader (just to make sure it behaves exactly the
186 	 * same as the reader version).
187 	 */
188 	@Test
189 	public void testReadUsingTokenizerReader() throws IOException {
190 		
191 		final String[] header = tokenizerMapReader.getHeader(true);
192 		assertArrayEquals(HEADER, header);
193 		
194 		int i = 0;
195 		Map<String, String> customer;
196 		while( (customer = tokenizerMapReader.read(HEADER)) != null ) {
197 			assertEquals(STRING_CUSTOMERS.get(i).getCustomerNo(), customer.get("customerNo"));
198 			assertEquals(STRING_CUSTOMERS.get(i).getFirstName(), customer.get("firstName"));
199 			assertEquals(STRING_CUSTOMERS.get(i).getLastName(), customer.get("lastName"));
200 			assertEquals(STRING_CUSTOMERS.get(i).getBirthDate(), customer.get("birthDate"));
201 			assertEquals(STRING_CUSTOMERS.get(i).getMailingAddress(), customer.get("mailingAddress"));
202 			assertEquals(STRING_CUSTOMERS.get(i).getMarried(), customer.get("married"));
203 			assertEquals(STRING_CUSTOMERS.get(i).getNumberOfKids(), customer.get("numberOfKids"));
204 			assertEquals(STRING_CUSTOMERS.get(i).getFavouriteQuote(), customer.get("favouriteQuote"));
205 			assertEquals(STRING_CUSTOMERS.get(i).getEmail(), customer.get("email"));
206 			assertEquals(STRING_CUSTOMERS.get(i).getLoyaltyPoints(), customer.get("loyaltyPoints"));
207 			i++;
208 		}
209 		
210 		assertEquals(STRING_CUSTOMERS.size() + 1, tokenizerMapReader.getRowNumber());
211 	}
212 	
213 	/**
214 	 * Tests the read() method, with a null name mapping array.
215 	 */
216 	@Test(expected = NullPointerException.class)
217 	public void testReadWithNullNameMapping() throws IOException {
218 		mapReader.read((String[]) null);
219 	}
220 	
221 	/**
222 	 * Tests the read() method (with processors), with a null cell processor array.
223 	 */
224 	@Test(expected = NullPointerException.class)
225 	public void testReadProcessorsWithNullProcessors() throws IOException {
226 		mapReader.read(HEADER, null);
227 	}
228 	
229 	/**
230 	 * Tests the read() method (with processors), with a null name mapping array.
231 	 */
232 	@Test(expected = NullPointerException.class)
233 	public void testReadProcessorsWithNullNameMapping() throws IOException {
234 		mapReader.read((String[]) null, READ_PROCESSORS);
235 	}
236 	
237 	/**
238 	 * Tests the Reader constructor with a null Reader.
239 	 */
240 	@SuppressWarnings("resource")
241 	@Test(expected = NullPointerException.class)
242 	public void testReaderConstructorWithNullReader() {
243 		new CsvMapReader((Reader) null, PREFS);
244 	}
245 	
246 	/**
247 	 * Tests the Reader constructor with a null preference.
248 	 */
249 	@SuppressWarnings("resource")
250 	@Test(expected = NullPointerException.class)
251 	public void testReaderConstructorWithNullPreferences() {
252 		new CsvMapReader(reader, null);
253 	}
254 	
255 	/**
256 	 * Tests the Tokenizer constructor with a null Reader.
257 	 */
258 	@SuppressWarnings("resource")
259 	@Test(expected = NullPointerException.class)
260 	public void testTokenizerConstructorWithNullReader() {
261 		new CsvMapReader((Tokenizer) null, PREFS);
262 	}
263 	
264 	/**
265 	 * Tests the Tokenizer constructor with a null preference.
266 	 */
267 	@SuppressWarnings("resource")
268 	@Test(expected = NullPointerException.class)
269 	public void testTokenizerConstructorWithNullPreferences() {
270 		new CsvMapReader(new Tokenizer(reader, PREFS), null);
271 	}
272 	
273 }