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 StrMinMax constraint.
30   * 
31   * @author Kasper B. Graversen
32   * @author James Bassett
33   */
34  public class StrMinMaxTest {
35  	
36  	private static final int MIN = 3;
37  	private static final int MAX = 6;
38  	
39  	private CellProcessor processor;
40  	private CellProcessor processorChain;
41  	
42  	/**
43  	 * Sets up the processors for the test using all constructor combinations.
44  	 */
45  	@Before
46  	public void setUp() {
47  		processor = new StrMinMax(MIN, MAX);
48  		processorChain = new StrMinMax(MIN, MAX, new IdentityTransform());
49  	}
50  	
51  	/**
52  	 * Tests unchained/chained execution with a String of valid length.
53  	 */
54  	@Test
55  	public void testValidInput() {
56  		String input = "valid";
57  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
58  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
59  	}
60  	
61  	/**
62  	 * Tests unchained/chained execution with a Integer String of valid length (should be converted to a String).
63  	 */
64  	@Test
65  	public void testValidIntegerString() {
66  		Integer input = 1234;
67  		String expected = "1234";
68  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
69  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
70  	}
71  	
72  	/**
73  	 * Tests unchained/chained execution with the minimum allowed length.
74  	 */
75  	@Test
76  	public void testMinBoundary() {
77  		String input = "123";
78  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
79  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
80  	}
81  	
82  	/**
83  	 * Tests unchained/chained execution with the maximum allowed length.
84  	 */
85  	@Test
86  	public void testMaxBoundary() {
87  		String input = "123456";
88  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
89  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
90  	}
91  	
92  	/**
93  	 * Tests execution with a length less than the minimum (should throw an Exception).
94  	 */
95  	@Test(expected = SuperCsvConstraintViolationException.class)
96  	public void testLessThanMin() {
97  		String input = "12";
98  		processor.execute(input, ANONYMOUS_CSVCONTEXT);
99  	}
100 	
101 	/**
102 	 * Tests execution with a length greater than the maximum (should throw an Exception).
103 	 */
104 	@Test(expected = SuperCsvConstraintViolationException.class)
105 	public void testGreaterThanMax() {
106 		String input = "1234567";
107 		processor.execute(input, ANONYMOUS_CSVCONTEXT);
108 	}
109 	
110 	/**
111 	 * Tests execution with a min < 0(should throw an Exception).
112 	 */
113 	@Test(expected = IllegalArgumentException.class)
114 	public void testWithNegativeMin() {
115 		new StrMinMax(-1, MAX);
116 	}
117 	
118 	/**
119 	 * Tests execution with a max < min (should throw an Exception).
120 	 */
121 	@Test(expected = IllegalArgumentException.class)
122 	public void testWithInvalidMaxMin() {
123 		new StrMinMax(MAX, MIN);
124 	}
125 	
126 	/**
127 	 * Tests execution with a null input (should throw an Exception).
128 	 */
129 	@Test(expected = SuperCsvCellProcessorException.class)
130 	public void testWithNull() {
131 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
132 	}
133 	
134 }