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;
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.mock.IdentityTransform;
26  
27  /**
28   * Tests the ParseInt processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class ParseIntTest {
34  	
35  	private static final int POSITIVE_VAL = 17;
36  	private static final String POSITIVE_STRING = "17";
37  	private static final int NEGATIVE_VAL = -43;
38  	private static final String NEGATIVE_STRING = "-43";
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 ParseInt();
49  		processorChain = new ParseInt(new IdentityTransform());
50  	}
51  	
52  	/**
53  	 * Tests unchained/chained execution with valid ints as input.
54  	 */
55  	@Test
56  	public void testValidInts() {
57  		// positive values
58  		assertEquals(POSITIVE_VAL, processor.execute(POSITIVE_VAL, ANONYMOUS_CSVCONTEXT));
59  		assertEquals(POSITIVE_VAL, processorChain.execute(POSITIVE_VAL, ANONYMOUS_CSVCONTEXT));
60  		
61  		// negative values
62  		assertEquals(NEGATIVE_VAL, processor.execute(NEGATIVE_VAL, ANONYMOUS_CSVCONTEXT));
63  		assertEquals(NEGATIVE_VAL, processorChain.execute(NEGATIVE_VAL, ANONYMOUS_CSVCONTEXT));
64  	}
65  	
66  	/**
67  	 * Tests unchained/chained execution with valid int Strings as input.
68  	 */
69  	@Test
70  	public void testValidIntStrings() {
71  		// positive values
72  		assertEquals(POSITIVE_VAL, processor.execute(POSITIVE_STRING, ANONYMOUS_CSVCONTEXT));
73  		assertEquals(POSITIVE_VAL, processorChain.execute(POSITIVE_STRING, ANONYMOUS_CSVCONTEXT));
74  		
75  		// negative values
76  		assertEquals(NEGATIVE_VAL, processor.execute(NEGATIVE_STRING, ANONYMOUS_CSVCONTEXT));
77  		assertEquals(NEGATIVE_VAL, processorChain.execute(NEGATIVE_STRING, ANONYMOUS_CSVCONTEXT));
78  	}
79  	
80  	/**
81  	 * Tests execution with an badly formatted String input (should throw an exception).
82  	 */
83  	@Test(expected = SuperCsvCellProcessorException.class)
84  	public void testWithInvalidFormatString() {
85  		processor.execute("123.45", ANONYMOUS_CSVCONTEXT);
86  	}
87  	
88  	/**
89  	 * Tests execution with a non Integer input (should throw an exception).
90  	 */
91  	@Test(expected = SuperCsvCellProcessorException.class)
92  	public void testWithNonIntInput() {
93  		processor.execute(1.23, ANONYMOUS_CSVCONTEXT);
94  	}
95  	
96  	/**
97  	 * Tests execution with a null input (should throw an Exception).
98  	 */
99  	@Test(expected = SuperCsvCellProcessorException.class)
100 	public void testWithNull() {
101 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
102 	}
103 	
104 }