1
2
3
4
5
6
7
8
9
10
11 package org.csveed.bean;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14
15 import java.io.IOException;
16 import java.io.StringWriter;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.csveed.test.model.BeanWithMultipleStrings;
21 import org.junit.jupiter.api.Test;
22
23
24
25
26 class BeanWriterTest {
27
28
29
30
31
32
33
34 @Test
35 void writeBeans() throws IOException {
36 try (StringWriter writer = new StringWriter()) {
37 List<BeanWithMultipleStrings> beans = new ArrayList<>();
38 beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
39 beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
40 beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
41 BeanWriter<BeanWithMultipleStrings> beanWriter = new BeanWriterImpl<>(writer,
42 BeanWithMultipleStrings.class);
43 beanWriter.writeBeans(beans);
44
45 assertEquals(
46 "\"gamma\";\"beta\";\"alpha\"\r\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"
47 + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"
48 + "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
49 writer.getBuffer().toString());
50 }
51 }
52
53
54
55
56
57
58
59
60 @Test
61 void bug46ReportedByJnash67() throws IOException {
62 try (StringWriter writer = new StringWriter()) {
63 List<BeanWithMultipleStrings> beans = new ArrayList<>();
64 beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
65 beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
66 beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
67 BeanInstructions bi = new BeanInstructionsImpl(BeanWithMultipleStrings.class);
68 bi.logSettings();
69 bi.mapColumnNameToProperty("Aap", "gamma");
70 bi.mapColumnNameToProperty("Noot", "beta");
71 bi.mapColumnNameToProperty("Mies", "alpha");
72 BeanWriter<BeanWithMultipleStrings> beanWriter = new BeanWriterImpl<>(writer, bi);
73 beanWriter.writeBeans(beans);
74
75 assertEquals(
76 "\"Aap\";\"Noot\";\"Mies\"\r\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"
77 + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"
78 + "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
79 writer.getBuffer().toString());
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95 private BeanWithMultipleStrings createBean(String alpha, String beta, String gamma) {
96 BeanWithMultipleStrings bean = new BeanWithMultipleStrings();
97 bean.setAlpha(alpha);
98 bean.setBeta(beta);
99 bean.setGamma(gamma);
100 return bean;
101 }
102
103 }