View Javadoc
1   package org.csveed.bean;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import java.io.IOException;
6   import java.io.StringWriter;
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import org.csveed.test.model.BeanWithMultipleStrings;
11  import org.junit.jupiter.api.Test;
12  
13  public class BeanWriterTest {
14      
15      @Test
16      public void writeBeans() throws IOException {
17          try (StringWriter writer = new StringWriter()) {
18              List<BeanWithMultipleStrings> beans = new ArrayList<>();
19              beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
20              beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
21              beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
22              BeanWriter<BeanWithMultipleStrings> beanWriter =
23                      new BeanWriterImpl<>(writer, BeanWithMultipleStrings.class);
24              beanWriter.writeBeans(beans);
25          
26              assertEquals(
27                  "\"gamma\";\"beta\";\"alpha\"\r\n"+
28                  "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
29                  "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
30                  "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
31                  writer.getBuffer().toString());
32          }
33      }
34  
35      // https://github.com/robert-bor/CSVeed/issues/46
36      @Test
37      public void bug46ReportedByJnash67() throws IOException {
38          try (StringWriter writer = new StringWriter()) {
39              List<BeanWithMultipleStrings> beans = new ArrayList<>();
40              beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
41              beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
42              beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
43              BeanInstructions bi = new BeanInstructionsImpl(BeanWithMultipleStrings.class);
44              bi.logSettings();
45              bi.mapColumnNameToProperty("Aap", "gamma");
46              bi.mapColumnNameToProperty("Noot", "beta");
47              bi.mapColumnNameToProperty("Mies", "alpha");
48              BeanWriter<BeanWithMultipleStrings> beanWriter =
49                  new BeanWriterImpl<>(writer, bi);
50              beanWriter.writeBeans(beans);
51          
52              assertEquals(
53                  "\"Aap\";\"Noot\";\"Mies\"\r\n"+
54                  "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
55                  "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
56                  "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
57                  writer.getBuffer().toString());
58          }
59      }
60  
61      private BeanWithMultipleStrings createBean(String alpha, String beta, String gamma) {
62          BeanWithMultipleStringshtml#BeanWithMultipleStrings">BeanWithMultipleStrings bean = new BeanWithMultipleStrings();
63          bean.setAlpha(alpha);
64          bean.setBeta(beta);
65          bean.setGamma(gamma);
66          return bean;
67      }
68  
69  }