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  import static org.supercsv.cellprocessor.constraint.LMinMax.MAX_INTEGER;
21  import static org.supercsv.cellprocessor.constraint.LMinMax.MIN_INTEGER;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.supercsv.cellprocessor.StrReplace;
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 LMinMax constraint.
33   * 
34   * @author Kasper B. Graversen
35   * @author James Bassett
36   */
37  public class LMinMaxTest {
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 LMinMax(MIN_INTEGER, MAX_INTEGER);
48  		processorChain = new LMinMax(MIN_INTEGER, MAX_INTEGER, new IdentityTransform());
49  	}
50  	
51  	/**
52  	 * Tests unchained/chained execution with a long in the range.
53  	 */
54  	@Test
55  	public void testValidLong() {
56  		long input = 123L;
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 long String in the range (should be converted to a Long).
63  	 */
64  	@Test
65  	public void testValidLongString() {
66  		String input = "123";
67  		Long expected = 123L;
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 value.
74  	 */
75  	@Test
76  	public void testMinBoundary() {
77  		long input = MIN_INTEGER;
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 value.
84  	 */
85  	@Test
86  	public void testMaxBoundary() {
87  		long input = MAX_INTEGER;
88  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
89  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
90  	}
91  	
92  	/**
93  	 * Tests that this processor can be chained after a StringCellProcessor.
94  	 */
95  	@Test
96  	public void testChainedAfterStringCellProcessor() {
97  		final CellProcessor chain = new StrReplace("zero", "0", new LMinMax(MIN_INTEGER, MAX_INTEGER));
98  		assertEquals(0L, chain.execute("zero", ANONYMOUS_CSVCONTEXT));
99  	}
100 	
101 	/**
102 	 * Tests execution with a value less than the minimum (should throw an Exception).
103 	 */
104 	@Test(expected = SuperCsvConstraintViolationException.class)
105 	public void testLessThanMin() {
106 		long lessThanMin = MIN_INTEGER - 1L;
107 		processor.execute(lessThanMin, ANONYMOUS_CSVCONTEXT);
108 	}
109 	
110 	/**
111 	 * Tests execution with a value greater than the maximum (should throw an Exception).
112 	 */
113 	@Test(expected = SuperCsvConstraintViolationException.class)
114 	public void testGreaterThanMax() {
115 		long greaterThanMax = MAX_INTEGER + 1L;
116 		processor.execute(greaterThanMax, ANONYMOUS_CSVCONTEXT);
117 	}
118 	
119 	/**
120 	 * Tests execution with a String that can't be parsed to a Long (should throw an Exception).
121 	 */
122 	@Test(expected = SuperCsvCellProcessorException.class)
123 	public void testWithNonLongString() {
124 		processor.execute("not long!", ANONYMOUS_CSVCONTEXT);
125 	}
126 	
127 	/**
128 	 * Tests execution with a max < min (should throw an Exception).
129 	 */
130 	@Test(expected = IllegalArgumentException.class)
131 	public void testWithInvalidMaxMin() {
132 		new LMinMax(MAX_INTEGER, MIN_INTEGER);
133 	}
134 	
135 	/**
136 	 * Tests execution with a null input (should throw an Exception).
137 	 */
138 	@Test(expected = SuperCsvCellProcessorException.class)
139 	public void testWithNull() {
140 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
141 	}
142 }