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 */ 017package org.apache.commons.validator.routines; 018 019import junit.framework.TestCase; 020 021import java.util.Locale; 022import java.math.BigDecimal; 023import java.text.DecimalFormatSymbols; 024 025/** 026 * Test Case for CurrencyValidator. 027 * 028 * @version $Revision: 1094751 $ $Date: 2011-04-18 17:17:40 -0400 (Mon, 18 Apr 2011) $ 029 */ 030public 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 * Constructor 039 * @param name test name 040 */ 041 public CurrencyValidatorTest(String name) { 042 super(name); 043 } 044 045 protected void setUp() throws Exception { 046 super.setUp(); 047 US_DOLLAR = (new DecimalFormatSymbols(Locale.US)).getCurrencySymbol(); 048 UK_POUND = (new DecimalFormatSymbols(Locale.UK)).getCurrencySymbol(); 049 } 050 051 /** 052 * Tear down 053 * @throws Exception 054 */ 055 protected void tearDown() throws Exception { 056 super.tearDown(); 057 } 058 059 /** 060 * Test Format Type 061 */ 062 public void testFormatType() { 063 assertEquals("Format Type A", 1, CurrencyValidator.getInstance().getFormatType()); 064 assertEquals("Format Type B", CurrencyValidator.CURRENCY_FORMAT, CurrencyValidator.getInstance().getFormatType()); 065 } 066 067 /** 068 * Test Valid currency values 069 */ 070 public void testValid() { 071 // Set the default Locale 072 Locale origDefault = Locale.getDefault(); 073 Locale.setDefault(Locale.UK); 074 075 BigDecimalValidator validator = CurrencyValidator.getInstance(); 076 BigDecimal expected = new BigDecimal("1234.56"); 077 BigDecimal negative = new BigDecimal("-1234.56"); 078 BigDecimal noDecimal = new BigDecimal("1234.00"); 079 BigDecimal oneDecimal = new BigDecimal("1234.50"); 080 081 assertEquals("Default locale", expected, validator.validate(UK_POUND + "1,234.56")); 082 083 assertEquals("UK locale", expected, validator.validate(UK_POUND + "1,234.56", Locale.UK)); 084 assertEquals("UK negative", negative, validator.validate("-" + UK_POUND + "1,234.56", Locale.UK)); 085 assertEquals("UK no decimal", noDecimal, validator.validate(UK_POUND + "1,234", Locale.UK)); 086 assertEquals("UK 1 decimal", oneDecimal, validator.validate(UK_POUND + "1,234.5", Locale.UK)); 087 assertEquals("UK 3 decimal", expected, validator.validate(UK_POUND + "1,234.567", Locale.UK)); 088 assertEquals("UK no symbol", expected, validator.validate("1,234.56", Locale.UK)); 089 090 assertEquals("US locale", expected, validator.validate(US_DOLLAR + "1,234.56", Locale.US)); 091 assertEquals("US negative", negative, validator.validate("(" + US_DOLLAR + "1,234.56)", Locale.US)); 092 assertEquals("US no decimal", noDecimal, validator.validate(US_DOLLAR + "1,234", Locale.US)); 093 assertEquals("US 1 decimal", oneDecimal, validator.validate(US_DOLLAR + "1,234.5", Locale.US)); 094 assertEquals("US 3 decimal", expected, validator.validate(US_DOLLAR + "1,234.567", Locale.US)); 095 assertEquals("US no symbol", expected, validator.validate("1,234.56", Locale.US)); 096 097 // Restore the original default 098 Locale.setDefault(origDefault); 099 } 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}