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 Token processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class TokenTest {
34  	
35  	private static final String TOKEN = "foo";
36  	private static final String REPLACEMENT = "bar";
37  	private static final int TOKEN2 = 2;
38  	private static final String REPLACEMENT2 = "two";
39  	
40  	private CellProcessor processor;
41  	private CellProcessor processor2;
42  	private CellProcessor processorChain;
43  	private CellProcessor processorChain2;
44  	
45  	/**
46  	 * Sets up the processors for the test using all constructor combinations.
47  	 */
48  	@Before
49  	public void setUp() {
50  		// foo -> bar
51  		processor = new Token(TOKEN, REPLACEMENT);
52  		processorChain = new Token(TOKEN, REPLACEMENT, new IdentityTransform());
53  		
54  		// 2 -> two
55  		processor2 = new Token(TOKEN2, REPLACEMENT2);
56  		processorChain2 = new Token(TOKEN2, REPLACEMENT2, new IdentityTransform());
57  	}
58  	
59  	/**
60  	 * Tests unchained/chained execution with valid input.
61  	 */
62  	@Test
63  	public void testValidInput() {
64  		// string input
65  		assertEquals(REPLACEMENT, processor.execute(TOKEN, ANONYMOUS_CSVCONTEXT));
66  		assertEquals(REPLACEMENT, processorChain.execute(TOKEN, ANONYMOUS_CSVCONTEXT));
67  		
68  		// int input
69  		assertEquals(REPLACEMENT2, processor2.execute(TOKEN2, ANONYMOUS_CSVCONTEXT));
70  		assertEquals(REPLACEMENT2, processorChain2.execute(TOKEN2, ANONYMOUS_CSVCONTEXT));
71  	}
72  	
73  	/**
74  	 * Tests unchained/chained execution when the token is not found (should return input unchanged).
75  	 */
76  	@Test
77  	public void testTokenNotFound() {
78  		// expecting 'foo', not 2
79  		assertEquals(TOKEN2, processor.execute(TOKEN2, ANONYMOUS_CSVCONTEXT));
80  		assertEquals(TOKEN2, processorChain.execute(TOKEN2, ANONYMOUS_CSVCONTEXT));
81  		
82  		// expecting 2, not 'foo'
83  		assertEquals(TOKEN, processor2.execute(TOKEN, ANONYMOUS_CSVCONTEXT));
84  		assertEquals(TOKEN, processorChain2.execute(TOKEN, ANONYMOUS_CSVCONTEXT));
85  	}
86  	
87  	/**
88  	 * Tests execution with a null input (should throw an Exception).
89  	 */
90  	@Test(expected = SuperCsvCellProcessorException.class)
91  	public void testWithNull() {
92  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
93  	}
94  }