Source Code Cross Referenced for CurrencyValidatorTest.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » routines » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator.routines 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.