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.assertEquals;
19  import static org.supercsv.SuperCsvTestUtils.CSV_FILE;
20  import static org.supercsv.SuperCsvTestUtils.CUSTOMERS;
21  import static org.supercsv.SuperCsvTestUtils.HEADER;
22  import static org.supercsv.SuperCsvTestUtils.STRING_CUSTOMERS;
23  import static org.supercsv.SuperCsvTestUtils.WRITE_PROCESSORS;
24  
25  import java.io.IOException;
26  import java.io.StringWriter;
27  import java.io.Writer;
28  import java.util.Arrays;
29  import java.util.List;
30  
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.supercsv.mock.CustomerBean;
35  import org.supercsv.mock.CustomerStringBean;
36  import org.supercsv.prefs.CsvPreference;
37  
38  /**
39   * Tests the CsvListWriter class.
40   * 
41   * @author James Bassett
42   */
43  public class CsvListWriterTest {
44  	
45  	private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
46  	
47  	private Writer writer;
48  	
49  	private CsvListWriter listWriter;
50  	
51  	/**
52  	 * Sets up the writer for the tests.
53  	 */
54  	@Before
55  	public void setUp() {
56  		writer = new StringWriter();
57  		listWriter = new CsvListWriter(writer, PREFS);
58  	}
59  	
60  	/**
61  	 * Closes the list writer after the test.
62  	 */
63  	@After
64  	public void tearDown() throws IOException {
65  		listWriter.close();
66  	}
67  	
68  	/**
69  	 * Tests the write() method with a List and array of CellProcessors.
70  	 */
71  	@Test
72  	public void testWriteListAndProcessors() throws IOException {
73  		listWriter.writeHeader(HEADER);
74  		for( CustomerBean customer : CUSTOMERS ) {
75  			final List<Object> customerList = Arrays.asList(new Object[] { customer.getCustomerNo(),
76  				customer.getFirstName(), customer.getLastName(), customer.getBirthDate(), customer.getMailingAddress(),
77  				customer.getMarried(), customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
78  				customer.getLoyaltyPoints() });
79  			listWriter.write(customerList, WRITE_PROCESSORS);
80  		}
81  		listWriter.flush();
82  		assertEquals(CSV_FILE, writer.toString());
83  	}
84  	
85  	/**
86  	 * Tests the write() method with a null List and array of CellProcessors.
87  	 */
88  	@Test(expected = NullPointerException.class)
89  	public void testWriteListAndProcessorsWithNullList() throws IOException {
90  		listWriter.write(null, WRITE_PROCESSORS);
91  	}
92  	
93  	/**
94  	 * Tests the write() method with a List and null array of CellProcessors.
95  	 */
96  	@Test(expected = NullPointerException.class)
97  	public void testWriteListAndProcessorsWithNullProcessors() throws IOException {
98  		listWriter.write(Arrays.asList(""), null);
99  	}
100 	
101 	/**
102 	 * Tests the write() method with a List.
103 	 */
104 	@Test
105 	public void testWriteList() throws IOException {
106 		listWriter.writeHeader(HEADER);
107 		for( CustomerStringBean customer : STRING_CUSTOMERS ) {
108 			final List<Object> customerList = Arrays.asList(new Object[] { customer.getCustomerNo(),
109 				customer.getFirstName(), customer.getLastName(), customer.getBirthDate(), customer.getMailingAddress(),
110 				customer.getMarried(), customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
111 				customer.getLoyaltyPoints() });
112 			listWriter.write(customerList);
113 		}
114 		listWriter.flush();
115 		assertEquals(CSV_FILE, writer.toString());
116 	}
117 	
118 	/**
119 	 * Tests the write() method with a null List.
120 	 */
121 	@Test(expected = NullPointerException.class)
122 	public void testWriteListWithNullList() throws IOException {
123 		listWriter.write((List<?>) null);
124 	}
125 	
126 	/**
127 	 * Tests the write() method with an Object array.
128 	 */
129 	@Test
130 	public void testWriteObjectArray() throws Exception {
131 		listWriter.writeHeader(HEADER);
132 		for( CustomerStringBean customer : STRING_CUSTOMERS ) {
133 			final Object[] customerArray = new Object[] { customer.getCustomerNo(), customer.getFirstName(),
134 				customer.getLastName(), customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
135 				customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
136 				customer.getLoyaltyPoints() };
137 			listWriter.write(customerArray);
138 		}
139 		listWriter.flush();
140 		assertEquals(CSV_FILE, writer.toString());
141 	}
142 	
143 	/**
144 	 * Tests the write() method with a null Object array.
145 	 */
146 	@Test(expected = NullPointerException.class)
147 	public void testWriteObjectArrayWithNullArray() throws IOException {
148 		listWriter.write((Object[]) null);
149 	}
150 	
151 	/**
152 	 * Tests the write() method with a String array.
153 	 */
154 	@Test
155 	public void testWriteStringArray() throws IOException {
156 		listWriter.writeHeader(HEADER);
157 		for( CustomerStringBean customer : STRING_CUSTOMERS ) {
158 			final String[] customerArray = new String[] { customer.getCustomerNo(), customer.getFirstName(),
159 				customer.getLastName(), customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
160 				customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
161 				customer.getLoyaltyPoints() };
162 			listWriter.write(customerArray);
163 		}
164 		listWriter.flush();
165 		assertEquals(CSV_FILE, writer.toString());
166 	}
167 	
168 	/**
169 	 * Tests the write() method with a null String array.
170 	 */
171 	@Test(expected = NullPointerException.class)
172 	public void testWriteStringArrayWithNullArray() throws IOException {
173 		listWriter.write((String[]) null);
174 	}
175 	
176 	/**
177 	 * Tests the constructor with a null writer.
178 	 */
179 	@SuppressWarnings("resource")
180 	@Test(expected = NullPointerException.class)
181 	public void testConstructorWillNullWriter() {
182 		new CsvListWriter(null, PREFS);
183 	}
184 	
185 	/**
186 	 * Tests the constructor with a null CsvPreference.
187 	 */
188 	@SuppressWarnings("resource")
189 	@Test(expected = NullPointerException.class)
190 	public void testConstructorWillNullPreference() {
191 		new CsvListWriter(writer, null);
192 	}
193 	
194 }