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.constraint;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.supercsv.cellprocessor.ift.CellProcessor;
28  import org.supercsv.exception.SuperCsvCellProcessorException;
29  import org.supercsv.exception.SuperCsvConstraintViolationException;
30  import org.supercsv.mock.IdentityTransform;
31  
32  /**
33   * Tests the ForbidSubStr constraint.
34   * 
35   * @author Kasper B. Graversen
36   * @author James Bassett
37   */
38  public class ForbidSubStrTest {
39  	
40  	private static final String FORBIDDEN = "C++";
41  	private static final String FORBIDDEN2 = "Microsoft";
42  	
43  	private CellProcessor processor;
44  	private CellProcessor processor2;
45  	private CellProcessor processorChain;
46  	private CellProcessor processorChain2;
47  	private CellProcessor processorChain3;
48  	
49  	/**
50  	 * Sets up the processors for the test using all constructor combinations.
51  	 */
52  	@Before
53  	public void setUp() {
54  		processor = new ForbidSubStr(Arrays.asList(FORBIDDEN, FORBIDDEN2));
55  		processor2 = new ForbidSubStr(FORBIDDEN, FORBIDDEN2);
56  		processorChain = new ForbidSubStr(Arrays.asList(FORBIDDEN, FORBIDDEN2), new IdentityTransform());
57  		processorChain2 = new ForbidSubStr(FORBIDDEN, new IdentityTransform());
58  		processorChain3 = new ForbidSubStr(new String[] { FORBIDDEN, FORBIDDEN2 }, new IdentityTransform());
59  	}
60  	
61  	/**
62  	 * Tests unchained/chained execution with a String that doesn't contain any forbidden Strings.
63  	 */
64  	@Test
65  	public void testValidInput() {
66  		String input = "I think Java is an awesome language";
67  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
68  		assertEquals(input, processor2.execute(input, ANONYMOUS_CSVCONTEXT));
69  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
70  		assertEquals(input, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT));
71  		assertEquals(input, processorChain3.execute(input, ANONYMOUS_CSVCONTEXT));
72  		
73  	}
74  	
75  	/**
76  	 * Tests input that contains the first forbidden String (should throw an Exception).
77  	 */
78  	@Test(expected = SuperCsvConstraintViolationException.class)
79  	public void testForbidden() {
80  		String input = "I think C++ is an awesome language"; // blasphemy!
81  		processor.execute(input, ANONYMOUS_CSVCONTEXT);
82  	}
83  	
84  	/**
85  	 * Tests input that contains the second forbidden String (should throw an Exception).
86  	 */
87  	@Test(expected = SuperCsvConstraintViolationException.class)
88  	public void testForbidden2() {
89  		String input = "I love Microsoft";
90  		processor.execute(input, ANONYMOUS_CSVCONTEXT);
91  	}
92  	
93  	/**
94  	 * Tests execution with a null input (should throw an Exception).
95  	 */
96  	@Test(expected = SuperCsvCellProcessorException.class)
97  	public void testWithNull() {
98  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
99  	}
100 	
101 	/**
102 	 * Tests construction with a null array (should throw an Exception).
103 	 */
104 	@Test(expected = NullPointerException.class)
105 	public void testConstructionWithNullArray() {
106 		new ForbidSubStr((String[]) null);
107 	}
108 	
109 	/**
110 	 * Tests construction with an empty array (should throw an Exception).
111 	 */
112 	@Test(expected = IllegalArgumentException.class)
113 	public void testConstructionWithEmptyArray() {
114 		new ForbidSubStr(new String[] {});
115 	}
116 	
117 	/**
118 	 * Tests construction with a null List (should throw an Exception).
119 	 */
120 	@Test(expected = NullPointerException.class)
121 	public void testConstructionWithNullList() {
122 		new ForbidSubStr((List<String>) null, new IdentityTransform());
123 	}
124 	
125 	/**
126 	 * Tests construction with an empty List (should throw an Exception).
127 	 */
128 	@Test(expected = IllegalArgumentException.class)
129 	public void testConstructionWithEmptyList() {
130 		new ForbidSubStr(new ArrayList<String>(), new IdentityTransform());
131 	}
132 	
133 	/**
134 	 * Tests construction with a null array (should throw an Exception).
135 	 */
136 	@Test(expected = NullPointerException.class)
137 	public void testConstructionWithNullSubstring() {
138 		new ForbidSubStr(new String[] { null });
139 	}
140 }