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.cellprocessor;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.exception.SuperCsvCellProcessorException;
25  import org.supercsv.mock.IdentityTransform;
26  
27  /**
28   * Tests the ParseChar processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class ParseCharTest {
34  	
35  	private static final char CHAR = 'c';
36  	private static final String STRING = String.valueOf(CHAR);
37  	
38  	private CellProcessor processor;
39  	private CellProcessor processorChain;
40  	
41  	/**
42  	 * Sets up the processors for the test using all constructor combinations.
43  	 */
44  	@Before
45  	public void setUp() {
46  		processor = new ParseChar();
47  		processorChain = new ParseChar(new IdentityTransform());
48  	}
49  	
50  	/**
51  	 * Tests unchained/chained execution with a valid char input (should be returned unchanged).
52  	 */
53  	@Test
54  	public void testValidChar() {
55  		assertEquals(CHAR, processor.execute(CHAR, ANONYMOUS_CSVCONTEXT));
56  		assertEquals(CHAR, processorChain.execute(CHAR, ANONYMOUS_CSVCONTEXT));
57  	}
58  	
59  	/**
60  	 * Tests unchained/chained execution with a valid single-char String input.
61  	 */
62  	@Test
63  	public void testStringWithSingleChar() {
64  		assertEquals(CHAR, processor.execute(STRING, ANONYMOUS_CSVCONTEXT));
65  		assertEquals(CHAR, processorChain.execute(STRING, ANONYMOUS_CSVCONTEXT));
66  	}
67  	
68  	/**
69  	 * Tests execution with an multi-character String input (should throw an exception).
70  	 */
71  	@Test(expected = SuperCsvCellProcessorException.class)
72  	public void testWithMultiCharString() {
73  		processor.execute("cc", ANONYMOUS_CSVCONTEXT);
74  	}
75  	
76  	/**
77  	 * Tests execution with a non char input (should throw an exception).
78  	 */
79  	@Test(expected = SuperCsvCellProcessorException.class)
80  	public void testWithNonCharInput() {
81  		processor.execute(1, ANONYMOUS_CSVCONTEXT);
82  	}
83  	
84  	/**
85  	 * Tests execution with a null input (should throw an Exception).
86  	 */
87  	@Test(expected = SuperCsvCellProcessorException.class)
88  	public void testWithNull() {
89  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
90  	}
91  	
92  }