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.STRING_CUSTOMERS;
23  import static org.supercsv.SuperCsvTestUtils.WRITE_PROCESSORS;
24  
25  import java.io.IOException;
26  import java.io.StringWriter;
27  import java.io.Writer;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.supercsv.mock.CustomerBean;
36  import org.supercsv.mock.CustomerStringBean;
37  import org.supercsv.prefs.CsvPreference;
38  import org.supercsv.util.Util;
39  
40  /**
41   * Tests the CsvMapWriter class.
42   * 
43   * @author James Bassett
44   */
45  public class CsvMapWriterTest {
46  	
47  	private static final CsvPreference PREFS = CsvPreference.STANDARD_PREFERENCE;
48  	
49  	private Writer writer;
50  	
51  	private CsvMapWriter mapWriter;
52  	
53  	/**
54  	 * Sets up the writer for the tests.
55  	 */
56  	@Before
57  	public void setUp() {
58  		writer = new StringWriter();
59  		mapWriter = new CsvMapWriter(writer, PREFS);
60  	}
61  	
62  	/**
63  	 * Closes the map writer after the test.
64  	 */
65  	@After
66  	public void tearDown() throws IOException {
67  		mapWriter.close();
68  	}
69  	
70  	/**
71  	 * Tests the constructor with a null writer.
72  	 */
73  	@SuppressWarnings("resource")
74  	@Test(expected = NullPointerException.class)
75  	public void testConstructorWillNullWriter() {
76  		new CsvMapWriter(null, PREFS);
77  	}
78  	
79  	/**
80  	 * Tests the constructor with a null CsvPreference.
81  	 */
82  	@SuppressWarnings("resource")
83  	@Test(expected = NullPointerException.class)
84  	public void testConstructorWillNullPreference() {
85  		new CsvMapWriter(writer, null);
86  	}
87  	
88  	/**
89  	 * Tests the write() method.
90  	 * 
91  	 * @throws IOException
92  	 */
93  	@Test
94  	public void testWrite() throws IOException {
95  		mapWriter.writeHeader(HEADER);
96  		for( CustomerStringBean customer : STRING_CUSTOMERS ) {
97  			Map<String, Object> customerMap = new HashMap<String, Object>();
98  			Util.filterListToMap(
99  				customerMap,
100 				HEADER,
101 				Arrays.asList(new String[] { customer.getCustomerNo(), customer.getFirstName(), customer.getLastName(),
102 					customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
103 					customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
104 					customer.getLoyaltyPoints() }));
105 			mapWriter.write(customerMap, HEADER);
106 		}
107 		mapWriter.flush();
108 		assertEquals(CSV_FILE, writer.toString());
109 	}
110 	
111 	/**
112 	 * Tests the write() method with processors.
113 	 */
114 	@Test
115 	public void testWriteProcessors() throws IOException {
116 		mapWriter.writeHeader(HEADER);
117 		for( CustomerBean customer : CUSTOMERS ) {
118 			Map<String, Object> customerMap = new HashMap<String, Object>();
119 			Util.filterListToMap(
120 				customerMap,
121 				HEADER,
122 				Arrays.asList(new Object[] { customer.getCustomerNo(), customer.getFirstName(), customer.getLastName(),
123 					customer.getBirthDate(), customer.getMailingAddress(), customer.getMarried(),
124 					customer.getNumberOfKids(), customer.getFavouriteQuote(), customer.getEmail(),
125 					customer.getLoyaltyPoints() }));
126 			mapWriter.write(customerMap, HEADER, WRITE_PROCESSORS);
127 		}
128 		mapWriter.flush();
129 		assertEquals(CSV_FILE, writer.toString());
130 	}
131 	
132 	/**
133 	 * Tests the write() method with a null map.
134 	 */
135 	@Test(expected = NullPointerException.class)
136 	public void testWriteWithNullMap() throws IOException {
137 		mapWriter.write(null, HEADER);
138 	}
139 	
140 	/**
141 	 * Tests the write() method with a null name mapping array.
142 	 */
143 	@Test(expected = NullPointerException.class)
144 	public void testWriteWithNullNameMapping() throws IOException {
145 		mapWriter.write(new HashMap<String, Object>(), (String[]) null);
146 	}
147 	
148 	/**
149 	 * Tests the write() method (with processors) with a null map.
150 	 */
151 	@Test(expected = NullPointerException.class)
152 	public void testWriteProcessorsWithNullMap() throws IOException {
153 		mapWriter.write(null, HEADER, WRITE_PROCESSORS);
154 	}
155 	
156 	/**
157 	 * Tests the write() method (with processors) with a null name mapping array.
158 	 */
159 	@Test(expected = NullPointerException.class)
160 	public void testWriteProcessorsWithNullNameMapping() throws IOException {
161 		mapWriter.write(new HashMap<String, Object>(), null, WRITE_PROCESSORS);
162 		
163 	}
164 	
165 	/**
166 	 * Tests the write() method (with processors) with a null cell processor array.
167 	 */
168 	@Test(expected = NullPointerException.class)
169 	public void testWriteProcessorsWithNullProcessors() throws IOException {
170 		mapWriter.write(new HashMap<String, Object>(), HEADER, null);
171 		
172 	}
173 	
174 }