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.fail;
19  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  import static org.supercsv.SuperCsvTestUtils.assertExecution;
21  
22  import java.math.BigDecimal;
23  import java.text.DecimalFormatSymbols;
24  import java.util.Arrays;
25  import java.util.Locale;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.supercsv.cellprocessor.ift.CellProcessor;
30  import org.supercsv.exception.SuperCsvCellProcessorException;
31  import org.supercsv.mock.IdentityTransform;
32  
33  /**
34   * Tests the ParseBigDecimal processor.
35   * 
36   * @author Kasper B. Graversen
37   * @author James Bassett
38   */
39  public class ParseBigDecimalTest {
40  	
41  	private static final DecimalFormatSymbols ENGLISH_SYMBOLS = new DecimalFormatSymbols(Locale.ENGLISH);
42  	private static final DecimalFormatSymbols GERMAN_SYMBOLS = new DecimalFormatSymbols(Locale.GERMANY);
43  	
44  	private CellProcessor processor;
45  	private CellProcessor processor2;
46  	private CellProcessor processor3;
47  	private CellProcessor processorChain;
48  	private CellProcessor processorChain2;
49  	private CellProcessor processorChain3;
50  	
51  	/**
52  	 * Sets up the processors for the test using all constructor combinations.
53  	 */
54  	@Before
55  	public void setUp() {
56  		processor = new ParseBigDecimal();
57  		processor2 = new ParseBigDecimal(ENGLISH_SYMBOLS);
58  		processor3 = new ParseBigDecimal(GERMAN_SYMBOLS);
59  		processorChain = new ParseBigDecimal(new IdentityTransform());
60  		processorChain2 = new ParseBigDecimal(ENGLISH_SYMBOLS, new IdentityTransform());
61  		processorChain3 = new ParseBigDecimal(GERMAN_SYMBOLS, new IdentityTransform());
62  	}
63  	
64  	/**
65  	 * Test unchained/chained execution with a valid positive input.
66  	 */
67  	@Test
68  	public void testValidInput() {
69  		
70  		String normalInput = "1357.459";
71  		String germanInput = "1357,459";
72  		BigDecimal expectedOutput = new BigDecimal(normalInput);
73  		
74  		// normal input
75  		for( final CellProcessor p : Arrays.asList(processor, processor2, processorChain, processorChain2) ) {
76  			assertExecution(p, normalInput, expectedOutput);
77  		}
78  		
79  		// german input ("," instead of "." as decimal symbol)
80  		for( final CellProcessor p : Arrays.asList(processor3, processorChain3) ) {
81  			assertExecution(p, germanInput, expectedOutput);
82  		}
83  	}
84  	
85  	/**
86  	 * Test unchained/chained execution with a valid negative input.
87  	 */
88  	@Test
89  	public void testValidNegativeInput() {
90  		
91  		String normalInput = "-1357.459";
92  		String germanInput = "-1357,459";
93  		BigDecimal expectedOutput = new BigDecimal(normalInput);
94  		
95  		// normal input
96  		for( final CellProcessor p : Arrays.asList(processor, processor2, processorChain, processorChain2) ) {
97  			assertExecution(p, normalInput, expectedOutput);
98  		}
99  		
100 		// german input ("," instead of "." as decimal symbol)
101 		for( final CellProcessor p : Arrays.asList(processor3, processorChain3) ) {
102 			assertExecution(p, germanInput, expectedOutput);
103 		}
104 	}
105 	
106 	/**
107 	 * Tests that grouping separators are handled correctly for positive numbers.
108 	 */
109 	@Test
110 	public void testValidInputWithGroupingSeparator() {
111 		String normalInput = "1,357.459";
112 		String germanInput = "1.357,459";
113 		BigDecimal expectedOutput = new BigDecimal("1357.459");
114 		
115 		// 'no symbols' processors should choke on grouping separators
116 		for( CellProcessor p : Arrays.asList(processor, processorChain) ) {
117 			try {
118 				p.execute(normalInput, ANONYMOUS_CSVCONTEXT);
119 				fail("should have thrown SuperCsvCellProcessorException");
120 			}
121 			catch(SuperCsvCellProcessorException e) {}
122 		}
123 		
124 		// normal input
125 		for( final CellProcessor p : Arrays.asList(processor2, processorChain2) ) {
126 			assertExecution(p, normalInput, expectedOutput);
127 		}
128 		
129 		// german input (opposite - "," is decimal symbol and "." is grouping)
130 		for( final CellProcessor p : Arrays.asList(processor3, processorChain3) ) {
131 			assertExecution(p, germanInput, expectedOutput);
132 		}
133 	}
134 	
135 	/**
136 	 * Tests that grouping separators are handled correctly for negative numbers.
137 	 */
138 	@Test
139 	public void testValidNegativeInputWithGroupingSeparator() {
140 		String normalInput = "-1,357.459";
141 		String germanInput = "-1.357,459";
142 		BigDecimal expectedOutput = new BigDecimal("-1357.459");
143 		
144 		// 'no symbols' processors should choke on grouping separators
145 		for( CellProcessor p : Arrays.asList(processor, processorChain) ) {
146 			try {
147 				p.execute(normalInput, ANONYMOUS_CSVCONTEXT);
148 				fail("should have thrown SuperCsvCellProcessorException");
149 			}
150 			catch(SuperCsvCellProcessorException e) {}
151 		}
152 		
153 		// normal input
154 		for( final CellProcessor p : Arrays.asList(processor2, processorChain2) ) {
155 			assertExecution(p, normalInput, expectedOutput);
156 		}
157 		
158 		// german input (opposite - "," is decimal symbol and "." is grouping)
159 		for( final CellProcessor p : Arrays.asList(processor3, processorChain3) ) {
160 			assertExecution(p, germanInput, expectedOutput);
161 		}
162 	}
163 	
164 	/**
165 	 * Tests execution with a non-String input (should throw an exception).
166 	 */
167 	@Test(expected = SuperCsvCellProcessorException.class)
168 	public void testWithNonString() {
169 		processor.execute(1234, ANONYMOUS_CSVCONTEXT);
170 	}
171 	
172 	/**
173 	 * Tests execution with a null input (should throw an Exception).
174 	 */
175 	@Test(expected = SuperCsvCellProcessorException.class)
176 	public void testWithNull() {
177 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
178 	}
179 	
180 	/**
181 	 * Tests execution with an empty-String input (should throw an Exception).
182 	 */
183 	@Test(expected = SuperCsvCellProcessorException.class)
184 	public void testWithEmptyString() {
185 		processor.execute("", ANONYMOUS_CSVCONTEXT);
186 	}
187 	
188 	/**
189 	 * Tests construction with null symbols (should throw an Exception).
190 	 */
191 	@Test(expected = NullPointerException.class)
192 	public void testWithNullSymbols() {
193 		new ParseBigDecimal((DecimalFormatSymbols) null);
194 	}
195 	
196 }