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.io.dozer;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  
21  import java.io.IOException;
22  import java.io.StringWriter;
23  import java.io.Writer;
24  import java.util.Arrays;
25  
26  import org.dozer.DozerBeanMapper;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.supercsv.cellprocessor.FmtBool;
31  import org.supercsv.cellprocessor.Optional;
32  import org.supercsv.cellprocessor.constraint.NotNull;
33  import org.supercsv.cellprocessor.ift.CellProcessor;
34  import org.supercsv.mock.dozer.Answer;
35  import org.supercsv.mock.dozer.SurveyResponse;
36  import org.supercsv.prefs.CsvPreference;
37  
38  /**
39   * Tests the CsvDozerBeanWriter class.
40   * 
41   * @author James Bassett
42   */
43  public class CsvDozerBeanWriterTest {
44  	
45  	private static final CsvPreference PREFS = CsvPreference.EXCEL_PREFERENCE;
46  	
47  	private static final String[] FIELD_MAPPING = new String[] { "age", "consentGiven", "answers[0].questionNo",
48  		"answers[0].answer", "answers[1].questionNo", "answers[1].answer", "answers[2].questionNo", "answers[2].answer" };
49  	
50  	private static final CellProcessor[] PROCESSORS = new CellProcessor[] { new NotNull(), new FmtBool("Y", "N"),
51  		new Optional(), new Optional(), new Optional(), new Optional(), new Optional(), new Optional() };
52  	
53  	private static final String CSV = "age,consentGiven,questionNo1,answer1,questionNo2,answer2,questionNo3,answer3\n"
54  		+ "66,true,1,Twelve,2,Albert Einstein?,3,Big Bang Theory\n" + "32,true,1,,2,Superman,3,Stargate\n"
55  		+ "19,true,1,\"Um, how am I supposed to know?\",2,Me,3,\"Last question, can I go now?\"\n" + "4,false,,,,,,\n";
56  	
57  	private static final String PROCESSORS_CSV = "age,consentGiven,questionNo1,answer1,questionNo2,answer2,questionNo3,answer3\n"
58  		+ "66,Y,1,Twelve,2,Albert Einstein?,3,Big Bang Theory\n"
59  		+ "32,Y,1,,2,Superman,3,Stargate\n"
60  		+ "19,Y,1,\"Um, how am I supposed to know?\",2,Me,3,\"Last question, can I go now?\"\n" + "4,N,,,,,,\n";
61  	
62  	private Writer writer;
63  	
64  	private CsvDozerBeanWriter beanWriter;
65  	private CsvDozerBeanWriter beanWriterWithMapper;
66  	private CsvDozerBeanWriter beanWriterWithConfiguredMapper;
67  	private DozerBeanMapper beanMapper;
68  	private DozerBeanMapper configuredBeanMapper;
69  	
70  	private SurveyResponse response1;
71  	private SurveyResponse response2;
72  	private SurveyResponse response3;
73  	private SurveyResponse response4;
74  	
75  	/**
76  	 * Sets up the writer for the tests.
77  	 */
78  	@Before
79  	public void setUp() {
80  		writer = new StringWriter();
81  		beanWriter = new CsvDozerBeanWriter(writer, PREFS);
82  		
83  		beanMapper = new DozerBeanMapper();
84  		beanWriterWithMapper = new CsvDozerBeanWriter(writer, PREFS, beanMapper);
85  		
86  		configuredBeanMapper = new DozerBeanMapper(Arrays.asList("reference.xml"));
87  		beanWriterWithConfiguredMapper = new CsvDozerBeanWriter(writer, PREFS, configuredBeanMapper);
88  	}
89  	
90  	/**
91  	 * Closes the bean writers after the test.
92  	 */
93  	@After
94  	public void tearDown() throws IOException {
95  		beanWriter.close();
96  		beanWriterWithMapper.close();
97  		beanWriterWithConfiguredMapper.close();
98  	}
99  	
100 	/**
101 	 * Creates the survey responses to use in the tests.
102 	 */
103 	@Before
104 	public void createResponses() {
105 		response1 = new SurveyResponse(66, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
106 			"Albert Einstein?"), new Answer(3, "Big Bang Theory")));
107 		response2 = new SurveyResponse(32, true, Arrays.asList(new Answer(1, null), new Answer(2, "Superman"),
108 			new Answer(3, "Stargate")));
109 		response3 = new SurveyResponse(19, true, Arrays.asList(new Answer(1, "Um, how am I supposed to know?"),
110 			new Answer(2, "Me"), new Answer(3, "Last question, can I go now?")));
111 		response4 = new SurveyResponse(4, false, null);
112 	}
113 	
114 	/**
115 	 * Tests the write() method with a normal bean writer and no processors.
116 	 */
117 	@Test
118 	public void testWriteWithBeanWriter() throws IOException {
119 		testWrite(beanWriter, false, false);
120 	}
121 	
122 	/**
123 	 * Tests the write() method with a normal bean writer and processors.
124 	 */
125 	@Test
126 	public void testWriteWithBeanWriterUsingProcessors() throws IOException {
127 		testWrite(beanWriter, true, false);
128 	}
129 	
130 	/**
131 	 * Tests the write() method with a bean writer using a custom DozerBeanMapper and no processors.
132 	 */
133 	@Test
134 	public void testWriteWithBeanWriterCustomMapper() throws IOException {
135 		testWrite(beanWriterWithMapper, false, false);
136 	}
137 	
138 	/**
139 	 * Tests the write() method with a bean writer using a custom DozerBeanMapper and processors.
140 	 */
141 	@Test
142 	public void testWriteWithBeanWriterCustomMapperUsingProcessors() throws IOException {
143 		testWrite(beanWriterWithMapper, true, false);
144 	}
145 	
146 	/**
147 	 * Tests the write() method with a bean writer using a custom DozerBeanMapper and no processors.
148 	 */
149 	@Test
150 	public void testWriteWithBeanWriterConfiguredMapper() throws IOException {
151 		testWrite(beanWriterWithConfiguredMapper, false, true);
152 	}
153 	
154 	/**
155 	 * Tests the write() method with a bean writer using a custom DozerBeanMapper and processors.
156 	 */
157 	@Test
158 	public void testWriteWithBeanWriterConfiguredMapperUsingProcessors() throws IOException {
159 		testWrite(beanWriterWithConfiguredMapper, true, true);
160 	}
161 	
162 	/**
163 	 * Tests the write() methods with the supplied writer, with or without using processors.
164 	 * 
165 	 * @param beanWriter
166 	 *            the dozer bean writer to use for the tests
167 	 * @param useProcessors
168 	 *            whether to use processors for the test
169 	 * @param configured
170 	 *            whether the writer is already configured
171 	 * @throws IOException
172 	 */
173 	private void testWrite(final CsvDozerBeanWriter beanWriter, final boolean useProcessors, final boolean configured)
174 		throws IOException {
175 		
176 		if (!configured){
177 			beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
178 		}
179 		
180 		beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
181 			"questionNo3", "answer3");
182 		
183 		if( useProcessors ) {
184 			beanWriter.write(response1, PROCESSORS);
185 			beanWriter.write(response2, PROCESSORS);
186 			beanWriter.write(response3, PROCESSORS);
187 			beanWriter.write(response4, PROCESSORS);
188 			beanWriter.flush();
189 			assertEquals(PROCESSORS_CSV, writer.toString());
190 			
191 		} else {
192 			beanWriter.write(response1);
193 			beanWriter.write(response2);
194 			beanWriter.write(response3);
195 			beanWriter.write(response4);
196 			beanWriter.flush();
197 			assertEquals(CSV, writer.toString());
198 		}
199 		
200 	}
201 	
202 	/**
203 	 * Tests all of the constructors with null values (should throw an Exception).
204 	 */
205 	@Test
206 	public void testConstructorsWithNulls() {
207 		
208 		// constructor one - null Writer
209 		try {
210 			new CsvDozerBeanWriter((Writer) null, PREFS);
211 			fail("should have thrown NullPointerException");
212 		}
213 		catch(NullPointerException e) {}
214 		
215 		// constructor one - null prefs
216 		try {
217 			new CsvDozerBeanWriter(writer, null);
218 			fail("should have thrown NullPointerException");
219 		}
220 		catch(NullPointerException e) {}
221 		
222 		// constructor two - null Writer
223 		try {
224 			new CsvDozerBeanWriter((Writer) null, PREFS, beanMapper);
225 			fail("should have thrown NullPointerException");
226 		}
227 		catch(NullPointerException e) {}
228 		
229 		// constructor two - null prefs
230 		try {
231 			new CsvDozerBeanWriter(writer, null, beanMapper);
232 			fail("should have thrown NullPointerException");
233 		}
234 		catch(NullPointerException e) {}
235 		
236 		// constructor two - null dozerBeanMapper
237 		try {
238 			new CsvDozerBeanWriter(writer, PREFS, null);
239 			fail("should have thrown NullPointerException");
240 		}
241 		catch(NullPointerException e) {}
242 		
243 	}
244 	
245 	/**
246 	 * Tests the configureBeanMapping() method with a null clazz (should throw an exception).
247 	 */
248 	@Test(expected = NullPointerException.class)
249 	public void testConfigureBeanMappingWithNullClazz() {
250 		beanWriter.configureBeanMapping(null, FIELD_MAPPING);
251 	}
252 	
253 	/**
254 	 * Tests the configureBeanMapping() method with a null fieldMapping array (should throw an exception).
255 	 */
256 	@Test(expected = NullPointerException.class)
257 	public void testConfigureBeanMappingWithNullFieldMapping() {
258 		beanWriter.configureBeanMapping(SurveyResponse.class, null);
259 	}
260 	
261 	/**
262 	 * Tests the configureBeanMapping() method with a null fieldMapping element (should throw an exception).
263 	 */
264 	@Test(expected = NullPointerException.class)
265 	public void testConfigureBeanMappingWithNullFieldMappingElement() {
266 		beanWriter.configureBeanMapping(SurveyResponse.class, new String[] { "age", null, "consentGiven" });
267 	}
268 	
269 	/**
270 	 * Tests the write() method with a null clazz (should throw an exception).
271 	 */
272 	@Test(expected = NullPointerException.class)
273 	public void testWriteWithNullClazz() throws IOException {
274 		beanWriter.write(null);
275 	}
276 	
277 	/**
278 	 * Tests the write() method (with processors) with a null clazz (should throw an exception).
279 	 */
280 	@Test(expected = NullPointerException.class)
281 	public void testWriteWithProcessorsWithNullClazz() throws IOException {
282 		beanWriter.write(null, PROCESSORS);
283 	}
284 	
285 	/**
286 	 * Tests the write() method (with processors) with a null cell processor array (should throw an exception).
287 	 */
288 	@Test(expected = NullPointerException.class)
289 	public void testWriteWithProcessorsWithNullProcessors() throws IOException {
290 		beanWriter.write(SurveyResponse.class, (CellProcessor[]) null);
291 	}
292 	
293 }