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 java.text.DecimalFormat;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.supercsv.cellprocessor.ift.CellProcessor;
26  import org.supercsv.exception.SuperCsvCellProcessorException;
27  import org.supercsv.mock.IdentityTransform;
28  
29  /**
30   * Tests the FmtNumber processor. As FmtNumber uses the default locale, this test must be written in a locale
31   * independent way (so the test will pass). The test can be run in a different local by specifying the
32   * <tt>user.language</tt> and <tt>user.country</tt> JVM properties (i.e. <tt>-Duser.language=de -Duser.country=DE</tt>).
33   * 
34   * @author Kasper B. Graversen
35   * @author James Bassett
36   */
37  public class FmtNumberTest {
38  	
39  	private static final String DECIMAL_FORMAT = "00.00";
40  	
41  	// locale-independent
42  	private static final String FORMATTED_NUMBER = new DecimalFormat(DECIMAL_FORMAT).format(12.34);
43  	
44  	private CellProcessor processor;
45  	private CellProcessor processor2;
46  	private CellProcessor processorChain;
47  	private CellProcessor processorChain2;
48  	
49  	/**
50  	 * Sets up the processors for the test using all constructor combinations.
51  	 */
52  	@Before
53  	public void setUp() {
54  		processor = new FmtNumber(DECIMAL_FORMAT);
55  		processor2 = new FmtNumber(new DecimalFormat(DECIMAL_FORMAT));
56  		processorChain = new FmtNumber(DECIMAL_FORMAT, new IdentityTransform());
57  		processorChain2 = new FmtNumber(new DecimalFormat(DECIMAL_FORMAT), new IdentityTransform());
58  	}
59  	
60  	/**
61  	 * Tests unchained/chained execution.
62  	 */
63  	@Test
64  	public void testFormat() {
65  		final double number = 12.34;
66  		assertEquals(FORMATTED_NUMBER, processor.execute(number, ANONYMOUS_CSVCONTEXT));
67  		assertEquals(FORMATTED_NUMBER, processor2.execute(number, ANONYMOUS_CSVCONTEXT));
68  		assertEquals(FORMATTED_NUMBER, processorChain.execute(number, ANONYMOUS_CSVCONTEXT));
69  		assertEquals(FORMATTED_NUMBER, processorChain2.execute(number, ANONYMOUS_CSVCONTEXT));
70  	}
71  	
72  	/**
73  	 * Tests unchained/chained execution with an input that should round up.
74  	 */
75  	@Test
76  	public void testRoundUp() {
77  		final double toRoundUp = 12.339;
78  		assertEquals(FORMATTED_NUMBER, processor.execute(toRoundUp, ANONYMOUS_CSVCONTEXT));
79  		assertEquals(FORMATTED_NUMBER, processor2.execute(toRoundUp, ANONYMOUS_CSVCONTEXT));
80  		assertEquals(FORMATTED_NUMBER, processorChain.execute(toRoundUp, ANONYMOUS_CSVCONTEXT));
81  		assertEquals(FORMATTED_NUMBER, processorChain2.execute(toRoundUp, ANONYMOUS_CSVCONTEXT));
82  	}
83  	
84  	/**
85  	 * Tests unchained/chained execution with an input that should round down.
86  	 */
87  	@Test
88  	public void testRoundDown() {
89  		final double toRoundDown = 12.341;
90  		assertEquals(FORMATTED_NUMBER, processor.execute(toRoundDown, ANONYMOUS_CSVCONTEXT));
91  		assertEquals(FORMATTED_NUMBER, processor2.execute(toRoundDown, ANONYMOUS_CSVCONTEXT));
92  		assertEquals(FORMATTED_NUMBER, processorChain.execute(toRoundDown, ANONYMOUS_CSVCONTEXT));
93  		assertEquals(FORMATTED_NUMBER, processorChain2.execute(toRoundDown, 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 	/**
105 	 * Tests execution with a non-Number input (should throw an Exception).
106 	 */
107 	@Test(expected = SuperCsvCellProcessorException.class)
108 	public void testWithNonNumber() {
109 		processor.execute("abc", ANONYMOUS_CSVCONTEXT);
110 	}
111 	
112 	/**
113 	 * Tests execution with a invalid number format (should throw an Exception).
114 	 */
115 	@Test(expected = SuperCsvCellProcessorException.class)
116 	public void testWithInvalidNumberFormat() {
117 		final double number = 12.34;
118 		CellProcessor invalidNumberFormatProcessor = new FmtNumber("%%%");
119 		invalidNumberFormatProcessor.execute(number, ANONYMOUS_CSVCONTEXT);
120 	}
121 	
122 	/**
123 	 * Tests execution with a invalid number format String (should throw an Exception).
124 	 */
125 	@Test(expected = NullPointerException.class)
126 	public void testWithNullNumberFormatString() {
127 		final double number = 12.34;
128 		CellProcessor invalidNumberFormatProcessor = new FmtNumber((String) null);
129 		invalidNumberFormatProcessor.execute(number, ANONYMOUS_CSVCONTEXT);
130 	}
131 	
132 	/**
133 	 * Tests execution with a invalid number format object (should throw an Exception).
134 	 */
135 	@Test(expected = NullPointerException.class)
136 	public void testWithNullNumberFormat() {
137 		final double number = 12.34;
138 		CellProcessor invalidNumberFormatProcessor = new FmtNumber((DecimalFormat) null);
139 		invalidNumberFormatProcessor.execute(number, ANONYMOUS_CSVCONTEXT);
140 	}
141 	
142 }