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