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.StrReplace;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.exception.SuperCsvCellProcessorException;
26  import org.supercsv.exception.SuperCsvConstraintViolationException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the DMinMax constaint.
31   * 
32   * @author James Bassett
33   */
34  public class DMinMaxTest {
35  	
36  	private static final double MIN = -5;
37  	private static final double MAX = 10;
38  	private static final double IN_RANGE = 0;
39  	
40  	private CellProcessor processor;
41  	private CellProcessor processorChain;
42  	
43  	/**
44  	 * Sets up the processors for the test using all constructor combinations.
45  	 */
46  	@Before
47  	public void setUp() {
48  		processor = new DMinMax(MIN, MAX);
49  		processorChain = new DMinMax(MIN, MAX, new IdentityTransform());
50  	}
51  	
52  	/**
53  	 * Tests unchained/chained execution with a valid Double.
54  	 */
55  	@Test
56  	public void testValidDouble() {
57  		assertEquals(IN_RANGE, processor.execute(IN_RANGE, ANONYMOUS_CSVCONTEXT));
58  		assertEquals(IN_RANGE, processorChain.execute(IN_RANGE, ANONYMOUS_CSVCONTEXT));
59  	}
60  	
61  	/**
62  	 * Tests unchained/chained execution with a valid String.
63  	 */
64  	@Test
65  	public void testValidString() {
66  		assertEquals(IN_RANGE, processor.execute(String.valueOf(IN_RANGE), ANONYMOUS_CSVCONTEXT));
67  		assertEquals(IN_RANGE, processorChain.execute(String.valueOf(IN_RANGE), ANONYMOUS_CSVCONTEXT));
68  	}
69  	
70  	/**
71  	 * Tests that this processor can be chained after a StringCellProcessor.
72  	 */
73  	@Test
74  	public void testChainedAfterStringCellProcessor() {
75  		final CellProcessor chain = new StrReplace("zero", "0", new DMinMax(MIN, MAX));
76  		assertEquals(0.0, chain.execute("zero", ANONYMOUS_CSVCONTEXT));
77  	}
78  	
79  	/**
80  	 * Tests an input above the max.
81  	 */
82  	@Test(expected = SuperCsvConstraintViolationException.class)
83  	public void testAboveMax() {
84  		processor.execute(11, ANONYMOUS_CSVCONTEXT);
85  	}
86  	
87  	/**
88  	 * Tests an input below the min.
89  	 */
90  	@Test(expected = SuperCsvConstraintViolationException.class)
91  	public void testBelowMin() {
92  		processor.execute(-6, ANONYMOUS_CSVCONTEXT);
93  	}
94  	
95  	/**
96  	 * Tests execution with invalid min/max values (should throw an Exception).
97  	 */
98  	@Test(expected = IllegalArgumentException.class)
99  	public void testWithMaxLessThanMin() {
100 		new DMinMax(2, 1);
101 	}
102 	
103 	/**
104 	 * Tests execution with a null input (should throw an Exception).
105 	 */
106 	@Test(expected = SuperCsvCellProcessorException.class)
107 	public void testWithNull() {
108 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
109 	}
110 	
111 	/**
112 	 * Tests execution with a non-Number input (should throw an Exception).
113 	 */
114 	@Test(expected = SuperCsvCellProcessorException.class)
115 	public void testWithNonNumber() {
116 		processor.execute("abc", ANONYMOUS_CSVCONTEXT);
117 	}
118 }