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.junit.Assert.assertNull;
20  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.exception.SuperCsvConstraintViolationException;
26  import org.supercsv.mock.IdentityTransform;
27  
28  /**
29   * Tests the Equals constraint.
30   * 
31   * @author James Bassett
32   */
33  public class EqualsTest {
34  	
35  	private static final String EXPECTED_VALUE = "expected";
36  	
37  	private CellProcessor processor;
38  	private CellProcessor processor2;
39  	private CellProcessor processorChain;
40  	private CellProcessor processorChain2;
41  	
42  	/**
43  	 * Sets up the processors for the test using all constructor combinations.
44  	 */
45  	@Before
46  	public void setUp() {
47  		processor = new Equals();
48  		processor2 = new Equals(EXPECTED_VALUE);
49  		processorChain = new Equals(new IdentityTransform());
50  		processorChain2 = new Equals(EXPECTED_VALUE, new IdentityTransform());
51  	}
52  	
53  	/**
54  	 * Tests unchained/chained execution with valid (equal) input. The non-constant processors have their constant value
55  	 * loaded after the first execution (no check is done on the first execution), so the subsequent call should check
56  	 * against that value.
57  	 */
58  	@Test
59  	public void testValidInput() {
60  		assertEquals(EXPECTED_VALUE, processor.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
61  		assertEquals(EXPECTED_VALUE, processor2.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
62  		assertEquals(EXPECTED_VALUE, processorChain.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
63  		assertEquals(EXPECTED_VALUE, processorChain2.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
64  		
65  		// repeat for the benefit of the 'non-constant' processors which will now check against the first value found
66  		assertEquals(EXPECTED_VALUE, processor.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
67  		assertEquals(EXPECTED_VALUE, processorChain.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
68  		
69  		// and check the 'constant' processors again, just for good measure
70  		assertEquals(EXPECTED_VALUE, processor2.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
71  		assertEquals(EXPECTED_VALUE, processorChain2.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
72  	}
73  	
74  	/**
75  	 * Tests execution with a null expected value and null input.
76  	 */
77  	@Test
78  	public void testNullConstantWithNull() {
79  		CellProcessor equalsWithNullConstant = new Equals((Object) null);
80  		assertNull(equalsWithNullConstant.execute(null, ANONYMOUS_CSVCONTEXT));
81  	}
82  	
83  	/**
84  	 * Tests execution with a null expected value, but a non-null input (should throw an exception).
85  	 */
86  	@Test(expected = SuperCsvConstraintViolationException.class)
87  	public void testNullConstantWithNonNull() {
88  		CellProcessor equalsWithNullConstant = new Equals((Object) null);
89  		equalsWithNullConstant.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT);
90  	}
91  	
92  	/**
93  	 * Tests a value that's not equal to the first value encountered (should throw an exception).
94  	 */
95  	@Test(expected = SuperCsvConstraintViolationException.class)
96  	public void testNotEqual() {
97  		assertEquals(EXPECTED_VALUE, processor.execute(EXPECTED_VALUE, ANONYMOUS_CSVCONTEXT));
98  		processor.execute("something else", ANONYMOUS_CSVCONTEXT);
99  	}
100 	
101 	/**
102 	 * Tests a value that's not equal to the constant value (should throw an exception).
103 	 */
104 	@Test(expected = SuperCsvConstraintViolationException.class)
105 	public void testNotEqualWithConstant() {
106 		processor2.execute("something else", ANONYMOUS_CSVCONTEXT);
107 	}
108 	
109 }