1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.supercsv.util;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.lang.reflect.Constructor;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.junit.Test;
30  import org.supercsv.cellprocessor.ParseInt;
31  import org.supercsv.cellprocessor.ift.CellProcessor;
32  import org.supercsv.exception.SuperCsvException;
33  import org.supercsv.mock.IdentityTransform;
34  
35  
36  
37  
38  
39  
40  public class UtilTest {
41  	
42  	private static final String[] NAME_MAPPING = new String[] { "name", null, "city" };
43  	
44  	private static final List<String> LIST = Arrays.asList("Ezio", "25", "Venice");
45  	
46  	private static final CellProcessor[] PROCESSORS = new CellProcessor[] { new IdentityTransform(), new ParseInt(),
47  		null };
48  	
49  	private static final int LINE_NO = 23;
50  	
51  	private static final int ROW_NO = 12;
52  	
53  	private static final Map<String, Object> MAP = new HashMap<String, Object>();
54  	static {
55  		MAP.put("name", "Ezio");
56  		MAP.put("age", 25);
57  		MAP.put("city", "Venice");
58  	}
59  	
60  	
61  
62  
63  	@Test
64  	public void testFilterMapToList() {
65  		List<Object> list = Util.filterMapToList(MAP, NAME_MAPPING);
66  		assertTrue(list.size() == 3);
67  		assertEquals("Ezio", list.get(0));
68  		assertNull(list.get(1));
69  		assertEquals("Venice", list.get(2));
70  	}
71  	
72  	
73  
74  
75  	@Test(expected = NullPointerException.class)
76  	public void testFilterMapToListWithNullMap() {
77  		Util.filterMapToList(null, NAME_MAPPING);
78  	}
79  	
80  	
81  
82  
83  	@Test(expected = NullPointerException.class)
84  	public void testFilterMapToListWithNullNameMapping() {
85  		Util.filterMapToList(MAP, null);
86  	}
87  	
88  	
89  
90  
91  	@Test
92  	public void testFilterListToMap() {
93  		final Map<String, String> map = new HashMap<String, String>();
94  		Util.filterListToMap(map, NAME_MAPPING, LIST);
95  		assertTrue(map.size() == 2);
96  		assertEquals("Ezio", map.get("name"));
97  		assertEquals("Venice", map.get("city"));
98  	}
99  	
100 	
101 
102 
103 	@Test(expected = NullPointerException.class)
104 	public void testFilterListToMapWithNullDestMap() {
105 		Util.filterListToMap(null, NAME_MAPPING, LIST);
106 	}
107 	
108 	
109 
110 
111 	@Test(expected = NullPointerException.class)
112 	public void testFilterListToMapWithNullNameMapping() {
113 		Util.filterListToMap(new HashMap<String, String>(), null, LIST);
114 	}
115 	
116 	
117 
118 
119 	@Test(expected = NullPointerException.class)
120 	public void testFilterListToMapWithNullSourceList() {
121 		Util.filterListToMap(new HashMap<String, String>(), NAME_MAPPING, null);
122 	}
123 	
124 	
125 
126 
127 	@Test(expected = SuperCsvException.class)
128 	public void testFilterListToMapWithSizeMismatch() {
129 		Util.filterListToMap(new HashMap<String, String>(), new String[] { "notEnoughColumns" }, LIST);
130 	}
131 	
132 	
133 
134 
135 	@Test(expected = SuperCsvException.class)
136 	public void testFilterListToMapWithDuplicateNameMapping() {
137 		Util.filterListToMap(new HashMap<String, String>(), new String[] { "name", "name", "city" }, LIST);
138 	}
139 	
140 	
141 
142 
143 	@Test
144 	public void testExecuteCellProcessors() {
145 		List<Object> destinationList = new ArrayList<Object>();
146 		Util.executeCellProcessors(destinationList, LIST, PROCESSORS, LINE_NO, ROW_NO);
147 		assertTrue(destinationList.size() == 3);
148 		assertEquals("Ezio", destinationList.get(0));
149 		assertEquals(Integer.valueOf(25), destinationList.get(1));
150 		assertEquals("Venice", destinationList.get(2));
151 	}
152 	
153 	
154 
155 
156 	@Test(expected = NullPointerException.class)
157 	public void testExecuteCellProcessorsWithNullDestination() {
158 		Util.executeCellProcessors(null, LIST, PROCESSORS, LINE_NO, ROW_NO);
159 	}
160 	
161 	
162 
163 
164 	@Test(expected = NullPointerException.class)
165 	public void testExecuteCellProcessorsWithNullSource() {
166 		Util.executeCellProcessors(new ArrayList<Object>(), null, PROCESSORS, LINE_NO, ROW_NO);
167 	}
168 	
169 	
170 
171 
172 	@Test(expected = NullPointerException.class)
173 	public void testExecuteCellProcessorsWithNullProcessors() {
174 		Util.executeCellProcessors(new ArrayList<Object>(), LIST, null, LINE_NO, ROW_NO);
175 	}
176 	
177 	
178 
179 
180 
181 	@Test(expected = SuperCsvException.class)
182 	public void testExecuteCellProcessorsWithSizeMismatch() {
183 		final List<Object> invalidSizeList = new ArrayList<Object>();
184 		Util.executeCellProcessors(new ArrayList<Object>(), invalidSizeList, PROCESSORS, LINE_NO, ROW_NO);
185 	}
186 	
187 	
188 
189 
190 	@Test
191 	public void testFilterMapToObjectArray() {
192 		final Object[] objectArray = Util.filterMapToObjectArray(MAP, NAME_MAPPING);
193 		assertTrue(objectArray.length == 3);
194 		assertEquals("Ezio", objectArray[0]);
195 		assertNull(objectArray[1]);
196 		assertEquals("Venice", objectArray[2]);
197 	}
198 	
199 	
200 
201 
202 	@Test(expected = NullPointerException.class)
203 	public void testFilterMapToObjectArrayWithNullValues() {
204 		Util.filterMapToObjectArray(null, NAME_MAPPING);
205 	}
206 	
207 	
208 
209 
210 	@Test(expected = NullPointerException.class)
211 	public void testFilterMapToObjectArrayWithNullNameMapping() {
212 		Util.filterMapToObjectArray(MAP, null);
213 	}
214 	
215 	
216 
217 
218 	@Test
219 	public void testObjectArrayToStringArray() {
220 		final Object[] input = new Object[] { 1, null, "three" };
221 		final String[] output = Util.objectArrayToStringArray(input);
222 		assertEquals(3, output.length);
223 		assertEquals("1", output[0]);
224 		assertNull(output[1]);
225 		assertEquals("three", output[2]);
226 	}
227 	
228 	
229 
230 
231 	@Test
232 	public void testObjectArrayToStringArrayWithNullArray() {
233 		assertNull(Util.objectArrayToStringArray(null));
234 	}
235 	
236 	
237 
238 
239 	@Test
240 	public void testObjectListToStringArray() {
241 		final List<Object> input = Arrays.asList(new Object[] { 1, null, "three" });
242 		final String[] output = Util.objectListToStringArray(input);
243 		assertEquals(3, output.length);
244 		assertEquals("1", output[0]);
245 		assertNull(output[1]);
246 		assertEquals("three", output[2]);
247 	}
248 	
249 	
250 
251 
252 	@Test
253 	public void testObjectListToStringArrayWithNullList() {
254 		assertNull(Util.objectListToStringArray(null));
255 	}
256 	
257 	
258 
259 
260 	@Test
261 	public void testPrivateConstructor() throws Exception {
262 		Constructor<?> c = Util.class.getDeclaredConstructors()[0];
263 		c.setAccessible(true);
264 		c.newInstance();
265 	}
266 	
267 }