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.HashMap;
30 import java.util.Map;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.supercsv.mock.CustomerBean;
36 import org.supercsv.mock.CustomerStringBean;
37 import org.supercsv.prefs.CsvPreference;
38 import org.supercsv.util.Util;
39
40
41
42
43
44
45 public class CsvMapWriterTest {
46
47 private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
48
49 private Writer writer;
50
51 private CsvMapWriter mapWriter;
52
53
54
55
56 @Before
57 public void setUp() {
58 writer = new StringWriter();
59 mapWriter = new CsvMapWriter(writer, PREFS);
60 }
61
62
63
64
65 @After
66 public void tearDown() throws IOException {
67 mapWriter.close();
68 }
69
70
71
72
73 @SuppressWarnings("resource")
74 @Test(expected = NullPointerException.class)
75 public void testConstructorWillNullWriter() {
76 new CsvMapWriter(null, PREFS);
77 }
78
79
80
81
82 @SuppressWarnings("resource")
83 @Test(expected = NullPointerException.class)
84 public void testConstructorWillNullPreference() {
85 new CsvMapWriter(writer, null);
86 }
87
88
89
90
91
92
93 @Test
94 public void testWrite() throws IOException {
95 mapWriter.writeHeader(HEADER);
96 for( CustomerStringBean customer : STRING_CUSTOMERS ) {
97 Map<String, Object> customerMap = new HashMap<String, Object>();
98 Util.filterListToMap(
99 customerMap,
100 HEADER,
101 Arrays.asList(new String[] { customer.getCustomerNo(), customer.getFirstName(), customer.getLastName(),
102 customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
103 customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
104 customer.getLoyaltyPoints() }));
105 mapWriter.write(customerMap, HEADER);
106 }
107 mapWriter.flush();
108 assertEquals(CSV_FILE, writer.toString());
109 }
110
111
112
113
114 @Test
115 public void testWriteProcessors() throws IOException {
116 mapWriter.writeHeader(HEADER);
117 for( CustomerBean customer : CUSTOMERS ) {
118 Map<String, Object> customerMap = new HashMap<String, Object>();
119 Util.filterListToMap(
120 customerMap,
121 HEADER,
122 Arrays.asList(new Object[] { customer.getCustomerNo(), customer.getFirstName(), customer.getLastName(),
123 customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
124 customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
125 customer.getLoyaltyPoints() }));
126 mapWriter.write(customerMap, HEADER, WRITE_PROCESSORS);
127 }
128 mapWriter.flush();
129 assertEquals(CSV_FILE, writer.toString());
130 }
131
132
133
134
135 @Test(expected = NullPointerException.class)
136 public void testWriteWithNullMap() throws IOException {
137 mapWriter.write(null, HEADER);
138 }
139
140
141
142
143 @Test(expected = NullPointerException.class)
144 public void testWriteWithNullNameMapping() throws IOException {
145 mapWriter.write(new HashMap<String, Object>(), (String[]) null);
146 }
147
148
149
150
151 @Test(expected = NullPointerException.class)
152 public void testWriteProcessorsWithNullMap() throws IOException {
153 mapWriter.write(null, HEADER, WRITE_PROCESSORS);
154 }
155
156
157
158
159 @Test(expected = NullPointerException.class)
160 public void testWriteProcessorsWithNullNameMapping() throws IOException {
161 mapWriter.write(new HashMap<String, Object>(), null, WRITE_PROCESSORS);
162
163 }
164
165
166
167
168 @Test(expected = NullPointerException.class)
169 public void testWriteProcessorsWithNullProcessors() throws IOException {
170 mapWriter.write(new HashMap<String, Object>(), HEADER, null);
171
172 }
173
174 }