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