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.quote;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import org.junit.Test;
22  import org.supercsv.prefs.CsvPreference;
23  import org.supercsv.util.CsvContext;
24  
25  /**
26   * Tests the ColumnQuoteMode class.
27   * 
28   * @author James Bassett
29   */
30  public class ColumnQuoteModeTest {
31  	
32  	/**
33  	 * Tests the quotesRequired() method with a boolean array.
34  	 */
35  	@Test
36  	public void testQuotesRequiredWithBooleanArray() {
37  		testQuotesRequired(new ColumnQuoteMode(new boolean[] { true, false, false, true }));
38  	}
39  	
40  	/**
41  	 * Tests the quotesRequired() method with an int array.
42  	 */
43  	@Test
44  	public void testQuotesRequiredWithIntArray() {
45  		testQuotesRequired(new ColumnQuoteMode(1, 4));
46  	}
47  	
48  	/**
49  	 * Tests the quotesRequired() method with the supplied quote mode.
50  	 * 
51  	 * @param quoteMode
52  	 *            the quote mode to use
53  	 */
54  	private void testQuotesRequired(final QuoteMode quoteMode) {
55  		final String input = "input";
56  		final CsvPreference prefs = CsvPreference.STANDARD_PREFERENCE;
57  		
58  		assertTrue(quoteMode.quotesRequired(input, new CsvContext(1, 1, 1), prefs));
59  		assertFalse(quoteMode.quotesRequired(input, new CsvContext(1, 1, 2), prefs));
60  		assertFalse(quoteMode.quotesRequired(input, new CsvContext(1, 1, 3), prefs));
61  		assertTrue(quoteMode.quotesRequired(input, new CsvContext(1, 1, 4), prefs));
62  	}
63  	
64  	/**
65  	 * Tests construction with a null boolean array (should throw an exception).
66  	 */
67  	@Test(expected = NullPointerException.class)
68  	public void testConstructorWithNullBooleanArray() {
69  		new ColumnQuoteMode((boolean[]) null);
70  	}
71  	
72  	/**
73  	 * Tests construction with a null int array (should throw an exception).
74  	 */
75  	@Test(expected = NullPointerException.class)
76  	public void testConstructorWithNullIntArray() {
77  		new ColumnQuoteMode((int[]) null);
78  	}
79  	
80  }