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.HashSet;
22  import java.util.Set;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.supercsv.cellprocessor.ift.CellProcessor;
27  import org.supercsv.exception.SuperCsvCellProcessorException;
28  import org.supercsv.exception.SuperCsvConstraintViolationException;
29  import org.supercsv.mock.IdentityTransform;
30  
31  /**
32   * Tests the IsIncludedIn constraint.
33   * 
34   * @author Dominique De Vito
35   * @author James Bassett
36   */
37  public class IsIncludedInTest {
38  	
39  	private static final Set<Object> VALUE_SET = new HashSet<Object>();
40  	
41  	private static final Integer ONE = 1;
42  	private static final String TWO = "Two";
43  	private static final Double THREE = 3.0;
44  	
45  	static {
46  		VALUE_SET.add(ONE);
47  		VALUE_SET.add(TWO);
48  		VALUE_SET.add(THREE);
49  	}
50  	
51  	private CellProcessor processor;
52  	private CellProcessor processor2;
53  	private CellProcessor processorChain;
54  	private CellProcessor processorChain2;
55  	
56  	/**
57  	 * Sets up the processors for the test using all constructor combinations.
58  	 */
59  	@Before
60  	public void setUp() {
61  		processor = new IsIncludedIn(VALUE_SET);
62  		processor2 = new IsIncludedIn(VALUE_SET.toArray());
63  		processorChain = new IsIncludedIn(VALUE_SET, new IdentityTransform());
64  		processorChain2 = new IsIncludedIn(VALUE_SET.toArray(), new IdentityTransform());
65  	}
66  	
67  	/**
68  	 * Tests unchained/chained execution with values from the Set.
69  	 */
70  	@Test
71  	public void testValidInput() {
72  		assertEquals(ONE, processor.execute(ONE, ANONYMOUS_CSVCONTEXT));
73  		assertEquals(ONE, processor2.execute(ONE, ANONYMOUS_CSVCONTEXT));
74  		assertEquals(ONE, processorChain.execute(ONE, ANONYMOUS_CSVCONTEXT));
75  		assertEquals(ONE, processorChain2.execute(ONE, ANONYMOUS_CSVCONTEXT));
76  		
77  		assertEquals(TWO, processor.execute(TWO, ANONYMOUS_CSVCONTEXT));
78  		assertEquals(TWO, processor2.execute(TWO, ANONYMOUS_CSVCONTEXT));
79  		assertEquals(TWO, processorChain.execute(TWO, ANONYMOUS_CSVCONTEXT));
80  		assertEquals(TWO, processorChain2.execute(TWO, ANONYMOUS_CSVCONTEXT));
81  		
82  		assertEquals(THREE, processor.execute(THREE, ANONYMOUS_CSVCONTEXT));
83  		assertEquals(THREE, processor2.execute(THREE, ANONYMOUS_CSVCONTEXT));
84  		assertEquals(THREE, processorChain.execute(THREE, ANONYMOUS_CSVCONTEXT));
85  		assertEquals(THREE, processorChain2.execute(THREE, ANONYMOUS_CSVCONTEXT));
86  		
87  	}
88  	
89  	/**
90  	 * Tests execution with a value that's not in the Set (should throw an Exception).
91  	 */
92  	@Test(expected = SuperCsvConstraintViolationException.class)
93  	public void testWithInvalidInput() {
94  		processor.execute(4, ANONYMOUS_CSVCONTEXT);
95  	}
96  	
97  	/**
98  	 * Tests execution with a null input (should throw an Exception).
99  	 */
100 	@Test(expected = SuperCsvCellProcessorException.class)
101 	public void testWithNull() {
102 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
103 	}
104 	
105 	/**
106 	 * Tests construction with a null array (should throw an Exception).
107 	 */
108 	@Test(expected = NullPointerException.class)
109 	public void testConstructionWithNullArray() {
110 		new IsIncludedIn((Object[]) null);
111 	}
112 	
113 	/**
114 	 * Tests construction with an empty array (should throw an Exception).
115 	 */
116 	@Test(expected = IllegalArgumentException.class)
117 	public void testConstructionWithEmptyArray() {
118 		new IsIncludedIn(new Object[] {});
119 	}
120 	
121 	/**
122 	 * Tests construction with a null Set (should throw an Exception).
123 	 */
124 	@Test(expected = NullPointerException.class)
125 	public void testConstructionWithNullSet() {
126 		new IsIncludedIn((Set<Object>) null);
127 	}
128 	
129 	/**
130 	 * Tests construction with an empty Set (should throw an Exception).
131 	 */
132 	@Test(expected = IllegalArgumentException.class)
133 	public void testConstructionWithEmptySet() {
134 		new IsIncludedIn(new HashSet<Object>());
135 	}
136 	
137 }