View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
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   * The Class BeanWriterTest.
25   */
26  class BeanWriterTest {
27  
28      /**
29       * Write beans.
30       *
31       * @throws IOException
32       *             Signals that an I/O exception has occurred.
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       * Bug 46 reported by jnash 67.
55       *
56       * @throws IOException
57       *             Signals that an I/O exception has occurred.
58       */
59      // https://github.com/42BV/CSVeed/issues/46
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       * Creates the bean.
85       *
86       * @param alpha
87       *            the alpha
88       * @param beta
89       *            the beta
90       * @param gamma
91       *            the gamma
92       *
93       * @return the bean with multiple strings
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 }