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.fail;
19  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.supercsv.exception.SuperCsvConstraintViolationException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the IsElementOf constraint.
31   * 
32   * @author James Bassett
33   */
34  public class IsElementOfTest {
35  	
36  	private IsElementOf processor;
37  	private IsElementOf chainedProcessor;
38  	private List<Object> input = Arrays.asList(new Object[] { "one", 2, 3.0, true, null });
39  	
40  	/**
41  	 * Sets up the IsElementOf processors.
42  	 */
43  	@Before
44  	public void setUp() {
45  		processor = new IsElementOf(input);
46  		chainedProcessor = new IsElementOf(input, new IdentityTransform());
47  	}
48  	
49  	/**
50  	 * Tests the IsElementOf constraint.
51  	 */
52  	@Test
53  	public void testIsElementOf() {
54  		
55  		for( Object o : input ) {
56  			processor.execute(o, ANONYMOUS_CSVCONTEXT);
57  		}
58  		
59  		try {
60  			processor.execute("not an element", ANONYMOUS_CSVCONTEXT);
61  			fail("should have thrown SuperCsvConstraintViolationException");
62  		}
63  		catch(final SuperCsvConstraintViolationException e) {}
64  		
65  	}
66  	
67  	/**
68  	 * Tests the IsElementOf constraint when chained to another processor.
69  	 */
70  	@Test
71  	public void testChainedIsElementOf() {
72  		
73  		for( Object o : input ) {
74  			chainedProcessor.execute(o, ANONYMOUS_CSVCONTEXT);
75  		}
76  		
77  		try {
78  			chainedProcessor.execute("not an element", ANONYMOUS_CSVCONTEXT);
79  			fail("should have thrown SuperCsvConstraintViolationException");
80  		}
81  		catch(final SuperCsvConstraintViolationException e) {}
82  		
83  	}
84  	
85  	/**
86  	 * Tests the 1 arg constructor with a null collection.
87  	 */
88  	@Test(expected = NullPointerException.class)
89  	public void testConstructorWithNullCollection() {
90  		new IsElementOf(null);
91  	}
92  	
93  	/**
94  	 * Tests the 2 arg constructor with a null collection.
95  	 */
96  	@Test(expected = NullPointerException.class)
97  	public void testChainedConstructorWithNullCollection() {
98  		new IsElementOf(null, new IdentityTransform());
99  	}
100 	
101 }