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 org.junit.Before;
22  import org.junit.Test;
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.exception.SuperCsvCellProcessorException;
25  import org.supercsv.exception.SuperCsvConstraintViolationException;
26  import org.supercsv.mock.IdentityTransform;
27  
28  /**
29   * Tests the StrNotNullOrEmpty constraint.
30   * 
31   * @author Dominique De Vito
32   * @author James Bassett
33   */
34  public class StrNotNullOrEmptyTest {
35  	
36  	private CellProcessor processor;
37  	private CellProcessor processorChain;
38  	
39  	/**
40  	 * Sets up the processors for the test using all constructor combinations.
41  	 */
42  	@Before
43  	public void setUp() {
44  		processor = new StrNotNullOrEmpty();
45  		processorChain = new StrNotNullOrEmpty(new IdentityTransform());
46  	}
47  	
48  	/**
49  	 * Tests unchained/chained execution with a non-null/empty value.
50  	 */
51  	@Test
52  	public void testValidInput() {
53  		String input = "not null or empty!";
54  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
55  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
56  	}
57  	
58  	/**
59  	 * Tests execution with a empty String (should throw an Exception).
60  	 */
61  	@Test(expected = SuperCsvCellProcessorException.class)
62  	public void testEmptyString() {
63  		String input = "";
64  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
65  	}
66  	
67  	/**
68  	 * Tests execution with a non-String input (should throw an Exception).
69  	 */
70  	@Test(expected = SuperCsvCellProcessorException.class)
71  	public void testNonStringInput() {
72  		processor.execute(123, ANONYMOUS_CSVCONTEXT);
73  	}
74  	
75  	/**
76  	 * Tests execution with a null input (should throw an Exception).
77  	 */
78  	@Test(expected = SuperCsvConstraintViolationException.class)
79  	public void testWithNull() {
80  		processor.execute(null, ANONYMOUS_CSVCONTEXT);
81  	}
82  	
83  }