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 RequireSubStr constraint.
34   * 
35   * @author Kasper B. Graversen
36   * @author James Bassett
37   */
38  public class RequireSubStrTest {
39  	
40  	private static final String REQUIRED1 = "in";
41  	private static final String REQUIRED2 = "to";
42  	
43  	private CellProcessor processor;
44  	private CellProcessor processorChain;
45  	private CellProcessor processorChain2;
46  	private CellProcessor processorChain3;
47  	
48  	/**
49  	 * Sets up the processors for the test using all constructor combinations.
50  	 */
51  	@Before
52  	public void setUp() {
53  		processor = new RequireSubStr(REQUIRED1, REQUIRED2);
54  		processorChain = new RequireSubStr(Arrays.asList(REQUIRED1, REQUIRED2), new IdentityTransform());
55  		processorChain2 = new RequireSubStr(REQUIRED1, new IdentityTransform()); // only allows 1 substring
56  		processorChain3 = new RequireSubStr(new String[] { REQUIRED1, REQUIRED2 }, new IdentityTransform());
57  	}
58  	
59  	/**
60  	 * Tests unchained/chained execution with a String that contains all of the required substrings.
61  	 */
62  	@Test
63  	public void testValidInput() {
64  		String input = "to infinity and beyond!";
65  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
66  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
67  		assertEquals(input, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT)); // only has 1 substring
68  		assertEquals(input, processorChain3.execute(input, ANONYMOUS_CSVCONTEXT));
69  		
70  	}
71  	
72  	/**
73  	 * Tests input that doesn't contain any of the substrings (should throw an Exception).
74  	 */
75  	@Test(expected = SuperCsvConstraintViolationException.class)
76  	public void testInvalidInput() {
77  		String input = "no matches here";
78  		processor.execute(input, ANONYMOUS_CSVCONTEXT);
79  	}
80  	
81  	/**
82  	 * Tests execution with a null input (should throw an Exception).
83  	 */
84  	@Test(expected = SuperCsvCellProcessorException.class)
85  	public void testWithNull() {
86  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
87  	}
88  	
89  	/**
90  	 * Tests construction with a null array (should throw an Exception).
91  	 */
92  	@Test(expected = NullPointerException.class)
93  	public void testConstructionWithNullArray() {
94  		new RequireSubStr((String[]) null);
95  	}
96  	
97  	/**
98  	 * Tests construction with an empty array (should throw an Exception).
99  	 */
100 	@Test(expected = IllegalArgumentException.class)
101 	public void testConstructionWithEmptyArray() {
102 		new RequireSubStr(new String[] {});
103 	}
104 	
105 	/**
106 	 * Tests construction with a null List (should throw an Exception).
107 	 */
108 	@Test(expected = NullPointerException.class)
109 	public void testConstructionWithNullList() {
110 		new RequireSubStr((List<String>) null, new IdentityTransform());
111 	}
112 	
113 	/**
114 	 * Tests construction with an empty List (should throw an Exception).
115 	 */
116 	@Test(expected = IllegalArgumentException.class)
117 	public void testConstructionWithEmptyList() {
118 		new RequireSubStr(new ArrayList<String>(), new IdentityTransform());
119 	}
120 	
121 	/**
122 	 * Tests construction with a null array (should throw an Exception).
123 	 */
124 	@Test(expected = NullPointerException.class)
125 	public void testConstructionWithNullSubstring() {
126 		new RequireSubStr(new String[] { null });
127 	}
128 }