1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
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
53
54 @Before
55 public void setUp() {
56 writer = new StringWriter();
57 listWriter = new CsvListWriter(writer, PREFS);
58 }
59
60
61
62
63 @After
64 public void tearDown() throws IOException {
65 listWriter.close();
66 }
67
68
69
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
87
88 @Test(expected = NullPointerException.class)
89 public void testWriteListAndProcessorsWithNullList() throws IOException {
90 listWriter.write(null, WRITE_PROCESSORS);
91 }
92
93
94
95
96 @Test(expected = NullPointerException.class)
97 public void testWriteListAndProcessorsWithNullProcessors() throws IOException {
98 listWriter.write(Arrays.asList(""), null);
99 }
100
101
102
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
120
121 @Test(expected = NullPointerException.class)
122 public void testWriteListWithNullList() throws IOException {
123 listWriter.write((List<?>) null);
124 }
125
126
127
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
145
146 @Test(expected = NullPointerException.class)
147 public void testWriteObjectArrayWithNullArray() throws IOException {
148 listWriter.write((Object[]) null);
149 }
150
151
152
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
170
171 @Test(expected = NullPointerException.class)
172 public void testWriteStringArrayWithNullArray() throws IOException {
173 listWriter.write((String[]) null);
174 }
175
176
177
178
179 @SuppressWarnings("resource")
180 @Test(expected = NullPointerException.class)
181 public void testConstructorWillNullWriter() {
182 new CsvListWriter(null, PREFS);
183 }
184
185
186
187
188 @SuppressWarnings("resource")
189 @Test(expected = NullPointerException.class)
190 public void testConstructorWillNullPreference() {
191 new CsvListWriter(writer, null);
192 }
193
194 }