View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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: 1094751 $ $Date: 2011-04-18 17:17:40 -0400 (Mon, 18 Apr 2011) $
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       * Constructor
39       * @param name test name
40       */
41      public CurrencyValidatorTest(String name) {
42          super(name);
43      }
44  
45      protected void setUp() throws Exception {
46          super.setUp();
47          US_DOLLAR = (new DecimalFormatSymbols(Locale.US)).getCurrencySymbol();
48          UK_POUND  = (new DecimalFormatSymbols(Locale.UK)).getCurrencySymbol();
49      }
50  
51      /**
52       * Tear down
53       * @throws Exception
54       */
55      protected void tearDown() throws Exception {
56          super.tearDown();
57      }
58  
59      /**
60       * Test Format Type
61       */
62      public void testFormatType() {
63          assertEquals("Format Type A", 1, CurrencyValidator.getInstance().getFormatType());
64          assertEquals("Format Type B", CurrencyValidator.CURRENCY_FORMAT, CurrencyValidator.getInstance().getFormatType());
65      }
66  
67      /**
68       * Test Valid currency values
69       */
70      public void testValid() {
71          // Set the default Locale
72          Locale origDefault = Locale.getDefault();
73          Locale.setDefault(Locale.UK);
74  
75          BigDecimalValidator validator = CurrencyValidator.getInstance();
76          BigDecimal expected   = new BigDecimal("1234.56");
77          BigDecimal negative   = new BigDecimal("-1234.56");
78          BigDecimal noDecimal  = new BigDecimal("1234.00");
79          BigDecimal oneDecimal = new BigDecimal("1234.50");
80  
81          assertEquals("Default locale", expected, validator.validate(UK_POUND + "1,234.56"));
82  
83          assertEquals("UK locale",     expected,   validator.validate(UK_POUND  + "1,234.56",   Locale.UK));
84          assertEquals("UK negative",   negative,   validator.validate("-" + UK_POUND  + "1,234.56",  Locale.UK));
85          assertEquals("UK no decimal", noDecimal,  validator.validate(UK_POUND  + "1,234",      Locale.UK));
86          assertEquals("UK 1 decimal",  oneDecimal, validator.validate(UK_POUND  + "1,234.5",    Locale.UK));
87          assertEquals("UK 3 decimal",  expected,   validator.validate(UK_POUND  + "1,234.567",  Locale.UK));
88          assertEquals("UK no symbol",  expected,   validator.validate("1,234.56",    Locale.UK));
89  
90          assertEquals("US locale",     expected,   validator.validate(US_DOLLAR + "1,234.56",   Locale.US));
91          assertEquals("US negative",   negative,   validator.validate("(" + US_DOLLAR + "1,234.56)", Locale.US));
92          assertEquals("US no decimal", noDecimal,  validator.validate(US_DOLLAR + "1,234",      Locale.US));
93          assertEquals("US 1 decimal",  oneDecimal, validator.validate(US_DOLLAR + "1,234.5",    Locale.US));
94          assertEquals("US 3 decimal",  expected,   validator.validate(US_DOLLAR + "1,234.567",  Locale.US));
95          assertEquals("US no symbol",  expected,   validator.validate("1,234.56",    Locale.US));
96  
97          // Restore the original default
98          Locale.setDefault(origDefault);
99      }
100 
101     /**
102      * Test Invalid currency values
103      */
104     public void testInvalid() {
105         BigDecimalValidator validator = CurrencyValidator.getInstance();
106 
107         // Invalid Missing
108         assertFalse("isValid() Null Value",    validator.isValid(null));
109         assertFalse("isValid() Empty Value",   validator.isValid(""));
110         assertNull("validate() Null Value",    validator.validate(null));
111         assertNull("validate() Empty Value",   validator.validate(""));
112 
113         // Invalid UK
114         assertFalse("UK wrong symbol",    validator.isValid(US_DOLLAR + "1,234.56",   Locale.UK));
115         assertFalse("UK wrong negative",  validator.isValid("(" + UK_POUND  + "1,234.56)", Locale.UK));
116 
117         // Invalid US
118         assertFalse("US wrong symbol",    validator.isValid(UK_POUND + "1,234.56",   Locale.US));
119         assertFalse("US wrong negative",  validator.isValid("-" + US_DOLLAR + "1,234.56",  Locale.US));
120     }
121 
122     /**
123      * Test Valid integer (non-decimal) currency values
124      */
125     public void testIntegerValid() {
126         // Set the default Locale
127         Locale origDefault = Locale.getDefault();
128         Locale.setDefault(Locale.UK);
129 
130         CurrencyValidator validator = new CurrencyValidator();
131         BigDecimal expected = new BigDecimal("1234.00");
132         BigDecimal negative = new BigDecimal("-1234.00");
133 
134         assertEquals("Default locale", expected, validator.validate(UK_POUND +"1,234"));
135 
136         assertEquals("UK locale",      expected, validator.validate(UK_POUND + "1,234",   Locale.UK));
137         assertEquals("UK negative",    negative, validator.validate("-" + UK_POUND + "1,234",  Locale.UK));
138 
139         assertEquals("US locale",      expected, validator.validate(US_DOLLAR + "1,234",   Locale.US));
140         assertEquals("US negative",    negative, validator.validate("(" + US_DOLLAR + "1,234)", Locale.US));
141 
142         // Restore the original default
143         Locale.setDefault(origDefault);
144     }
145 
146     /**
147      * Test Invalid integer (non decimal) currency values
148      */
149     public void testIntegerInvalid() {
150         CurrencyValidator validator = new CurrencyValidator(true, false);
151 
152         // Invalid UK - has decimals
153         assertFalse("UK positive",    validator.isValid(UK_POUND + "1,234.56",   Locale.UK));
154         assertFalse("UK negative",    validator.isValid("-" + UK_POUND + "1,234.56", Locale.UK));
155 
156         // Invalid US - has decimals
157         assertFalse("US positive",    validator.isValid(US_DOLLAR + "1,234.56",   Locale.US));
158         assertFalse("US negative",    validator.isValid("(" + US_DOLLAR + "1,234.56)",  Locale.US));
159     }
160 
161 
162     /**
163      * Test currency values with a pattern
164      */
165     public void testPattern() {
166         // Set the default Locale
167         Locale origDefault = Locale.getDefault();
168         Locale.setDefault(Locale.UK);
169 
170         BigDecimalValidator validator = CurrencyValidator.getInstance();
171         String basicPattern = CURRENCY_SYMBOL + "#,##0.000";
172         String pattern = basicPattern + ";[" + basicPattern +"]";
173         BigDecimal expected   = new BigDecimal("1234.567");
174         BigDecimal negative   = new BigDecimal("-1234.567");
175 
176         // Test Pattern
177         assertEquals("default",        expected,   validator.validate(UK_POUND + "1,234.567", pattern));
178         assertEquals("negative",       negative,   validator.validate("[" + UK_POUND + "1,234.567]", pattern));
179         assertEquals("no symbol +ve",  expected,   validator.validate("1,234.567",    pattern));
180         assertEquals("no symbol -ve",  negative,   validator.validate("[1,234.567]",  pattern));
181 
182         // Test Pattern & Locale
183         assertEquals("default",        expected,   validator.validate(US_DOLLAR + "1,234.567", pattern, Locale.US));
184         assertEquals("negative",       negative,   validator.validate("[" + US_DOLLAR + "1,234.567]", pattern, Locale.US));
185         assertEquals("no symbol +ve",  expected,   validator.validate("1,234.567",    pattern, Locale.US));
186         assertEquals("no symbol -ve",  negative,   validator.validate("[1,234.567]",  pattern, Locale.US));
187 
188         // invalid
189         assertFalse("invalid symbol",  validator.isValid(US_DOLLAR + "1,234.567", pattern));
190         assertFalse("invalid symbol",  validator.isValid(UK_POUND  + "1,234.567", pattern, Locale.US));
191 
192         // Restore the original default
193         Locale.setDefault(origDefault);
194     }
195 }