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;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.supercsv.SuperCsvTestUtils.CSV_FILE;
20  import static org.supercsv.SuperCsvTestUtils.CUSTOMERS;
21  import static org.supercsv.SuperCsvTestUtils.HEADER;
22  import static org.supercsv.SuperCsvTestUtils.JOHN;
23  import static org.supercsv.SuperCsvTestUtils.STRING_CUSTOMERS;
24  import static org.supercsv.SuperCsvTestUtils.WRITE_PROCESSORS;
25  
26  import java.io.IOException;
27  import java.io.StringWriter;
28  import java.io.Writer;
29  
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.supercsv.exception.SuperCsvReflectionException;
34  import org.supercsv.mock.CustomerBean;
35  import org.supercsv.mock.CustomerStringBean;
36  import org.supercsv.prefs.CsvPreference;
37  
38  /**
39   * Tests the CsvBeanWriter class.
40   * 
41   * @author James Bassett
42   */
43  public class CsvBeanWriterTest {
44  	
45  	private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
46  	
47  	private Writer writer;
48  	
49  	private CsvBeanWriter beanWriter;
50  	
51  	private CustomerBean customer;
52  	
53  	/**
54  	 * Sets up the writer for the tests.
55  	 */
56  	@Before
57  	public void setUp() {
58  		writer = new StringWriter();
59  		beanWriter = new CsvBeanWriter(writer, PREFS);
60  		customer = new CustomerBean();
61  	}
62  	
63  	/**
64  	 * Closes the bean writer after the test.
65  	 */
66  	@After
67  	public void tearDown() throws IOException {
68  		beanWriter.close();
69  	}
70  	
71  	/**
72  	 * Tests the constructor with a null writer.
73  	 */
74  	@SuppressWarnings("resource")
75  	@Test(expected = NullPointerException.class)
76  	public void testConstructorWillNullWriter() {
77  		new CsvBeanWriter(null, PREFS);
78  	}
79  	
80  	/**
81  	 * Tests the constructor with a null CsvPreference.
82  	 */
83  	@SuppressWarnings("resource")
84  	@Test(expected = NullPointerException.class)
85  	public void testConstructorWillNullPreference() {
86  		new CsvBeanWriter(writer, null);
87  	}
88  	
89  	/**
90  	 * Tests the write() method.
91  	 */
92  	@Test
93  	public void testWrite() throws IOException {
94  		beanWriter.writeHeader(HEADER);
95  		for( CustomerStringBean customer : STRING_CUSTOMERS ) {
96  			beanWriter.write(customer, HEADER);
97  		}
98  		beanWriter.flush();
99  		assertEquals(CSV_FILE, writer.toString());
100 	}
101 	
102 	/**
103 	 * Tests the write() method with processors.
104 	 */
105 	@Test
106 	public void testWriteProcessors() throws IOException {
107 		beanWriter.writeHeader(HEADER);
108 		for( CustomerBean customer : CUSTOMERS ) {
109 			beanWriter.write(customer, HEADER, WRITE_PROCESSORS);
110 		}
111 		beanWriter.flush();
112 		assertEquals(CSV_FILE, writer.toString());
113 	}
114 	
115 	/**
116 	 * Tests the write() method with a null bean.
117 	 */
118 	@Test(expected = NullPointerException.class)
119 	public void testWriteWithNullSource() throws IOException {
120 		beanWriter.write(null, HEADER);
121 	}
122 	
123 	/**
124 	 * Tests the write() method with a null name mapping array.
125 	 */
126 	@Test(expected = NullPointerException.class)
127 	public void testWriteWithNullNameMappingArray() throws IOException {
128 		beanWriter.write(customer, (String[]) null);
129 	}
130 	
131 	/**
132 	 * Tests the write() method with a a name mapping containing nulls (should be empty columns).
133 	 */
134 	@Test
135 	public void testWriteWithNullNameMapping() throws IOException {
136 		
137 		final String[] headerWithNulls = new String[] { "customerNo", null, "firstName", null, "lastName" };
138 		final String expectedCsv = JOHN.getCustomerNo() + ",," + JOHN.getFirstName() + ",," + JOHN.getLastName()
139 			+ "\r\n";
140 		
141 		beanWriter.write(JOHN, headerWithNulls);
142 		beanWriter.flush();
143 		assertEquals(expectedCsv, writer.toString());
144 	}
145 	
146 	/**
147 	 * Tests the write() method (with processors) with a null bean.
148 	 */
149 	@Test(expected = NullPointerException.class)
150 	public void testWriteProcessorsWithNullSource() throws IOException {
151 		beanWriter.write(null, HEADER, WRITE_PROCESSORS);
152 	}
153 	
154 	/**
155 	 * Tests the write() method (with processors) with a null name mapping array.
156 	 */
157 	@Test(expected = NullPointerException.class)
158 	public void testWriteProcessorsWithNullNameMapping() throws IOException {
159 		beanWriter.write(customer, null, WRITE_PROCESSORS);
160 	}
161 	
162 	/**
163 	 * Tests the write() method (with processors) with a null cell processor array.
164 	 */
165 	@Test(expected = NullPointerException.class)
166 	public void testWriteProcessorsWithNullProcessors() throws IOException {
167 		beanWriter.write(customer, HEADER, null);
168 		
169 	}
170 	
171 	/**
172 	 * Tests the write() method when a getter throws an Exception.
173 	 */
174 	@Test(expected = SuperCsvReflectionException.class)
175 	public void testGetterThrowingException() throws IOException {
176 		beanWriter.write(new ExceptionBean(), "exception");
177 	}
178 	
179 	/**
180 	 * Bean to test exceptions when invoking getters using CsvBeanWriter.
181 	 */
182 	public static class ExceptionBean extends CustomerBean {
183 		
184 		public String getException() {
185 			throw new RuntimeException("oops!");
186 		}
187 		
188 	}
189 }