1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator.routines;
18
19 import junit.framework.TestCase;
20
21 import java.util.Locale;
22 import java.math.BigDecimal;
23 import java.text.DecimalFormatSymbols;
24
25 /***
26 * Test Case for CurrencyValidator.
27 *
28 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
29 */
30 public class CurrencyValidatorTest extends TestCase {
31
32 private static final char CURRENCY_SYMBOL = '\u00A4';
33
34 private String US_DOLLAR;
35 private String UK_POUND;
36
37 /***
38 * Main
39 * @param args arguments
40 */
41 public static void main(String[] args) {
42 junit.textui.TestRunner.run(CurrencyValidatorTest.class);
43 }
44
45 /***
46 * Constructor
47 * @param name test name
48 */
49 public CurrencyValidatorTest(String name) {
50 super(name);
51 }
52
53 protected void setUp() throws Exception {
54 super.setUp();
55 US_DOLLAR = (new DecimalFormatSymbols(Locale.US)).getCurrencySymbol();
56 UK_POUND = (new DecimalFormatSymbols(Locale.UK)).getCurrencySymbol();
57 }
58
59 /***
60 * Tear down
61 * @throws Exception
62 */
63 protected void tearDown() throws Exception {
64 super.tearDown();
65 }
66
67 /***
68 * Test Format Type
69 */
70 public void testFormatType() {
71 assertEquals("Format Type A", 1, CurrencyValidator.getInstance().getFormatType());
72 assertEquals("Format Type B", CurrencyValidator.CURRENCY_FORMAT, CurrencyValidator.getInstance().getFormatType());
73 }
74
75 /***
76 * Test Valid currency values
77 */
78 public void testValid() {
79
80 Locale origDefault = Locale.getDefault();
81 Locale.setDefault(Locale.UK);
82
83 BigDecimalValidator validator = CurrencyValidator.getInstance();
84 BigDecimal expected = new BigDecimal("1234.56");
85 BigDecimal negative = new BigDecimal("-1234.56");
86 BigDecimal noDecimal = new BigDecimal("1234.00");
87 BigDecimal oneDecimal = new BigDecimal("1234.50");
88
89 assertEquals("Default locale", expected, validator.validate(UK_POUND + "1,234.56"));
90
91 assertEquals("UK locale", expected, validator.validate(UK_POUND + "1,234.56", Locale.UK));
92 assertEquals("UK negative", negative, validator.validate("-" + UK_POUND + "1,234.56", Locale.UK));
93 assertEquals("UK no decimal", noDecimal, validator.validate(UK_POUND + "1,234", Locale.UK));
94 assertEquals("UK 1 decimal", oneDecimal, validator.validate(UK_POUND + "1,234.5", Locale.UK));
95 assertEquals("UK 3 decimal", expected, validator.validate(UK_POUND + "1,234.567", Locale.UK));
96 assertEquals("UK no symbol", expected, validator.validate("1,234.56", Locale.UK));
97
98 assertEquals("US locale", expected, validator.validate(US_DOLLAR + "1,234.56", Locale.US));
99 assertEquals("US negative", negative, validator.validate("(" + US_DOLLAR + "1,234.56)", Locale.US));
100 assertEquals("US no decimal", noDecimal, validator.validate(US_DOLLAR + "1,234", Locale.US));
101 assertEquals("US 1 decimal", oneDecimal, validator.validate(US_DOLLAR + "1,234.5", Locale.US));
102 assertEquals("US 3 decimal", expected, validator.validate(US_DOLLAR + "1,234.567", Locale.US));
103 assertEquals("US no symbol", expected, validator.validate("1,234.56", Locale.US));
104
105
106 Locale.setDefault(origDefault);
107 }
108
109 /***
110 * Test Invalid currency values
111 */
112 public void testInvalid() {
113 BigDecimalValidator validator = CurrencyValidator.getInstance();
114
115
116 assertFalse("isValid() Null Value", validator.isValid(null));
117 assertFalse("isValid() Empty Value", validator.isValid(""));
118 assertNull("validate() Null Value", validator.validate(null));
119 assertNull("validate() Empty Value", validator.validate(""));
120
121
122 assertFalse("UK wrong symbol", validator.isValid(US_DOLLAR + "1,234.56", Locale.UK));
123 assertFalse("UK wrong negative", validator.isValid("(" + UK_POUND + "1,234.56)", Locale.UK));
124
125
126 assertFalse("US wrong symbol", validator.isValid(UK_POUND + "1,234.56", Locale.US));
127 assertFalse("US wrong negative", validator.isValid("-" + US_DOLLAR + "1,234.56", Locale.US));
128 }
129
130 /***
131 * Test Valid integer (non-decimal) currency values
132 */
133 public void testIntegerValid() {
134
135 Locale origDefault = Locale.getDefault();
136 Locale.setDefault(Locale.UK);
137
138 CurrencyValidator validator = new CurrencyValidator();
139 BigDecimal expected = new BigDecimal("1234.00");
140 BigDecimal negative = new BigDecimal("-1234.00");
141
142 assertEquals("Default locale", expected, validator.validate(UK_POUND +"1,234"));
143
144 assertEquals("UK locale", expected, validator.validate(UK_POUND + "1,234", Locale.UK));
145 assertEquals("UK negative", negative, validator.validate("-" + UK_POUND + "1,234", Locale.UK));
146
147 assertEquals("US locale", expected, validator.validate(US_DOLLAR + "1,234", Locale.US));
148 assertEquals("US negative", negative, validator.validate("(" + US_DOLLAR + "1,234)", Locale.US));
149
150
151 Locale.setDefault(origDefault);
152 }
153
154 /***
155 * Test Invalid integer (non decimal) currency values
156 */
157 public void testIntegerInvalid() {
158 CurrencyValidator validator = new CurrencyValidator(true, false);
159
160
161 assertFalse("UK positive", validator.isValid(UK_POUND + "1,234.56", Locale.UK));
162 assertFalse("UK negative", validator.isValid("-" + UK_POUND + "1,234.56", Locale.UK));
163
164
165 assertFalse("US positive", validator.isValid(US_DOLLAR + "1,234.56", Locale.US));
166 assertFalse("US negative", validator.isValid("(" + US_DOLLAR + "1,234.56)", Locale.US));
167 }
168
169
170 /***
171 * Test currency values with a pattern
172 */
173 public void testPattern() {
174
175 Locale origDefault = Locale.getDefault();
176 Locale.setDefault(Locale.UK);
177
178 BigDecimalValidator validator = CurrencyValidator.getInstance();
179 String basicPattern = CURRENCY_SYMBOL + "#,##0.000";
180 String pattern = basicPattern + ";[" + basicPattern +"]";
181 BigDecimal expected = new BigDecimal("1234.567");
182 BigDecimal negative = new BigDecimal("-1234.567");
183
184
185 assertEquals("default", expected, validator.validate(UK_POUND + "1,234.567", pattern));
186 assertEquals("negative", negative, validator.validate("[" + UK_POUND + "1,234.567]", pattern));
187 assertEquals("no symbol +ve", expected, validator.validate("1,234.567", pattern));
188 assertEquals("no symbol -ve", negative, validator.validate("[1,234.567]", pattern));
189
190
191 assertEquals("default", expected, validator.validate(US_DOLLAR + "1,234.567", pattern, Locale.US));
192 assertEquals("negative", negative, validator.validate("[" + US_DOLLAR + "1,234.567]", pattern, Locale.US));
193 assertEquals("no symbol +ve", expected, validator.validate("1,234.567", pattern, Locale.US));
194 assertEquals("no symbol -ve", negative, validator.validate("[1,234.567]", pattern, Locale.US));
195
196
197 assertFalse("invalid symbol", validator.isValid(US_DOLLAR + "1,234.567", pattern));
198 assertFalse("invalid symbol", validator.isValid(UK_POUND + "1,234.567", pattern, Locale.US));
199
200
201 Locale.setDefault(origDefault);
202 }
203 }