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 org.junit.Before;
22  import org.junit.Test;
23  import org.supercsv.SuperCsvTestUtils;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.exception.SuperCsvCellProcessorException;
26  import org.supercsv.exception.SuperCsvConstraintViolationException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the Strlen constraint.
31   * 
32   * @author Kasper B. Graversen
33   * @author James Bassett
34   */
35  public class StrlenTest {
36  	
37  	private static final int LENGTH1 = 2;
38  	private static final int LENGTH2 = 3;
39  	
40  	private CellProcessor processor;
41  	private CellProcessor processorChain;
42  	private CellProcessor processorChain2;
43  	
44  	/**
45  	 * Sets up the processors for the test using all constructor combinations.
46  	 */
47  	@Before
48  	public void setUp() {
49  		processor = new Strlen(LENGTH1, LENGTH2);
50  		processorChain = new Strlen(LENGTH1, new IdentityTransform()); // only allows 1 length
51  		processorChain2 = new Strlen(new int[] { LENGTH1, LENGTH2 }, new IdentityTransform());
52  	}
53  	
54  	/**
55  	 * Tests unchained/chained execution with inputs of valid lengths.
56  	 */
57  	@Test
58  	public void testValidInput() {
59  		String input = "OK";
60  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
61  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
62  		assertEquals(input, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT));
63  		
64  		input = "yep";
65  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
66  		// skip 'processorChain' as it only has 1 valid length
67  		assertEquals(input, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT));
68  		
69  	}
70  	
71  	/**
72  	 * Tests execution with an input with an unexpected length (should throw an Exception).
73  	 */
74  	@Test(expected = SuperCsvConstraintViolationException.class)
75  	public void testInvalidInput() {
76  		processor.execute("four", SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT);
77  	}
78  	
79  	/**
80  	 * Tests construction of the processor with a negative length (should throw an Exception).
81  	 */
82  	@Test(expected = IllegalArgumentException.class)
83  	public void testNegativeLength() {
84  		new Strlen(-1);
85  	}
86  	
87  	/**
88  	 * Tests execution with a null input (should throw an Exception).
89  	 */
90  	@Test(expected = SuperCsvCellProcessorException.class)
91  	public void testWithNull() {
92  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
93  	}
94  	
95  	/**
96  	 * Tests construction with a null array (should throw an Exception).
97  	 */
98  	@Test(expected = NullPointerException.class)
99  	public void testConstructionWithNullArray() {
100 		new Strlen((int[]) null);
101 	}
102 	
103 	/**
104 	 * Tests construction with an empty array (should throw an Exception).
105 	 */
106 	@Test(expected = IllegalArgumentException.class)
107 	public void testConstructionWithEmptyArray() {
108 		new Strlen(new int[] {});
109 	}
110 }