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.SuperCsvConstraintViolationException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the Unique constraint.
31   * 
32   * @author Kasper B. Graversen
33   * @author James Bassett
34   */
35  public class UniqueTest {
36  	
37  	private CellProcessor processor;
38  	private CellProcessor processorChain;
39  	
40  	/**
41  	 * Sets up the processors for the test using all constructor combinations.
42  	 */
43  	@Before
44  	public void setUp() {
45  		processor = new Unique();
46  		processorChain = new Unique(new IdentityTransform());
47  	}
48  	
49  	/**
50  	 * Tests unchained/chained execution with valid (unique) String input.
51  	 */
52  	@Test
53  	public void testValidStringInput() {
54  		for( String input : Arrays.asList("1", "2", "3", "4", "5") ) {
55  			assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
56  			assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
57  		}
58  		
59  	}
60  	
61  	/**
62  	 * Tests unchained/chained execution with valid (unique) Integer input.
63  	 */
64  	@Test
65  	public void testValidIntegerInput() {
66  		for( Integer input : Arrays.asList(1, 2, 3, 4, 5) ) {
67  			assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
68  			assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
69  		}
70  		
71  	}
72  	
73  	/**
74  	 * Tests invalid (non-unique) String input.
75  	 */
76  	@Test(expected = SuperCsvConstraintViolationException.class)
77  	public void testInvalidStringInput() {
78  		assertEquals("duplicate", processor.execute("duplicate", ANONYMOUS_CSVCONTEXT));
79  		processor.execute("duplicate", ANONYMOUS_CSVCONTEXT);
80  	}
81  	
82  	/**
83  	 * Tests invalid (non-unique) Integer input.
84  	 */
85  	@Test(expected = SuperCsvConstraintViolationException.class)
86  	public void testInvalidIntegerInput() {
87  		assertEquals(Integer.valueOf(99), processor.execute(Integer.valueOf(99), ANONYMOUS_CSVCONTEXT));
88  		processor.execute(Integer.valueOf(99), ANONYMOUS_CSVCONTEXT);
89  		
90  	}
91  }