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 java.util.regex.PatternSyntaxException;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.supercsv.cellprocessor.ift.CellProcessor;
26  import org.supercsv.exception.SuperCsvCellProcessorException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the StrReplace processor.
31   * 
32   * @author Kasper B. Graversen
33   * @author James Bassett
34   */
35  public class StrReplaceTest {
36  	
37  	private static final String REGEX = "\\s+"; // whitespace
38  	private static final String REPLACEMENT = "_";
39  	
40  	private CellProcessor processor;
41  	private CellProcessor processorChain;
42  	
43  	/**
44  	 * Sets up the processors for the test using all constructor combinations.
45  	 */
46  	@Before
47  	public void setUp() {
48  		processor = new StrReplace(REGEX, REPLACEMENT);
49  		processorChain = new StrReplace(REGEX, REPLACEMENT, new IdentityTransform());
50  	}
51  	
52  	/**
53  	 * Tests unchained/chained execution with valid input.
54  	 */
55  	@Test
56  	public void testValidInput() {
57  		String input = "This is a \tString with some \n whitespace";
58  		String expected = "This_is_a_String_with_some_whitespace";
59  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
60  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
61  	}
62  	
63  	/**
64  	 * Tests unchained/chained execution with non-String input (toString() is called first).
65  	 */
66  	@Test
67  	public void testNonStringInput() {
68  		StringBuilder input = new StringBuilder("This is a \tString with some \n whitespace"); // how lazy!
69  		String expected = "This_is_a_String_with_some_whitespace";
70  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
71  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
72  	}
73  	
74  	/**
75  	 * Tests execution with a null regex (should throw an Exception).
76  	 */
77  	@Test(expected = NullPointerException.class)
78  	public void testWithNullRegex() {
79  		processor = new StrReplace(null, REPLACEMENT);
80  	}
81  	
82  	/**
83  	 * Tests execution with a null replacement (should throw an Exception).
84  	 */
85  	@Test(expected = NullPointerException.class)
86  	public void testWithNullReplacement() {
87  		processor = new StrReplace(REGEX, null);
88  	}
89  	
90  	/**
91  	 * Tests execution with an empty regex (should throw an Exception).
92  	 */
93  	@Test(expected = IllegalArgumentException.class)
94  	public void testWithEmptyRegex() {
95  		processor = new StrReplace("", REPLACEMENT);
96  	}
97  	
98  	/**
99  	 * Tests execution with a null input (should throw an Exception).
100 	 */
101 	@Test(expected = PatternSyntaxException.class)
102 	public void testWithInvalidRegex() {
103 		processor = new StrReplace("***", REPLACEMENT);
104 	}
105 	
106 	/**
107 	 * Tests execution with a null input (should throw an Exception).
108 	 */
109 	@Test(expected = SuperCsvCellProcessorException.class)
110 	public void testWithNull() {
111 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
112 	}
113 	
114 }