View Javadoc
1   package com.opencsv;
2   
3   import com.opencsv.bean.StatefulBeanToCsv;
4   import com.opencsv.bean.StatefulBeanToCsvBuilder;
5   import com.opencsv.exceptions.CsvDataTypeMismatchException;
6   import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
7   import org.junit.jupiter.api.Test;
8   
9   import java.io.StringWriter;
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  /**
16   * Created by scott on 3/4/17.
17   */
18  public class Bug63Test {
19  
20      @Test
21      public void mappingStrategyRead() throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
22          String expectedString = "\"CONTENTSID\";\"KEY\";\"POSITION\";\"TEXT\"\n" +
23                  "\"\";\"\";\"1\";\"this is a test for row one\"\n" +
24                  "\"\";\"\";\"2\";\"this is a test for row, two\"\n";
25  
26          Contents c2 = new Contents();
27          List<Contents> cl2 = new ArrayList<>();
28  
29          c2.setKey("");
30          c2.setPosition(1);
31          c2.setText("this is a test for row one");
32          cl2.add(c2);
33  
34          c2 = new Contents();
35          c2.setKey("");
36          c2.setPosition(2);
37          c2.setText("this is a test for row, two");
38          cl2.add(c2);
39  
40          StringWriter writer = new StringWriter();
41  
42          StatefulBeanToCsv<Contents> bToC = new StatefulBeanToCsvBuilder<Contents>(writer)
43                  .withQuotechar(ICSVWriter.DEFAULT_QUOTE_CHARACTER)
44                  .withSeparator(';')
45                  .build();
46  
47  
48          bToC.write(cl2);
49          assertEquals(expectedString, writer.toString());
50  
51      }
52  
53      public static class Contents {
54  
55          private Integer contentsID;
56          private String key;
57          private Integer position;
58          private String text;
59  
60          public Integer getContentsID() {
61              return contentsID;
62          }
63  
64          public void setContentsID(Integer contentsID) {
65              this.contentsID = contentsID;
66          }
67  
68          public String getKey() {
69              return key;
70          }
71  
72          public void setKey(String key) {
73              this.key = key;
74          }
75  
76          public Integer getPosition() {
77              return position;
78          }
79  
80          public void setPosition(Integer position) {
81              this.position = position;
82          }
83  
84          public String getText() {
85              return text;
86          }
87  
88          public void setText(String text) {
89              this.text = text;
90          }
91  
92          @Override
93          public String toString() {
94              return "Contents [contentsID=" + contentsID + ", key=" + key + ", position=" + position + ", text=" + text
95                      + "]";
96          }
97  
98          @Override
99          public int hashCode() {
100             final int prime = 31;
101             int result = 1;
102             result = prime * result + ((contentsID == null) ? 0 : contentsID.hashCode());
103             result = prime * result + ((key == null) ? 0 : key.hashCode());
104             result = prime * result + ((position == null) ? 0 : position.hashCode());
105             result = prime * result + ((text == null) ? 0 : text.hashCode());
106             return result;
107         }
108 
109         @Override
110         public boolean equals(Object obj) {
111             if (this == obj)
112                 return true;
113             if (obj == null)
114                 return false;
115             if (getClass() != obj.getClass())
116                 return false;
117             Contents other = (Contents) obj;
118             if (contentsID == null) {
119                 if (other.contentsID != null)
120                     return false;
121             } else if (!contentsID.equals(other.contentsID))
122                 return false;
123             if (key == null) {
124                 if (other.key != null)
125                     return false;
126             } else if (!key.equals(other.key))
127                 return false;
128             if (position == null) {
129                 if (other.position != null)
130                     return false;
131             } else if (!position.equals(other.position))
132                 return false;
133             if (text == null) {
134                 return other.text == null;
135             } else return text.equals(other.text);
136         }
137     }
138 
139 }