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 Trim processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class TrimTest {
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 Trim();
44  		processorChain = new Trim(new IdentityTransform());
45  	}
46  	
47  	/**
48  	 * Tests unchained/chained execution with input containing no surrounding whitespace.
49  	 */
50  	@Test
51  	public void testInputNoWhitespace() {
52  		String input = "abc";
53  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
54  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
55  	}
56  	
57  	/**
58  	 * Tests unchained/chained execution with input containing surrounding spaces.
59  	 */
60  	@Test
61  	public void testInputSurroundingSpace() {
62  		String input = "  abc  ";
63  		String expected = "abc";
64  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
65  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
66  	}
67  	
68  	/**
69  	 * Tests unchained/chained execution with input containing surrounding whitespace.
70  	 */
71  	@Test
72  	public void testInputSurroundingWhitespace() {
73  		String input = "\tabc  \n";
74  		String expected = "abc";
75  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
76  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
77  	}
78  	
79  	/**
80  	 * Tests execution with a null input (should throw an Exception).
81  	 */
82  	@Test(expected = SuperCsvCellProcessorException.class)
83  	public void testWithNull() {
84  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
85  	}
86  	
87  }