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.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
22  
23  import java.util.Arrays;
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.mock.IdentityTransform;
30  
31  /**
32   * Tests the ParseBool processor.
33   * 
34   * @author Kasper B. Graversen
35   * @author James Bassett
36   */
37  public class ParseBoolTest {
38  	
39  	private static final String TRUE_VALUE = "y";
40  	private static final String FALSE_VALUE = "n";
41  	private static final String[] DEFAULT_TRUE_VALUES = new String[] { "1", "true", "t", "y" };
42  	private static final String[] DEFAULT_FALSE_VALUES = new String[] { "0", "false", "f", "n" };
43  	private static final String[] MIXED_TRUE_VALUES = new String[] { "1", "true", "True", "TRUE", "t", "T", "y", "Y" };
44  	private static final String[] MIXED_FALSE_VALUES = new String[] { "0", "false", "False", "FALSE", "f", "F", "n",
45  		"N" };
46  	
47  	private CellProcessor processor;
48  	private CellProcessor processor2;
49  	private CellProcessor processor3;
50  	private CellProcessor processorChain;
51  	private CellProcessor processorChain2;
52  	private CellProcessor processorChain3;
53  	private CellProcessor matchCaseProcessor;
54  	private CellProcessor matchCaseProcessor2;
55  	private CellProcessor matchCaseProcessor3;
56  	private CellProcessor matchCaseProcessorChain;
57  	private CellProcessor matchCaseProcessorChain2;
58  	private CellProcessor matchCaseProcessorChain3;
59  	
60  	/**
61  	 * Sets up the processors for the test using all constructor combinations.
62  	 */
63  	@Before
64  	public void setUp() {
65  		// so many constructors to test...sigh
66  		processor = new ParseBool();
67  		processor2 = new ParseBool(TRUE_VALUE, FALSE_VALUE);
68  		processor3 = new ParseBool(new String[] { TRUE_VALUE }, new String[] { FALSE_VALUE });
69  		processorChain = new ParseBool(new IdentityTransform());
70  		processorChain2 = new ParseBool(TRUE_VALUE, FALSE_VALUE, new IdentityTransform());
71  		processorChain3 = new ParseBool(new String[] { TRUE_VALUE }, new String[] { FALSE_VALUE },
72  			new IdentityTransform());
73  		matchCaseProcessor = new ParseBool(false);
74  		matchCaseProcessor2 = new ParseBool(TRUE_VALUE, FALSE_VALUE, false);
75  		matchCaseProcessor3 = new ParseBool(new String[] { TRUE_VALUE }, new String[] { FALSE_VALUE }, false);
76  		matchCaseProcessorChain = new ParseBool(false, new IdentityTransform());
77  		matchCaseProcessorChain2 = new ParseBool(TRUE_VALUE, FALSE_VALUE, false, new IdentityTransform());
78  		matchCaseProcessorChain3 = new ParseBool(new String[] { TRUE_VALUE }, new String[] { FALSE_VALUE }, false,
79  			new IdentityTransform());
80  	}
81  	
82  	/**
83  	 * Tests unchained/chained execution with valid true/false values.
84  	 */
85  	@Test
86  	public void testValidInput() {
87  		
88  		// processors using default true/false values
89  		for( CellProcessor cp : Arrays.asList(processor, processorChain) ) {
90  			for( String trueValue : MIXED_TRUE_VALUES ) {
91  				assertTrue((Boolean) cp.execute(trueValue, ANONYMOUS_CSVCONTEXT));
92  			}
93  			for( String falseValue : MIXED_FALSE_VALUES ) {
94  				assertFalse((Boolean) cp.execute(falseValue, ANONYMOUS_CSVCONTEXT));
95  			}
96  		}
97  		
98  		// other processors with single supplied true/false values
99  		for( CellProcessor processor : Arrays.asList(processor2, processor3, processorChain2, processorChain3) ) {
100 			assertTrue((Boolean) processor.execute(TRUE_VALUE, ANONYMOUS_CSVCONTEXT));
101 			assertTrue((Boolean) processor.execute(TRUE_VALUE.toUpperCase(), ANONYMOUS_CSVCONTEXT));
102 			assertFalse((Boolean) processor.execute(FALSE_VALUE, ANONYMOUS_CSVCONTEXT));
103 			assertFalse((Boolean) processor.execute(FALSE_VALUE.toUpperCase(), ANONYMOUS_CSVCONTEXT));
104 		}
105 		
106 	}
107 	
108 	/**
109 	 * Tests unchained/chained execution with valid true/false values using processors with ignoreCase disabled.
110 	 */
111 	@Test
112 	public void testMatchCaseProcessorsWithValidInput() {
113 		
114 		// processors using default true/false values
115 		for( CellProcessor processor : Arrays.asList(matchCaseProcessor, matchCaseProcessorChain) ) {
116 			for( String trueValue : DEFAULT_TRUE_VALUES ) {
117 				assertTrue((Boolean) processor.execute(trueValue, ANONYMOUS_CSVCONTEXT));
118 			}
119 			for( String falseValue : DEFAULT_FALSE_VALUES ) {
120 				assertFalse((Boolean) processor.execute(falseValue, ANONYMOUS_CSVCONTEXT));
121 			}
122 		}
123 		
124 		// other processors with single supplied true/false values
125 		for( CellProcessor processor : Arrays.asList(matchCaseProcessor2, matchCaseProcessor3,
126 			matchCaseProcessorChain2, matchCaseProcessorChain3) ) {
127 			assertTrue((Boolean) processor.execute(TRUE_VALUE, ANONYMOUS_CSVCONTEXT));
128 			assertFalse((Boolean) processor.execute(FALSE_VALUE, ANONYMOUS_CSVCONTEXT));
129 		}
130 		
131 	}
132 	
133 	/**
134 	 * Tests unchained/chained execution with invalid true/false values using processors with ignoreCase disabled.
135 	 */
136 	@Test
137 	public void testMatchCaseProcessorsWithInvalidInput() {
138 		
139 		for( CellProcessor processor : Arrays.asList(matchCaseProcessor, matchCaseProcessor2, matchCaseProcessor3,
140 			matchCaseProcessorChain, matchCaseProcessorChain2, matchCaseProcessorChain3) ) {
141 			try {
142 				assertTrue((Boolean) processor.execute(TRUE_VALUE.toUpperCase(), ANONYMOUS_CSVCONTEXT));
143 				fail("expecting SuperCsvCellProcessorException");
144 			}
145 			catch(SuperCsvCellProcessorException e) {}
146 			try {
147 				assertFalse((Boolean) processor.execute(FALSE_VALUE.toUpperCase(), ANONYMOUS_CSVCONTEXT));
148 				fail("expecting SuperCsvCellProcessorException");
149 			}
150 			catch(SuperCsvCellProcessorException e) {}
151 		}
152 		
153 	}
154 	
155 	/**
156 	 * Tests execution with an non-boolean String input (should throw an exception).
157 	 */
158 	@Test(expected = SuperCsvCellProcessorException.class)
159 	public void testWithNonBooleanString() {
160 		processor.execute("not a boolean!", ANONYMOUS_CSVCONTEXT);
161 	}
162 	
163 	/**
164 	 * Tests execution with a non-String input (should throw an exception).
165 	 */
166 	@Test(expected = SuperCsvCellProcessorException.class)
167 	public void testWithNonString() {
168 		processor.execute(1, ANONYMOUS_CSVCONTEXT);
169 	}
170 	
171 	/**
172 	 * Tests execution with a null input (should throw an Exception).
173 	 */
174 	@Test(expected = SuperCsvCellProcessorException.class)
175 	public void testWithNull() {
176 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
177 	}
178 	
179 	/**
180 	 * Tests execution with an empty-String input (should throw an exception).
181 	 */
182 	@Test(expected = SuperCsvCellProcessorException.class)
183 	public void testWithEmptyString() {
184 		processor.execute("", ANONYMOUS_CSVCONTEXT);
185 	}
186 	
187 	/**
188 	 * Tests construction with a null true String (should throw an Exception).
189 	 */
190 	@Test(expected = NullPointerException.class)
191 	public void testConstructionWithNullTrueString() {
192 		new ParseBool((String) null, FALSE_VALUE);
193 	}
194 	
195 	/**
196 	 * Tests construction with a null false String (should throw an Exception).
197 	 */
198 	@Test(expected = NullPointerException.class)
199 	public void testConstructionWithNullFalseString() {
200 		new ParseBool(TRUE_VALUE, (String) null);
201 	}
202 	
203 	/**
204 	 * Tests construction with a null true array (should throw an Exception).
205 	 */
206 	@Test(expected = NullPointerException.class)
207 	public void testConstructionWithNullTrueArray() {
208 		new ParseBool((String[]) null, new String[] { FALSE_VALUE });
209 	}
210 	
211 	/**
212 	 * Tests construction with a null false array (should throw an Exception).
213 	 */
214 	@Test(expected = NullPointerException.class)
215 	public void testConstructionWithNullFalseArray() {
216 		new ParseBool(new String[] { TRUE_VALUE }, (String[]) null);
217 	}
218 	
219 	/**
220 	 * Tests construction with an empty true array (should throw an Exception).
221 	 */
222 	@Test(expected = IllegalArgumentException.class)
223 	public void testConstructionWithEmptyTrueArray() {
224 		new ParseBool(new String[] {}, new String[] { FALSE_VALUE });
225 	}
226 	
227 	/**
228 	 * Tests construction with an empty false array (should throw an Exception).
229 	 */
230 	@Test(expected = IllegalArgumentException.class)
231 	public void testConstructionWithEmptyFalseArray() {
232 		new ParseBool(new String[] { TRUE_VALUE }, new String[] {});
233 	}
234 	
235 }