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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.math.BigDecimal;
24  import java.text.DecimalFormatSymbols;
25  import java.util.Locale;
26  
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test Case for CurrencyValidator.
32   */
33  public class CurrencyValidatorTest {
34  
35      private static final char CURRENCY_SYMBOL = '\u00A4';
36  
37      private String US_DOLLAR;
38      private String UK_POUND;
39  
40      @BeforeEach
41      protected void setUp() {
42          US_DOLLAR = new DecimalFormatSymbols(Locale.US).getCurrencySymbol();
43          UK_POUND = new DecimalFormatSymbols(Locale.UK).getCurrencySymbol();
44      }
45  
46      /**
47       * Test Format Type
48       */
49      @Test
50      public void testFormatType() {
51          assertEquals(1, CurrencyValidator.getInstance().getFormatType(), "Format Type A");
52          assertEquals(AbstractNumberValidator.CURRENCY_FORMAT, CurrencyValidator.getInstance().getFormatType(), "Format Type B");
53      }
54  
55      /**
56       * Test Invalid integer (non decimal) currency values
57       */
58      @Test
59      public void testIntegerInvalid() {
60          final CurrencyValidator validator = new CurrencyValidator(true, false);
61  
62          // Invalid UK - has decimals
63          assertFalse(validator.isValid(UK_POUND + "1,234.56", Locale.UK), "UK positive");
64          assertFalse(validator.isValid("-" + UK_POUND + "1,234.56", Locale.UK), "UK negative");
65  
66          // Invalid US - has decimals
67          assertFalse(validator.isValid(US_DOLLAR + "1,234.56", Locale.US), "US positive");
68          assertFalse(validator.isValid("(" + US_DOLLAR + "1,234.56)", Locale.US), "US negative");
69      }
70  
71      /**
72       * Test Valid integer (non-decimal) currency values
73       */
74      @Test
75      public void testIntegerValid() {
76          // Set the default Locale
77          final Locale origDefault = Locale.getDefault();
78          Locale.setDefault(Locale.UK);
79  
80          final CurrencyValidator validator = new CurrencyValidator();
81          final BigDecimal expected = new BigDecimal("1234.00");
82          final BigDecimal negative = new BigDecimal("-1234.00");
83  
84          assertEquals(expected, validator.validate(UK_POUND + "1,234"), "Default locale");
85  
86          assertEquals(expected, validator.validate(UK_POUND + "1,234", Locale.UK), "UK locale");
87          assertEquals(negative, validator.validate("-" + UK_POUND + "1,234", Locale.UK), "UK negative");
88  
89          assertEquals(expected, validator.validate(US_DOLLAR + "1,234", Locale.US), "US locale");
90          assertEquals(negative, validator.validate("(" + US_DOLLAR + "1,234)", Locale.US), "US negative");
91  
92          // Restore the original default
93          Locale.setDefault(origDefault);
94      }
95  
96      /**
97       * Test Invalid currency values
98       */
99      @Test
100     public void testInvalid() {
101         final BigDecimalValidator validator = CurrencyValidator.getInstance();
102 
103         // Invalid Missing
104         assertFalse(validator.isValid(null), "isValid() Null Value");
105         assertFalse(validator.isValid(""), "isValid() Empty Value");
106         assertNull(validator.validate(null), "validate() Null Value");
107         assertNull(validator.validate(""), "validate() Empty Value");
108 
109         // Invalid UK
110         assertFalse(validator.isValid(US_DOLLAR + "1,234.56", Locale.UK), "UK wrong symbol");
111         assertFalse(validator.isValid("(" + UK_POUND + "1,234.56)", Locale.UK), "UK wrong negative");
112 
113         // Invalid US
114         assertFalse(validator.isValid(UK_POUND + "1,234.56", Locale.US), "US wrong symbol");
115         assertFalse(validator.isValid("-" + US_DOLLAR + "1,234.56", Locale.US), "US wrong negative");
116     }
117 
118     /**
119      * Test currency values with a pattern
120      */
121     @Test
122     public void testPattern() {
123         // Set the default Locale
124         final Locale origDefault = Locale.getDefault();
125         Locale.setDefault(Locale.UK);
126 
127         final BigDecimalValidator validator = CurrencyValidator.getInstance();
128         final String basicPattern = CURRENCY_SYMBOL + "#,##0.000";
129         final String pattern = basicPattern + ";[" + basicPattern + "]";
130         final BigDecimal expected = new BigDecimal("1234.567");
131         final BigDecimal negative = new BigDecimal("-1234.567");
132 
133         // Test Pattern
134         assertEquals(expected, validator.validate(UK_POUND + "1,234.567", pattern), "default");
135         assertEquals(negative, validator.validate("[" + UK_POUND + "1,234.567]", pattern), "negative");
136         assertEquals(expected, validator.validate("1,234.567", pattern), "no symbol +ve");
137         assertEquals(negative, validator.validate("[1,234.567]", pattern), "no symbol -ve");
138 
139         // Test Pattern & Locale
140         assertEquals(expected, validator.validate(US_DOLLAR + "1,234.567", pattern, Locale.US), "default");
141         assertEquals(negative, validator.validate("[" + US_DOLLAR + "1,234.567]", pattern, Locale.US), "negative");
142         assertEquals(expected, validator.validate("1,234.567", pattern, Locale.US), "no symbol +ve");
143         assertEquals(negative, validator.validate("[1,234.567]", pattern, Locale.US), "no symbol -ve");
144 
145         // invalid
146         assertFalse(validator.isValid(US_DOLLAR + "1,234.567", pattern), "invalid symbol");
147         assertFalse(validator.isValid(UK_POUND + "1,234.567", pattern, Locale.US), "invalid symbol");
148 
149         // Restore the original default
150         Locale.setDefault(origDefault);
151     }
152 
153     /**
154      * Test Valid currency values
155      */
156     @Test
157     public void testValid() {
158         // Set the default Locale
159         final Locale origDefault = Locale.getDefault();
160         Locale.setDefault(Locale.UK);
161 
162         final BigDecimalValidator validator = CurrencyValidator.getInstance();
163         final BigDecimal expected = new BigDecimal("1234.56");
164         final BigDecimal negative = new BigDecimal("-1234.56");
165         final BigDecimal noDecimal = new BigDecimal("1234.00");
166         final BigDecimal oneDecimal = new BigDecimal("1234.50");
167 
168         assertEquals(expected, validator.validate(UK_POUND + "1,234.56"), "Default locale");
169 
170         assertEquals(expected, validator.validate(UK_POUND + "1,234.56", Locale.UK), "UK locale");
171         assertEquals(negative, validator.validate("-" + UK_POUND + "1,234.56", Locale.UK), "UK negative");
172         assertEquals(noDecimal, validator.validate(UK_POUND + "1,234", Locale.UK), "UK no decimal");
173         assertEquals(oneDecimal, validator.validate(UK_POUND + "1,234.5", Locale.UK), "UK 1 decimal");
174         assertEquals(expected, validator.validate(UK_POUND + "1,234.567", Locale.UK), "UK 3 decimal");
175         assertEquals(expected, validator.validate("1,234.56", Locale.UK), "UK no symbol");
176 
177         assertEquals(expected, validator.validate(US_DOLLAR + "1,234.56", Locale.US), "US locale");
178         assertEquals(negative, validator.validate("(" + US_DOLLAR + "1,234.56)", Locale.US), "US negative");
179         assertEquals(noDecimal, validator.validate(US_DOLLAR + "1,234", Locale.US), "US no decimal");
180         assertEquals(oneDecimal, validator.validate(US_DOLLAR + "1,234.5", Locale.US), "US 1 decimal");
181         assertEquals(expected, validator.validate(US_DOLLAR + "1,234.567", Locale.US), "US 3 decimal");
182         assertEquals(expected, validator.validate("1,234.56", Locale.US), "US no symbol");
183 
184         // Restore the original default
185         Locale.setDefault(origDefault);
186     }
187 }