1 package com.opencsv.bean;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import com.opencsv.bean.mocks.MockBean;
20 import org.apache.commons.lang3.StringUtils;
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25
26 import java.io.StringReader;
27 import java.util.List;
28 import java.util.Locale;
29
30 import static org.junit.jupiter.api.Assertions.*;
31
32 public class ColumnPositionMappingStrategyTest {
33 private ColumnPositionMappingStrategy<MockBean> strat;
34
35 private static Locale systemLocale;
36
37 @BeforeAll
38 public static void storeSystemLocale() {
39 systemLocale = Locale.getDefault();
40 }
41
42 @AfterEach
43 public void setSystemLocaleBackToDefault() {
44 Locale.setDefault(systemLocale);
45 }
46
47 @BeforeEach
48 public void setUp() {
49 Locale.setDefault(Locale.US);
50 strat = new ColumnPositionMappingStrategy<>();
51 strat.setType(MockBean.class);
52 }
53
54 @Test
55 public void testParse() {
56 String s = "" +
57 "kyle,123456,emp123,1\n" +
58 "jimmy,abcnum,cust09878,2";
59
60 strat.setColumnMapping("name", "orderNumber", "id", "num");
61
62 CsvToBean<MockBean> csv = new CsvToBeanBuilder<MockBean>(new StringReader(s))
63 .withMappingStrategy(strat)
64 .build();
65 List<MockBean> list = csv.parse();
66 assertNotNull(list);
67 assertEquals(2, list.size());
68 MockBean bean = list.get(0);
69 assertEquals("kyle", bean.getName());
70 assertEquals("123456", bean.getOrderNumber());
71 assertEquals("emp123", bean.getId());
72 assertEquals(1, bean.getNum());
73 }
74
75 @Test
76 public void testParseWithTrailingSpaces() {
77 String s = "" +
78 "kyle ,123456 ,emp123 , 1 \n" +
79 "jimmy,abcnum,cust09878,2 ";
80
81 String[] columns = new String[]{"name", "orderNumber", "id", "num"};
82 strat.setColumnMapping(columns);
83
84 CsvToBean<MockBean> csv = new CsvToBeanBuilder<MockBean>(new StringReader(s))
85 .withMappingStrategy(strat)
86 .build();
87 List<MockBean> list = csv.parse();
88 assertNotNull(list);
89 assertEquals(2, list.size());
90 MockBean bean = list.get(0);
91 assertEquals("kyle ", bean.getName());
92 assertEquals("123456 ", bean.getOrderNumber());
93 assertEquals("emp123 ", bean.getId());
94 assertEquals(1, bean.getNum());
95 }
96
97 @Test
98 public void testParseEmptyInput() {
99 String[] columns = new String[]{"name", "orderNumber", "id", "num"};
100 strat.setColumnMapping(columns);
101
102 CsvToBean<MockBean> csv = new CsvToBeanBuilder<MockBean>(new StringReader(StringUtils.EMPTY))
103 .withMappingStrategy(strat)
104 .build();
105 List<MockBean> list = csv.parse();
106 assertNotNull(list);
107 assertTrue(list.isEmpty());
108 }
109
110 @Test
111 public void testGetColumnMapping() {
112 String[] columnMapping = strat.getColumnMapping();
113 assertNotNull(columnMapping);
114 assertEquals(0, columnMapping.length);
115
116 String[] columns = new String[]{"name", "orderNumber", "id"};
117 strat.setColumnMapping(columns);
118
119 columnMapping = strat.getColumnMapping();
120 assertNotNull(columnMapping);
121 assertEquals(3, columnMapping.length);
122 assertArrayEquals(columns, columnMapping);
123
124 }
125
126 @Test
127 public void testGetColumnNames() {
128
129 strat.setColumnMapping("name", null, "id");
130
131 assertEquals("name", strat.getColumnName(0));
132 assertNull(strat.getColumnName(1));
133 assertEquals("id", strat.getColumnName(2));
134 assertNull(strat.getColumnName(3));
135 }
136
137 @Test
138 public void testGetColumnNamesArray() {
139
140 strat.setColumnMapping("name", null, "id");
141 String[] mapping = strat.getColumnMapping();
142
143 assertEquals(3, mapping.length);
144 assertEquals("name", mapping[0]);
145 assertNull(mapping[1]);
146 assertEquals("id", mapping[2]);
147 }
148
149 @Test
150 public void getColumnNamesWhenNullArray() {
151 strat.setColumnMapping((String[]) null);
152
153 assertNull(strat.getColumnName(0));
154 assertNull(strat.getColumnName(1));
155 assertArrayEquals(new String[0], strat.getColumnMapping());
156 }
157
158 @Test
159 public void getColumnNamesWhenNullColumnName() {
160 String[] columns = {null};
161 strat.setColumnMapping(columns);
162
163 assertNull(strat.getColumnName(0));
164 assertNull(strat.getColumnName(1));
165 assertArrayEquals(columns, strat.getColumnMapping());
166 }
167
168 @Test
169 public void getColumnNamesWhenEmptyMapping() {
170 strat.setColumnMapping();
171
172 assertNull(strat.getColumnName(0));
173 assertArrayEquals(new String[0], strat.getColumnMapping());
174 }
175
176 @Test
177 public void throwsIllegalStateExceptionIfTypeNotSet() {
178 String englishErrorMessage = null;
179 try {
180 new CsvToBeanBuilder<MockBean>(new StringReader("doesnt,matter\nat,all"))
181 .withMappingStrategy(new ColumnPositionMappingStrategy<>())
182 .build().parse();
183 fail("RuntimeException with inner IllegalStateException should have been thrown.");
184 }
185 catch(RuntimeException e) {
186 assertEquals(IllegalStateException.class, e.getCause().getClass());
187 englishErrorMessage = e.getCause().getLocalizedMessage();
188 }
189
190
191 try {
192 new CsvToBeanBuilder<MockBean>(new StringReader("doesnt,matter\nat,all"))
193 .withMappingStrategy(new ColumnPositionMappingStrategy<>())
194 .withErrorLocale(Locale.GERMAN)
195 .build().parse();
196 fail("RuntimeException with inner IllegalStateException should have been thrown.");
197 }
198 catch(RuntimeException e) {
199 assertEquals(IllegalStateException.class, e.getCause().getClass());
200 assertNotSame(englishErrorMessage, e.getCause().getLocalizedMessage());
201 }
202 }
203 }