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$
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      @Override
46      protected void setUp() throws Exception {
47          super.setUp();
48          US_DOLLAR = (new DecimalFormatSymbols(Locale.US)).getCurrencySymbol();
49          UK_POUND  = (new DecimalFormatSymbols(Locale.UK)).getCurrencySymbol();
50      }
51  
52      /**
53       * Tear down
54       * @throws Exception
55       */
56      @Override
57      protected void tearDown() throws Exception {
58          super.tearDown();
59      }
60  
61      /**
62       * Test Format Type
63       */
64      public void testFormatType() {
65          assertEquals("Format Type A", 1, CurrencyValidator.getInstance().getFormatType());
66          assertEquals("Format Type B", AbstractNumberValidator.CURRENCY_FORMAT, CurrencyValidator.getInstance().getFormatType());
67      }
68  
69      /**
70       * Test Valid currency values
71       */
72      public void testValid() {
73          // Set the default Locale
74          Locale origDefault = Locale.getDefault();
75          Locale.setDefault(Locale.UK);
76  
77          BigDecimalValidator validator = CurrencyValidator.getInstance();
78          BigDecimal expected   = new BigDecimal("1234.56");
79          BigDecimal negative   = new BigDecimal("-1234.56");
80          BigDecimal noDecimal  = new BigDecimal("1234.00");
81          BigDecimal oneDecimal = new BigDecimal("1234.50");
82  
83          assertEquals("Default locale", expected, validator.validate(UK_POUND + "1,234.56"));
84  
85          assertEquals("UK locale",     expected,   validator.validate(UK_POUND  + "1,234.56",   Locale.UK));
86          assertEquals("UK negative",   negative,   validator.validate("-" + UK_POUND  + "1,234.56",  Locale.UK));
87          assertEquals("UK no decimal", noDecimal,  validator.validate(UK_POUND  + "1,234",      Locale.UK));
88          assertEquals("UK 1 decimal",  oneDecimal, validator.validate(UK_POUND  + "1,234.5",    Locale.UK));
89          assertEquals("UK 3 decimal",  expected,   validator.validate(UK_POUND  + "1,234.567",  Locale.UK));
90          assertEquals("UK no symbol",  expected,   validator.validate("1,234.56",    Locale.UK));
91  
92          assertEquals("US locale",     expected,   validator.validate(US_DOLLAR + "1,234.56",   Locale.US));
93          assertEquals("US negative",   negative,   validator.validate("(" + US_DOLLAR + "1,234.56)", Locale.US));
94          assertEquals("US no decimal", noDecimal,  validator.validate(US_DOLLAR + "1,234",      Locale.US));
95          assertEquals("US 1 decimal",  oneDecimal, validator.validate(US_DOLLAR + "1,234.5",    Locale.US));
96          assertEquals("US 3 decimal",  expected,   validator.validate(US_DOLLAR + "1,234.567",  Locale.US));
97          assertEquals("US no symbol",  expected,   validator.validate("1,234.56",    Locale.US));
98  
99          // Restore the original default
100         Locale.setDefault(origDefault);
101     }
102 
103     /**
104      * Test Invalid currency values
105      */
106     public void testInvalid() {
107         BigDecimalValidator validator = CurrencyValidator.getInstance();
108 
109         // Invalid Missing
110         assertFalse("isValid() Null Value",    validator.isValid(null));
111         assertFalse("isValid() Empty Value",   validator.isValid(""));
112         assertNull("validate() Null Value",    validator.validate(null));
113         assertNull("validate() Empty Value",   validator.validate(""));
114 
115         // Invalid UK
116         assertFalse("UK wrong symbol",    validator.isValid(US_DOLLAR + "1,234.56",   Locale.UK));
117         assertFalse("UK wrong negative",  validator.isValid("(" + UK_POUND  + "1,234.56)", Locale.UK));
118 
119         // Invalid US
120         assertFalse("US wrong symbol",    validator.isValid(UK_POUND + "1,234.56",   Locale.US));
121         assertFalse("US wrong negative",  validator.isValid("-" + US_DOLLAR + "1,234.56",  Locale.US));
122     }
123 
124     /**
125      * Test Valid integer (non-decimal) currency values
126      */
127     public void testIntegerValid() {
128         // Set the default Locale
129         Locale origDefault = Locale.getDefault();
130         Locale.setDefault(Locale.UK);
131 
132         CurrencyValidator validator = new CurrencyValidator();
133         BigDecimal expected = new BigDecimal("1234.00");
134         BigDecimal negative = new BigDecimal("-1234.00");
135 
136         assertEquals("Default locale", expected, validator.validate(UK_POUND +"1,234"));
137 
138         assertEquals("UK locale",      expected, validator.validate(UK_POUND + "1,234",   Locale.UK));
139         assertEquals("UK negative",    negative, validator.validate("-" + UK_POUND + "1,234",  Locale.UK));
140 
141         assertEquals("US locale",      expected, validator.validate(US_DOLLAR + "1,234",   Locale.US));
142         assertEquals("US negative",    negative, validator.validate("(" + US_DOLLAR + "1,234)", Locale.US));
143 
144         // Restore the original default
145         Locale.setDefault(origDefault);
146     }
147 
148     /**
149      * Test Invalid integer (non decimal) currency values
150      */
151     public void testIntegerInvalid() {
152         CurrencyValidator validator = new CurrencyValidator(true, false);
153 
154         // Invalid UK - has decimals
155         assertFalse("UK positive",    validator.isValid(UK_POUND + "1,234.56",   Locale.UK));
156         assertFalse("UK negative",    validator.isValid("-" + UK_POUND + "1,234.56", Locale.UK));
157 
158         // Invalid US - has decimals
159         assertFalse("US positive",    validator.isValid(US_DOLLAR + "1,234.56",   Locale.US));
160         assertFalse("US negative",    validator.isValid("(" + US_DOLLAR + "1,234.56)",  Locale.US));
161     }
162 
163 
164     /**
165      * Test currency values with a pattern
166      */
167     public void testPattern() {
168         // Set the default Locale
169         Locale origDefault = Locale.getDefault();
170         Locale.setDefault(Locale.UK);
171 
172         BigDecimalValidator validator = CurrencyValidator.getInstance();
173         String basicPattern = CURRENCY_SYMBOL + "#,##0.000";
174         String pattern = basicPattern + ";[" + basicPattern +"]";
175         BigDecimal expected   = new BigDecimal("1234.567");
176         BigDecimal negative   = new BigDecimal("-1234.567");
177 
178         // Test Pattern
179         assertEquals("default",        expected,   validator.validate(UK_POUND + "1,234.567", pattern));
180         assertEquals("negative",       negative,   validator.validate("[" + UK_POUND + "1,234.567]", pattern));
181         assertEquals("no symbol +ve",  expected,   validator.validate("1,234.567",    pattern));
182         assertEquals("no symbol -ve",  negative,   validator.validate("[1,234.567]",  pattern));
183 
184         // Test Pattern & Locale
185         assertEquals("default",        expected,   validator.validate(US_DOLLAR + "1,234.567", pattern, Locale.US));
186         assertEquals("negative",       negative,   validator.validate("[" + US_DOLLAR + "1,234.567]", pattern, Locale.US));
187         assertEquals("no symbol +ve",  expected,   validator.validate("1,234.567",    pattern, Locale.US));
188         assertEquals("no symbol -ve",  negative,   validator.validate("[1,234.567]",  pattern, Locale.US));
189 
190         // invalid
191         assertFalse("invalid symbol",  validator.isValid(US_DOLLAR + "1,234.567", pattern));
192         assertFalse("invalid symbol",  validator.isValid(UK_POUND  + "1,234.567", pattern, Locale.US));
193 
194         // Restore the original default
195         Locale.setDefault(origDefault);
196     }
197 }