1 package org.supercsv.features;
2
3 import org.junit.Assert;
4 import org.junit.Ignore;
5 import org.junit.Test;
6 import org.supercsv.cellprocessor.FmtDate;
7 import org.supercsv.cellprocessor.FmtNumber;
8 import org.supercsv.cellprocessor.Trim;
9 import org.supercsv.cellprocessor.constraint.NotNull;
10 import org.supercsv.cellprocessor.ift.CellProcessor;
11 import org.supercsv.io.CsvBeanWriter;
12 import org.supercsv.io.CsvListWriter;
13 import org.supercsv.io.CsvMapWriter;
14 import org.supercsv.prefs.CsvPreference;
15 import org.supercsv.prefs.CsvPreference.Builder;
16
17 import java.io.IOException;
18 import java.io.StringWriter;
19 import java.math.BigDecimal;
20 import java.text.DecimalFormat;
21 import java.text.DecimalFormatSymbols;
22 import java.util.Arrays;
23 import java.util.Calendar;
24 import java.util.Collections;
25 import java.util.List;
26
27 import static org.supercsv.prefs.CsvPreference.STANDARD_PREFERENCE;
28
29
30
31
32
33
34
35 public class WritingFeaturesTest {
36
37 @Test
38 public void testCustomSeparator() throws IOException {
39 List<String> data = Arrays.asList("John", "Connor");
40 CellProcessor[] processors = { new NotNull(), new NotNull() };
41
42 char customSeparator = '+';
43 CsvPreference customPreference = new Builder('"', customSeparator, "").build();
44 String result = writeToCsv(data, processors, customPreference);
45
46 Assert.assertEquals("John+Connor", result);
47 }
48
49 @Test
50 public void testCustomQuote() throws IOException {
51 List<String> data = Collections.singletonList("John \n Connor");
52 CellProcessor[] processors = { new NotNull() };
53
54 char customQuote = '|';
55 CsvPreference customPreference = new Builder(customQuote, ',', "").build();
56 String result = writeToCsv(data, processors, customPreference);
57
58 Assert.assertEquals("|John Connor|", result);
59 }
60
61 @Ignore
62 @Test
63 public void testCustomEscape() throws IOException {
64 throw new UnsupportedOperationException("Not implemented yet!");
65 }
66
67 @Test
68 public void testCustomEOL() throws IOException {
69 List<String> data = Collections.singletonList("John Connor");
70 CellProcessor[] processors = { new NotNull() };
71
72 String customEndOfLine = ">\r\n";
73 CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
74 String result = writeToCsv(data, processors, customPreference);
75
76 Assert.assertEquals("John Connor>\r\n", result);
77 }
78
79 @Test
80 public void testNewLineInDelimitedField() throws IOException {
81 List<String> data = Arrays.asList("Jo\nhn", "Con\nnor");
82 CellProcessor[] processors = { new NotNull(), new NotNull() };
83
84 CsvPreference customPreference = new Builder('"', ',', "\n").build();
85 String result = writeToCsv(data, processors, customPreference);
86
87 Assert.assertEquals("\"Jo\nhn\",\"Con\nnor\"\n", result);
88 }
89
90 @Test
91 public void testEscapedQuoteInQuotedField() throws IOException {
92 List<String> data = Arrays.asList("Joh\"n", "Con\"nor");
93 CellProcessor[] processors = { new NotNull(), new NotNull() };
94
95 CsvPreference customPreference = new Builder('"', ',', "").build();
96 String result = writeToCsv(data, processors, customPreference);
97
98 Assert.assertEquals("\"Joh\"\"n\",\"Con\"\"nor\"", result);
99 }
100
101 @Ignore
102 @Test
103 public void testDifferentEscapeAndQuote() throws IOException {
104 throw new UnsupportedOperationException("Not implemented yet!");
105 }
106
107 @Test
108 public void testDealWithLeadingTrailingWhitespace() throws IOException {
109 List<String> data = Arrays.asList(" John ", " Connor ");
110 CellProcessor[] processors = { new Trim(), new Trim() };
111
112 char customQuote = '"';
113 CsvPreference customPreference = new Builder(customQuote, ',', "").surroundingSpacesNeedQuotes(false).build();
114 String result = writeToCsv(data, processors, customPreference);
115
116 Assert.assertEquals("John,Connor", result);
117 }
118
119 @Ignore
120 @Test
121 public void testColumnIndexBasedMapping() throws IOException {
122 throw new UnsupportedOperationException("not implemented yet!");
123 }
124
125 @Test
126 public void testColumnNameBasedMapping() throws IOException {
127 FeatureBean character = new FeatureBean("John", "Connor", 16);
128 String[] mapping = { "lastName", "firstName" };
129 StringWriter writer = new StringWriter();
130 CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE);
131 beanWriter.write(character, mapping);
132 beanWriter.close();
133
134 String csv = writer.toString();
135 Assert.assertNotNull(csv);
136 Assert.assertEquals("Connor,John\r\n", csv);
137 }
138
139 @Ignore
140 @Test
141 public void testSupportsAnnotations() throws IOException {
142 throw new UnsupportedOperationException("Annotations are not implemented yet!");
143 }
144
145 @Test
146 public void testConvertsToPrimitives() throws IOException {
147 FeatureBean character = new FeatureBean("John", "Connor", 16);
148 String[] mapping = { "lastName", "firstName", "age" };
149
150 StringWriter writer = new StringWriter();
151 CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE);
152 beanWriter.write(character, mapping);
153 beanWriter.close();
154
155 String csv = writer.toString();
156 Assert.assertNotNull(csv);
157 Assert.assertEquals("Connor,John,16\r\n", csv);
158 }
159
160 @Test
161 public void testConvertsToBasicObjects() throws IOException {
162 Calendar calendar = Calendar.getInstance();
163 calendar.set(Calendar.YEAR, 1999);
164 calendar.set(Calendar.MONTH, 6);
165 calendar.set(Calendar.DAY_OF_MONTH, 12);
166
167 FeatureBean character = new FeatureBean("John", "Connor", 16);
168 character.setSavings(new BigDecimal(6.65));
169 character.setBirthDate(calendar.getTime());
170
171 String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
172 DecimalFormat formatter = new DecimalFormat();
173 formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
174 CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
175 new FmtNumber(formatter) };
176
177 StringWriter writer = new StringWriter();
178 CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
179 CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
180 beanWriter.write(character, mapping, processors);
181 beanWriter.close();
182
183 String csv = writer.toString();
184 Assert.assertNotNull(csv);
185 Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
186 }
187
188 @Test
189 public void testConverterSupport() throws IOException {
190 Calendar calendar = Calendar.getInstance();
191 calendar.set(Calendar.YEAR, 1999);
192 calendar.set(Calendar.MONTH, 6);
193 calendar.set(Calendar.DAY_OF_MONTH, 12);
194
195 FeatureBean character = new FeatureBean("John", "Connor", 16);
196 character.setSavings(new BigDecimal(6.65));
197 character.setBirthDate(calendar.getTime());
198
199 String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
200 DecimalFormat formatter = new DecimalFormat();
201 formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
202 CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
203 new FmtNumber(formatter) };
204
205 StringWriter writer = new StringWriter();
206 CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
207 CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
208 beanWriter.write(character, mapping, processors);
209 beanWriter.close();
210
211 String csv = writer.toString();
212 Assert.assertNotNull(csv);
213 Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
214 }
215
216 @Test
217 public void testDateSupport() throws IOException {
218 Calendar calendar = Calendar.getInstance();
219 calendar.set(Calendar.YEAR, 1999);
220 calendar.set(Calendar.MONTH, 6);
221 calendar.set(Calendar.DAY_OF_MONTH, 12);
222
223 FeatureBean character = new FeatureBean("John", "Connor", 16);
224 character.setBirthDate(calendar.getTime());
225
226 String[] mapping = { "birthDate" };
227 DecimalFormat formatter = new DecimalFormat();
228 formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
229 CellProcessor[] processors = { new FmtDate("yyyy-MM-dd") };
230
231 StringWriter writer = new StringWriter();
232 CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE);
233 beanWriter.write(character, mapping, processors);
234 beanWriter.close();
235
236 String csv = writer.toString();
237 Assert.assertNotNull(csv);
238 Assert.assertEquals("1999-07-12\r\n", csv);
239 }
240
241 @Test
242 public void testDeepConversion() {
243 Assert.assertNotNull("See org.supercsv.example.dozer.Writing class!");
244 }
245
246 @Ignore
247 @Test
248 public void testSplitCellToMultipleProperties() {
249 throw new UnsupportedOperationException("Not implemented yet!");
250 }
251
252 @Ignore
253 @Test
254 public void testJoinMultipleCellsIntoOneProperty() {
255 throw new UnsupportedOperationException("Not implemented yet!");
256 }
257
258 @Test
259 public void testWriteToWriter() throws IOException {
260 StringWriter writer = new StringWriter();
261 new CsvListWriter(writer, STANDARD_PREFERENCE);
262 new CsvMapWriter(writer, STANDARD_PREFERENCE);
263 new CsvBeanWriter(writer, STANDARD_PREFERENCE);
264 }
265
266 private String writeToCsv(List<String> data, CellProcessor[] processors, CsvPreference preference)
267 throws IOException {
268 StringWriter writer = new StringWriter();
269 CsvListWriter listWriter = new CsvListWriter(writer, preference);
270 listWriter.write(data, processors);
271 listWriter.close();
272
273 return writer.toString();
274 }
275 }