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.Arrays;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.supercsv.cellprocessor.ift.CellProcessor;
26  import org.supercsv.exception.SuperCsvCellProcessorException;
27  import org.supercsv.exception.SuperCsvConstraintViolationException;
28  import org.supercsv.mock.IdentityTransform;
29  import org.supercsv.mock.PersonBean;
30  
31  /**
32   * Tests the UniqueHashCode constraint.
33   * 
34   * @author James Bassett
35   */
36  public class UniqueHashCodeTest {
37  	
38  	private CellProcessor processor;
39  	private CellProcessor processorChain;
40  	
41  	/**
42  	 * Sets up the processors for the test using all constructor combinations.
43  	 */
44  	@Before
45  	public void setUp() {
46  		processor = new UniqueHashCode();
47  		processorChain = new UniqueHashCode(new IdentityTransform());
48  	}
49  	
50  	/**
51  	 * Tests unchained/chained execution with a String inputs that have different hash codes.
52  	 */
53  	@Test
54  	public void testValidStringInput() {
55  		for( String input : Arrays.asList("a", "b", "c", "d", "A", "B", "C", "D") ) {
56  			assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
57  			assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
58  		}
59  		
60  	}
61  	
62  	/**
63  	 * Tests unchained/chained execution with a Object inputs that have different hash codes.
64  	 */
65  	@Test
66  	public void testValidObjectInput() {
67  		
68  		PersonBean p1 = new PersonBean();
69  		p1.setFirstName("Neo");
70  		PersonBean p2 = new PersonBean();
71  		p2.setFirstName("Trinity");
72  		PersonBean p3 = new PersonBean();
73  		p3.setFirstName("Morpheus");
74  		PersonBean p4 = new PersonBean();
75  		p4.setFirstName("Switch");
76  		
77  		for( PersonBean input : Arrays.asList(p1, p2, p3, p4) ) {
78  			assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
79  			assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
80  		}
81  		
82  	}
83  	
84  	/**
85  	 * Tests execution with String inputs that have the same hash code (should throw an Exception).
86  	 */
87  	@Test(expected = SuperCsvConstraintViolationException.class)
88  	public void testInvalidStringInput() {
89  		assertEquals("a", processor.execute("a", ANONYMOUS_CSVCONTEXT));
90  		assertEquals("b", processor.execute("b", ANONYMOUS_CSVCONTEXT));
91  		assertEquals("c", processor.execute("c", ANONYMOUS_CSVCONTEXT));
92  		processor.execute("b", ANONYMOUS_CSVCONTEXT);
93  	}
94  	
95  	/**
96  	 * Tests execution with Object inputs that have the same hash code (should throw an Exception).
97  	 */
98  	@Test(expected = SuperCsvConstraintViolationException.class)
99  	public void testInvalidObjectInput() {
100 		
101 		PersonBean p1 = new PersonBean();
102 		p1.setFirstName("Neo");
103 		PersonBean p2 = new PersonBean();
104 		p2.setFirstName("Trinity");
105 		PersonBean p3 = new PersonBean();
106 		p3.setFirstName("Morpheus");
107 		PersonBean p4 = new PersonBean();
108 		p4.setFirstName("Neo"); // invalid! there's only one 'one'!
109 		
110 		assertEquals(p1, processor.execute(p1, ANONYMOUS_CSVCONTEXT));
111 		assertEquals(p2, processor.execute(p2, ANONYMOUS_CSVCONTEXT));
112 		assertEquals(p3, processor.execute(p3, ANONYMOUS_CSVCONTEXT));
113 		processor.execute(p4, ANONYMOUS_CSVCONTEXT);
114 		
115 	}
116 	
117 	/**
118 	 * Tests execution with a null input (should throw an Exception).
119 	 */
120 	@Test(expected = SuperCsvCellProcessorException.class)
121 	public void testWithNull() {
122 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
123 	}
124 	
125 }