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.cellprocessor.ift.CellProcessor;
24  import org.supercsv.exception.SuperCsvCellProcessorException;
25  import org.supercsv.exception.SuperCsvConstraintViolationException;
26  import org.supercsv.mock.IdentityTransform;
27  
28  /**
29   * Tests the RequireHashCode constraint.
30   * 
31   * @author Kasper B. Graversen
32   * @author James Bassett
33   */
34  public class RequireHashCodeTest {
35  	
36  	private static final String INPUT1 = "One";
37  	private static final String INPUT2 = "Two";
38  	private static final int HASH1 = INPUT1.hashCode();
39  	private static final int HASH2 = INPUT2.hashCode();
40  	
41  	private CellProcessor processor;
42  	private CellProcessor processorChain;
43  	private CellProcessor processorChain2;
44  	
45  	/**
46  	 * Sets up the processors for the test using all constructor combinations.
47  	 */
48  	@Before
49  	public void setUp() {
50  		processor = new RequireHashCode(HASH1, HASH2);
51  		processorChain = new RequireHashCode(HASH1, new IdentityTransform());
52  		processorChain2 = new RequireHashCode(new int[] { HASH1, HASH2 }, new IdentityTransform());
53  	}
54  	
55  	/**
56  	 * Tests unchained/chained execution with valid values.
57  	 */
58  	@Test
59  	public void testValidInput() {
60  		assertEquals(INPUT1, processor.execute(INPUT1, ANONYMOUS_CSVCONTEXT));
61  		assertEquals(INPUT1, processorChain.execute(INPUT1, ANONYMOUS_CSVCONTEXT));
62  		assertEquals(INPUT1, processorChain2.execute(INPUT1, ANONYMOUS_CSVCONTEXT));
63  		
64  		assertEquals(INPUT2, processor.execute(INPUT2, ANONYMOUS_CSVCONTEXT));
65  		// 'processorChain' doesn't have a second value to test with
66  		assertEquals(INPUT2, processorChain2.execute(INPUT2, ANONYMOUS_CSVCONTEXT));
67  	}
68  	
69  	/**
70  	 * Tests execution with input that doesn't match any of the hashcodes (should throw an Exception).
71  	 */
72  	@Test(expected = SuperCsvConstraintViolationException.class)
73  	public void testInvalidInput() {
74  		String input = "invalid";
75  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
76  	}
77  	
78  	/**
79  	 * Tests execution with a null input (should throw an Exception).
80  	 */
81  	@Test(expected = SuperCsvCellProcessorException.class)
82  	public void testWithNull() {
83  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
84  	}
85  	
86  	/**
87  	 * Tests construction with a null array (should throw an Exception).
88  	 */
89  	@Test(expected = NullPointerException.class)
90  	public void testConstructionWithNullArray() {
91  		new RequireHashCode((int[]) null);
92  	}
93  	
94  	/**
95  	 * Tests construction with an empty array (should throw an Exception).
96  	 */
97  	@Test(expected = IllegalArgumentException.class)
98  	public void testConstructionWithEmptyArray() {
99  		new RequireHashCode(new int[] {});
100 	}
101 }