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.JOHN;
23 import static org.supercsv.SuperCsvTestUtils.STRING_CUSTOMERS;
24 import static org.supercsv.SuperCsvTestUtils.WRITE_PROCESSORS;
25
26 import java.io.IOException;
27 import java.io.StringWriter;
28 import java.io.Writer;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.supercsv.exception.SuperCsvReflectionException;
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 CsvBeanWriterTest {
44
45 private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
46
47 private Writer writer;
48
49 private CsvBeanWriter beanWriter;
50
51 private CustomerBean customer;
52
53
54
55
56 @Before
57 public void setUp() {
58 writer = new StringWriter();
59 beanWriter = new CsvBeanWriter(writer, PREFS);
60 customer = new CustomerBean();
61 }
62
63
64
65
66 @After
67 public void tearDown() throws IOException {
68 beanWriter.close();
69 }
70
71
72
73
74 @SuppressWarnings("resource")
75 @Test(expected = NullPointerException.class)
76 public void testConstructorWillNullWriter() {
77 new CsvBeanWriter(null, PREFS);
78 }
79
80
81
82
83 @SuppressWarnings("resource")
84 @Test(expected = NullPointerException.class)
85 public void testConstructorWillNullPreference() {
86 new CsvBeanWriter(writer, null);
87 }
88
89
90
91
92 @Test
93 public void testWrite() throws IOException {
94 beanWriter.writeHeader(HEADER);
95 for( CustomerStringBean customer : STRING_CUSTOMERS ) {
96 beanWriter.write(customer, HEADER);
97 }
98 beanWriter.flush();
99 assertEquals(CSV_FILE, writer.toString());
100 }
101
102
103
104
105 @Test
106 public void testWriteProcessors() throws IOException {
107 beanWriter.writeHeader(HEADER);
108 for( CustomerBean customer : CUSTOMERS ) {
109 beanWriter.write(customer, HEADER, WRITE_PROCESSORS);
110 }
111 beanWriter.flush();
112 assertEquals(CSV_FILE, writer.toString());
113 }
114
115
116
117
118 @Test(expected = NullPointerException.class)
119 public void testWriteWithNullSource() throws IOException {
120 beanWriter.write(null, HEADER);
121 }
122
123
124
125
126 @Test(expected = NullPointerException.class)
127 public void testWriteWithNullNameMappingArray() throws IOException {
128 beanWriter.write(customer, (String[]) null);
129 }
130
131
132
133
134 @Test
135 public void testWriteWithNullNameMapping() throws IOException {
136
137 final String[] headerWithNulls = new String[] { "customerNo", null, "firstName", null, "lastName" };
138 final String expectedCsv = JOHN.getCustomerNo() + ",," + JOHN.getFirstName() + ",," + JOHN.getLastName()
139 + "\r\n";
140
141 beanWriter.write(JOHN, headerWithNulls);
142 beanWriter.flush();
143 assertEquals(expectedCsv, writer.toString());
144 }
145
146
147
148
149 @Test(expected = NullPointerException.class)
150 public void testWriteProcessorsWithNullSource() throws IOException {
151 beanWriter.write(null, HEADER, WRITE_PROCESSORS);
152 }
153
154
155
156
157 @Test(expected = NullPointerException.class)
158 public void testWriteProcessorsWithNullNameMapping() throws IOException {
159 beanWriter.write(customer, null, WRITE_PROCESSORS);
160 }
161
162
163
164
165 @Test(expected = NullPointerException.class)
166 public void testWriteProcessorsWithNullProcessors() throws IOException {
167 beanWriter.write(customer, HEADER, null);
168
169 }
170
171
172
173
174 @Test(expected = SuperCsvReflectionException.class)
175 public void testGetterThrowingException() throws IOException {
176 beanWriter.write(new ExceptionBean(), "exception");
177 }
178
179
180
181
182 public static class ExceptionBean extends CustomerBean {
183
184 public String getException() {
185 throw new RuntimeException("oops!");
186 }
187
188 }
189 }