1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
31  
32  
33  
34  
35  public class StrReplaceTest {
36  	
37  	private static final String REGEX = "\\s+"; 
38  	private static final String REPLACEMENT = "_";
39  	
40  	private CellProcessor processor;
41  	private CellProcessor processorChain;
42  	
43  	
44  
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  
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  
65  
66  	@Test
67  	public void testNonStringInput() {
68  		StringBuilder input = new StringBuilder("This is a \tString with some \n whitespace"); 
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  
76  
77  	@Test(expected = NullPointerException.class)
78  	public void testWithNullRegex() {
79  		processor = new StrReplace(null, REPLACEMENT);
80  	}
81  	
82  	
83  
84  
85  	@Test(expected = NullPointerException.class)
86  	public void testWithNullReplacement() {
87  		processor = new StrReplace(REGEX, null);
88  	}
89  	
90  	
91  
92  
93  	@Test(expected = IllegalArgumentException.class)
94  	public void testWithEmptyRegex() {
95  		processor = new StrReplace("", REPLACEMENT);
96  	}
97  	
98  	
99  
100 
101 	@Test(expected = PatternSyntaxException.class)
102 	public void testWithInvalidRegex() {
103 		processor = new StrReplace("***", REPLACEMENT);
104 	}
105 	
106 	
107 
108 
109 	@Test(expected = SuperCsvCellProcessorException.class)
110 	public void testWithNull() {
111 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
112 	}
113 	
114 }