View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Test the Util class.
37   * 
38   * @author James Bassett
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  	 * Tests the filterMapToList() method (the age attribute is not used).
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  	 * Tests the filterMapToList() method with a null map (should throw an exception).
74  	 */
75  	@Test(expected = NullPointerException.class)
76  	public void testFilterMapToListWithNullMap() {
77  		Util.filterMapToList(null, NAME_MAPPING);
78  	}
79  	
80  	/**
81  	 * Tests the filterMapToList() method with a null name mapping array (should throw an exception).
82  	 */
83  	@Test(expected = NullPointerException.class)
84  	public void testFilterMapToListWithNullNameMapping() {
85  		Util.filterMapToList(MAP, null);
86  	}
87  	
88  	/**
89  	 * Tests the filterListToMap() method.
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 	 * Tests the filterListToMap() method with a null destination map (should throw an exception).
102 	 */
103 	@Test(expected = NullPointerException.class)
104 	public void testFilterListToMapWithNullDestMap() {
105 		Util.filterListToMap(null, NAME_MAPPING, LIST);
106 	}
107 	
108 	/**
109 	 * Tests the filterListToMap() method with a null name mapping array (should throw an exception).
110 	 */
111 	@Test(expected = NullPointerException.class)
112 	public void testFilterListToMapWithNullNameMapping() {
113 		Util.filterListToMap(new HashMap<String, String>(), null, LIST);
114 	}
115 	
116 	/**
117 	 * Tests the filterListToMap() method with a null source list (should throw an exception).
118 	 */
119 	@Test(expected = NullPointerException.class)
120 	public void testFilterListToMapWithNullSourceList() {
121 		Util.filterListToMap(new HashMap<String, String>(), NAME_MAPPING, null);
122 	}
123 	
124 	/**
125 	 * Tests the filterListToMap() method with a name mapping array with too few elements (should throw an exception).
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 	 * Tests the filterListToMap() method with a name mapping array with duplicate elements (should throw an exception).
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 	 * Tests the executeCellProcessors() method.
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 	 * Tests the executeCellProcessors() method with a null destination List (should throw an Exception).
155 	 */
156 	@Test(expected = NullPointerException.class)
157 	public void testExecuteCellProcessorsWithNullDestination() {
158 		Util.executeCellProcessors(null, LIST, PROCESSORS, LINE_NO, ROW_NO);
159 	}
160 	
161 	/**
162 	 * Tests the executeCellProcessors() method with a null source List (should throw an Exception).
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 	 * Tests the executeCellProcessors() method with a processors array (should throw an Exception).
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 	 * Tests the executeCellProcessors() method with a source List whose size doesn't match the number of CellProcessors
179 	 * (should throw an Exception).
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 	 * Tests the filterMapToObjectArray() method.
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 	 * Tests the filterMapToObjectArray() method with a null values Map (should throw an Exception).
201 	 */
202 	@Test(expected = NullPointerException.class)
203 	public void testFilterMapToObjectArrayWithNullValues() {
204 		Util.filterMapToObjectArray(null, NAME_MAPPING);
205 	}
206 	
207 	/**
208 	 * Tests the filterMapToObjectArray() method with a null nameMapping array (should throw an Exception).
209 	 */
210 	@Test(expected = NullPointerException.class)
211 	public void testFilterMapToObjectArrayWithNullNameMapping() {
212 		Util.filterMapToObjectArray(MAP, null);
213 	}
214 	
215 	/**
216 	 * Tests the objectArrayToStringArray() method.
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 	 * Tests the objectArrayToStringArray() method with a null array.
230 	 */
231 	@Test
232 	public void testObjectArrayToStringArrayWithNullArray() {
233 		assertNull(Util.objectArrayToStringArray(null));
234 	}
235 	
236 	/**
237 	 * Tests the objectListToStringArray() method.
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 	 * Tests the objectListToStringArray() method with a null List.
251 	 */
252 	@Test
253 	public void testObjectListToStringArrayWithNullList() {
254 		assertNull(Util.objectListToStringArray(null));
255 	}
256 	
257 	/**
258 	 * Tests the private constructor for test coverage (yes, this is stupid).
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 }