1 package integrationTest.issue3189428;
2
3
4 import com.opencsv.CSVReader;
5 import com.opencsv.CSVReaderBuilder;
6 import com.opencsv.CSVWriter;
7 import com.opencsv.ICSVWriter;
8 import com.opencsv.bean.ColumnPositionMappingStrategy;
9 import com.opencsv.bean.CsvToBean;
10 import com.opencsv.bean.CsvToBeanBuilder;
11 import com.opencsv.exceptions.CsvValidationException;
12
13 import java.io.FileNotFoundException;
14 import java.io.FileReader;
15 import java.io.FileWriter;
16 import java.io.IOException;
17 import java.util.List;
18
19 public class CsvSample {
20 String filePath = "test/integrationTest/issue3189428/mysample.csv";
21
22 public CsvSample() {
23 }
24
25 public static void main(String[] args) throws Exception {
26 CsvSample sample = new CsvSample();
27 sample.doSample();
28 }
29
30 public void doSample() throws Exception {
31 String[] fields = new String[5];
32 fields[0] = "field1";
33 fields[1] = "3.0";
34 fields[2] = "3,147.25";
35 fields[3] = "$3,147.26";
36
37 fields[4] = "Joe said, \"This is a test of a \nlong broken string,\" and Sally said, \"I bet it won't work.\" ";
38
39
40
41 ICSVWriter writer = new CSVWriter(new FileWriter(filePath));
42 writer.writeNext(fields);
43 writer.writeNext(fields);
44 writer.writeNext(fields);
45 writer.close();
46
47 testRawCsvRead(fields[4]);
48 testMappingStrategyRead(fields[4]);
49
50 System.out.println("\nComplete. File written out to " + filePath);
51
52 }
53
54
55
56
57
58
59
60 protected void testRawCsvRead(String originalCommentText) throws IOException, CsvValidationException {
61 CSVReader reader = new CSVReader(new FileReader(filePath));
62 String[] nextLine;
63 int count = 0;
64 while ((nextLine = reader.readNext()) != null) {
65 if (!nextLine[0].equals("field1")) {
66 System.out.println("RawCsvRead Assert Error: Name is wrong.");
67 }
68 if (!nextLine[1].equals("3.0")) {
69 System.out.println("RawCsvRead Assert Error: Value is wrong.");
70 }
71 if (!nextLine[2].equals("3,147.25")) {
72 System.out.println("RawCsvRead Assert Error: Amount1 is wrong.");
73 }
74 if (!nextLine[3].equals("$3,147.26")) {
75 System.out.println("RawCsvRead Assert Error: Currency is wrong.");
76 }
77 System.out.println("Field 4 read: " + nextLine[4]);
78 if (!nextLine[4].equals(originalCommentText)) {
79 System.out.println("RawCsvRead Assert Error: Comment is wrong.");
80 }
81 count++;
82 }
83 if (count != 3) {
84 System.out.println("RawCsvRead Assert Error: Count of lines is wrong.");
85 }
86
87 }
88
89
90
91
92
93
94
95
96 protected void testMappingStrategyRead(String originalCommentText)
97 throws FileNotFoundException {
98 ColumnPositionMappingStrategy<MyBean> mappingStrategy = new ColumnPositionMappingStrategy<>();
99 mappingStrategy.setType(MyBean.class);
100 String[] columns = new String[]{"name", "value", "amount1", "currency", "comments"};
101 mappingStrategy.setColumnMapping(columns);
102
103 CSVReader reader = new CSVReaderBuilder(new FileReader(filePath))
104 .build();
105 CsvToBean<MyBean> csv = new CsvToBeanBuilder<MyBean>(reader)
106 .withMappingStrategy(mappingStrategy)
107 .withStrictQuotes(false)
108 .withIgnoreLeadingWhiteSpace(false)
109 .build();
110 List<MyBean> list = csv.parse();
111
112 if (list.size() != 3) {
113 System.out.println("Error - list size is wrong.");
114 }
115 MyBean myBean = list.get(2);
116 if (!myBean.getName().equals("field1")) {
117 System.out.println("MappingStrategy Assert Error: Name is wrong.");
118 }
119 if (!myBean.getValue().equals("3.0")) {
120 System.out.println("MappingStrategy Assert Error: Value is wrong.");
121 }
122 if (!myBean.getAmount1().equals("3,147.25")) {
123 System.out.println("MappingStrategy Assert Error: Amount1 is wrong.");
124 }
125 if (!myBean.getCurrency().equals("$3,147.26")) {
126 System.out.println("MappingStrategy Assert Error: Currency is wrong.");
127 }
128 printfield("MyBeanComments: ", myBean.getComments());
129 printfield("OriginalCommentText: ", originalCommentText);
130 if (!myBean.getComments().equals(originalCommentText)) {
131 System.out.println("MappingStrategy Assert Error: Comment is wrong.");
132 }
133 }
134
135 private void printfield(String header, String field) {
136 System.out.println(header + field);
137 System.out.println("fieldlen: " + field.length());
138 }
139
140 public static class MyBean {
141 String name;
142 String value;
143 String amount1;
144 String currency;
145 String comments;
146
147 public MyBean() {
148 }
149
150 public String getName() {
151 return name;
152 }
153
154 public void setName(String name) {
155 this.name = name;
156 }
157
158 public String getValue() {
159 return value;
160 }
161
162 public void setValue(String value) {
163 this.value = value;
164 }
165
166 public String getAmount1() {
167 return amount1;
168 }
169
170 public void setAmount1(String amount1) {
171 this.amount1 = amount1;
172 }
173
174 public String getCurrency() {
175 return currency;
176 }
177
178 public void setCurrency(String currency) {
179 this.currency = currency;
180 }
181
182 public String getComments() {
183 return comments;
184 }
185
186 public void setComments(String comments) {
187 this.comments = comments;
188 }
189
190 }
191 }