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.encoder;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import org.junit.Test;
21  import org.supercsv.prefs.CsvPreference;
22  import org.supercsv.util.CsvContext;
23  
24  /**
25   * Tests the SelectiveCsvEncoder class.
26   * 
27   * @author James Bassett
28   */
29  public class SelectiveCsvEncoderTest {
30  	
31  	private static final String UNESCAPED = "\"Escape!\", he yelled.";
32  	
33  	private static final String ESCAPED = "\"\"\"Escape!\"\", he yelled.\"";
34  	
35  	/**
36  	 * Tests the encode method using the int array constructor.
37  	 */
38  	@Test
39  	public void testEncodeWithIntArray() {
40  		final SelectiveCsvEncoder encoder = new SelectiveCsvEncoder(2, 3);
41  		testEncode(encoder);
42  	}
43  	
44  	/**
45  	 * Tests the encode method using the boolean array constructor.
46  	 */
47  	@Test
48  	public void testEncodeWithBoolArray() {
49  		final SelectiveCsvEncoder encoder = new SelectiveCsvEncoder(new boolean[]{false, true, true, false});
50  		testEncode(encoder);
51  	}
52  	
53  	/**
54  	 * Tests encoding using the supplied encoder.
55  	 * 
56  	 * @param encoder
57  	 *            the encoder
58  	 */
59  	private void testEncode(final CsvEncoder encoder) {
60  		final CsvContext context = new CsvContext(1, 1, 1);
61  		assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
62  		context.setColumnNumber(2);
63  		assertEquals(ESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
64  		context.setColumnNumber(3);
65  		assertEquals(ESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
66  		context.setColumnNumber(4);
67  		assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
68  	}
69  	
70  	/**
71  	 * Tests the encode method when no column numbers are supplied.
72  	 */
73  	@Test
74  	public void testEncodeWithNoColumnNumbers() {
75  		final SelectiveCsvEncoder encoder = new SelectiveCsvEncoder();
76  		final CsvContext context = new CsvContext(1, 1, 1);
77  		assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
78  	}
79  	
80  	/**
81  	 * Tests the int array constructor with a null array.
82  	 */
83  	@Test(expected = NullPointerException.class)
84  	public void testIntArrayConstructorWithNull() {
85  		new SelectiveCsvEncoder((int[]) null);
86  	}
87  	
88  	/**
89  	 * Tests the boolean array constructor with a null array.
90  	 */
91  	@Test(expected = NullPointerException.class)
92  	public void testBoolArrayConstructorWithNull() {
93  		new SelectiveCsvEncoder((boolean[]) null);
94  	}
95  	
96  }