001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator.routines;
018:
019: import junit.framework.TestCase;
020:
021: import java.util.Locale;
022: import java.math.BigDecimal;
023: import java.text.DecimalFormatSymbols;
024:
025: /**
026: * Test Case for CurrencyValidator.
027: *
028: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
029: */
030: public class CurrencyValidatorTest extends TestCase {
031:
032: private static final char CURRENCY_SYMBOL = '\u00A4';
033:
034: private String US_DOLLAR;
035: private String UK_POUND;
036:
037: /**
038: * Main
039: * @param args arguments
040: */
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(CurrencyValidatorTest.class);
043: }
044:
045: /**
046: * Constructor
047: * @param name test name
048: */
049: public CurrencyValidatorTest(String name) {
050: super (name);
051: }
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: US_DOLLAR = (new DecimalFormatSymbols(Locale.US))
056: .getCurrencySymbol();
057: UK_POUND = (new DecimalFormatSymbols(Locale.UK))
058: .getCurrencySymbol();
059: }
060:
061: /**
062: * Tear down
063: * @throws Exception
064: */
065: protected void tearDown() throws Exception {
066: super .tearDown();
067: }
068:
069: /**
070: * Test Format Type
071: */
072: public void testFormatType() {
073: assertEquals("Format Type A", 1, CurrencyValidator
074: .getInstance().getFormatType());
075: assertEquals("Format Type B",
076: CurrencyValidator.CURRENCY_FORMAT, CurrencyValidator
077: .getInstance().getFormatType());
078: }
079:
080: /**
081: * Test Valid currency values
082: */
083: public void testValid() {
084: // Set the default Locale
085: Locale origDefault = Locale.getDefault();
086: Locale.setDefault(Locale.UK);
087:
088: BigDecimalValidator validator = CurrencyValidator.getInstance();
089: BigDecimal expected = new BigDecimal("1234.56");
090: BigDecimal negative = new BigDecimal("-1234.56");
091: BigDecimal noDecimal = new BigDecimal("1234.00");
092: BigDecimal oneDecimal = new BigDecimal("1234.50");
093:
094: assertEquals("Default locale", expected, validator
095: .validate(UK_POUND + "1,234.56"));
096:
097: assertEquals("UK locale", expected, validator.validate(UK_POUND
098: + "1,234.56", Locale.UK));
099: assertEquals("UK negative", negative, validator.validate("-"
100: + UK_POUND + "1,234.56", Locale.UK));
101: assertEquals("UK no decimal", noDecimal, validator.validate(
102: UK_POUND + "1,234", Locale.UK));
103: assertEquals("UK 1 decimal", oneDecimal, validator.validate(
104: UK_POUND + "1,234.5", Locale.UK));
105: assertEquals("UK 3 decimal", expected, validator.validate(
106: UK_POUND + "1,234.567", Locale.UK));
107: assertEquals("UK no symbol", expected, validator.validate(
108: "1,234.56", Locale.UK));
109:
110: assertEquals("US locale", expected, validator.validate(
111: US_DOLLAR + "1,234.56", Locale.US));
112: assertEquals("US negative", negative, validator.validate("("
113: + US_DOLLAR + "1,234.56)", Locale.US));
114: assertEquals("US no decimal", noDecimal, validator.validate(
115: US_DOLLAR + "1,234", Locale.US));
116: assertEquals("US 1 decimal", oneDecimal, validator.validate(
117: US_DOLLAR + "1,234.5", Locale.US));
118: assertEquals("US 3 decimal", expected, validator.validate(
119: US_DOLLAR + "1,234.567", Locale.US));
120: assertEquals("US no symbol", expected, validator.validate(
121: "1,234.56", Locale.US));
122:
123: // Restore the original default
124: Locale.setDefault(origDefault);
125: }
126:
127: /**
128: * Test Invalid currency values
129: */
130: public void testInvalid() {
131: BigDecimalValidator validator = CurrencyValidator.getInstance();
132:
133: // Invalid Missing
134: assertFalse("isValid() Null Value", validator.isValid(null));
135: assertFalse("isValid() Empty Value", validator.isValid(""));
136: assertNull("validate() Null Value", validator.validate(null));
137: assertNull("validate() Empty Value", validator.validate(""));
138:
139: // Invalid UK
140: assertFalse("UK wrong symbol", validator.isValid(US_DOLLAR
141: + "1,234.56", Locale.UK));
142: assertFalse("UK wrong negative", validator.isValid("("
143: + UK_POUND + "1,234.56)", Locale.UK));
144:
145: // Invalid US
146: assertFalse("US wrong symbol", validator.isValid(UK_POUND
147: + "1,234.56", Locale.US));
148: assertFalse("US wrong negative", validator.isValid("-"
149: + US_DOLLAR + "1,234.56", Locale.US));
150: }
151:
152: /**
153: * Test Valid integer (non-decimal) currency values
154: */
155: public void testIntegerValid() {
156: // Set the default Locale
157: Locale origDefault = Locale.getDefault();
158: Locale.setDefault(Locale.UK);
159:
160: CurrencyValidator validator = new CurrencyValidator();
161: BigDecimal expected = new BigDecimal("1234.00");
162: BigDecimal negative = new BigDecimal("-1234.00");
163:
164: assertEquals("Default locale", expected, validator
165: .validate(UK_POUND + "1,234"));
166:
167: assertEquals("UK locale", expected, validator.validate(UK_POUND
168: + "1,234", Locale.UK));
169: assertEquals("UK negative", negative, validator.validate("-"
170: + UK_POUND + "1,234", Locale.UK));
171:
172: assertEquals("US locale", expected, validator.validate(
173: US_DOLLAR + "1,234", Locale.US));
174: assertEquals("US negative", negative, validator.validate("("
175: + US_DOLLAR + "1,234)", Locale.US));
176:
177: // Restore the original default
178: Locale.setDefault(origDefault);
179: }
180:
181: /**
182: * Test Invalid integer (non decimal) currency values
183: */
184: public void testIntegerInvalid() {
185: CurrencyValidator validator = new CurrencyValidator(true, false);
186:
187: // Invalid UK - has decimals
188: assertFalse("UK positive", validator.isValid(UK_POUND
189: + "1,234.56", Locale.UK));
190: assertFalse("UK negative", validator.isValid("-" + UK_POUND
191: + "1,234.56", Locale.UK));
192:
193: // Invalid US - has decimals
194: assertFalse("US positive", validator.isValid(US_DOLLAR
195: + "1,234.56", Locale.US));
196: assertFalse("US negative", validator.isValid("(" + US_DOLLAR
197: + "1,234.56)", Locale.US));
198: }
199:
200: /**
201: * Test currency values with a pattern
202: */
203: public void testPattern() {
204: // Set the default Locale
205: Locale origDefault = Locale.getDefault();
206: Locale.setDefault(Locale.UK);
207:
208: BigDecimalValidator validator = CurrencyValidator.getInstance();
209: String basicPattern = CURRENCY_SYMBOL + "#,##0.000";
210: String pattern = basicPattern + ";[" + basicPattern + "]";
211: BigDecimal expected = new BigDecimal("1234.567");
212: BigDecimal negative = new BigDecimal("-1234.567");
213:
214: // Test Pattern
215: assertEquals("default", expected, validator.validate(UK_POUND
216: + "1,234.567", pattern));
217: assertEquals("negative", negative, validator.validate("["
218: + UK_POUND + "1,234.567]", pattern));
219: assertEquals("no symbol +ve", expected, validator.validate(
220: "1,234.567", pattern));
221: assertEquals("no symbol -ve", negative, validator.validate(
222: "[1,234.567]", pattern));
223:
224: // Test Pattern & Locale
225: assertEquals("default", expected, validator.validate(US_DOLLAR
226: + "1,234.567", pattern, Locale.US));
227: assertEquals("negative", negative, validator.validate("["
228: + US_DOLLAR + "1,234.567]", pattern, Locale.US));
229: assertEquals("no symbol +ve", expected, validator.validate(
230: "1,234.567", pattern, Locale.US));
231: assertEquals("no symbol -ve", negative, validator.validate(
232: "[1,234.567]", pattern, Locale.US));
233:
234: // invalid
235: assertFalse("invalid symbol", validator.isValid(US_DOLLAR
236: + "1,234.567", pattern));
237: assertFalse("invalid symbol", validator.isValid(UK_POUND
238: + "1,234.567", pattern, Locale.US));
239:
240: // Restore the original default
241: Locale.setDefault(origDefault);
242: }
243: }
|