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.junit.Assert.assertNull;
20  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.mock.IdentityTransform;
26  
27  /**
28   * Tests the Optional processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class OptionalTest {
34  	
35  	private CellProcessor processor;
36  	private CellProcessor processorChain;
37  	
38  	/**
39  	 * Sets up the processors for the test using all constructor combinations.
40  	 */
41  	@Before
42  	public void setUp() {
43  		processor = new Optional();
44  		processorChain = new Optional(new IdentityTransform());
45  	}
46  	
47  	/**
48  	 * Tests unchained/chained execution with empty string as input.
49  	 */
50  	@Test
51  	public void testEmptyString() {
52  		assertEquals("", processor.execute("", ANONYMOUS_CSVCONTEXT));
53  		assertEquals("", processorChain.execute("", ANONYMOUS_CSVCONTEXT));
54  	}
55  	
56  	/**
57  	 * Tests unchained/chained execution with a space as input.
58  	 */
59  	@Test
60  	public void testSpace() {
61  		assertEquals(" ", processor.execute(" ", ANONYMOUS_CSVCONTEXT));
62  		assertEquals(" ", processorChain.execute(" ", ANONYMOUS_CSVCONTEXT));
63  	}
64  	
65  	/**
66  	 * Tests unchained/chained execution with normal input (not "").
67  	 */
68  	@Test
69  	public void testNormalInput() {
70  		String normal = "normal";
71  		assertEquals(normal, processor.execute(normal, ANONYMOUS_CSVCONTEXT));
72  		assertEquals(normal, processorChain.execute(normal, ANONYMOUS_CSVCONTEXT));
73  	}
74  	
75  	/**
76  	 * Tests execution with a null input (should return null).
77  	 */
78  	@Test
79  	public void testWithNull() {
80  		assertNull(processor.execute(null, ANONYMOUS_CSVCONTEXT));
81  		assertNull(processorChain.execute(null, ANONYMOUS_CSVCONTEXT));
82  	}
83  }