Source Code Cross Referenced for FloatValidatorTest.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 java.text.DecimalFormat;
020:        import java.util.Locale;
021:
022:        /**
023:         * Test Case for FloatValidator.
024:         * 
025:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
026:         */
027:        public class FloatValidatorTest extends BaseNumberValidatorTest {
028:
029:            /**
030:             * Main
031:             * @param args arguments
032:             */
033:            public static void main(String[] args) {
034:                junit.textui.TestRunner.run(FloatValidatorTest.class);
035:            }
036:
037:            /**
038:             * Constructor
039:             * @param name test name
040:             */
041:            public FloatValidatorTest(String name) {
042:                super (name);
043:            }
044:
045:            protected void setUp() throws Exception {
046:                super .setUp();
047:
048:                validator = new FloatValidator(false, 0);
049:                strictValidator = new FloatValidator();
050:
051:                testPattern = "#,###.#";
052:
053:                // testValidateMinMax()
054:                max = new Float(Float.MAX_VALUE);
055:                maxPlusOne = new Double(max.doubleValue() * 10);
056:                min = new Float(Float.MAX_VALUE * -1);
057:                minMinusOne = new Double(min.doubleValue() * 10);
058:
059:                // testInvalidStrict()
060:                invalidStrict = new String[] { null, "", "X", "X12", "12X",
061:                        "1X2" };
062:
063:                // testInvalidNotStrict()
064:                invalid = new String[] { null, "", "X", "X12" };
065:
066:                // testValid()
067:                testNumber = new Float(1234.5);
068:                testZero = new Float(0);
069:                validStrict = new String[] { "0", "1234.5", "1,234.5" };
070:                validStrictCompare = new Number[] { testZero, testNumber,
071:                        testNumber };
072:                valid = new String[] { "0", "1234.5", "1,234.5", "1,234.5",
073:                        "1234.5X" };
074:                validCompare = new Number[] { testZero, testNumber, testNumber,
075:                        testNumber, testNumber };
076:
077:                testStringUS = "1,234.5";
078:                testStringDE = "1.234,5";
079:
080:                // Localized Pattern test
081:                localeValue = testStringDE;
082:                localePattern = "#.###,#";
083:                testLocale = Locale.GERMANY;
084:                localeExpected = testNumber;
085:
086:            }
087:
088:            /**
089:             * Test FloatValidator validate Methods
090:             */
091:            public void testFloatValidatorMethods() {
092:                Locale locale = Locale.GERMAN;
093:                String pattern = "0,00,00";
094:                String patternVal = "1,23,45";
095:                String localeVal = "12.345";
096:                String germanPatternVal = "1.23.45";
097:                String defaultVal = "12,345";
098:                String XXXX = "XXXX";
099:                Float expected = new Float(12345);
100:                assertEquals("validate(A) default", expected, FloatValidator
101:                        .getInstance().validate(defaultVal));
102:                assertEquals("validate(A) locale ", expected, FloatValidator
103:                        .getInstance().validate(localeVal, locale));
104:                assertEquals("validate(A) pattern", expected, FloatValidator
105:                        .getInstance().validate(patternVal, pattern));
106:                assertEquals("validate(A) both", expected, FloatValidator
107:                        .getInstance().validate(germanPatternVal, pattern,
108:                                Locale.GERMAN));
109:
110:                assertTrue("isValid(A) default", FloatValidator.getInstance()
111:                        .isValid(defaultVal));
112:                assertTrue("isValid(A) locale ", FloatValidator.getInstance()
113:                        .isValid(localeVal, locale));
114:                assertTrue("isValid(A) pattern", FloatValidator.getInstance()
115:                        .isValid(patternVal, pattern));
116:                assertTrue("isValid(A) both", FloatValidator.getInstance()
117:                        .isValid(germanPatternVal, pattern, Locale.GERMAN));
118:
119:                assertNull("validate(B) default", FloatValidator.getInstance()
120:                        .validate(XXXX));
121:                assertNull("validate(B) locale ", FloatValidator.getInstance()
122:                        .validate(XXXX, locale));
123:                assertNull("validate(B) pattern", FloatValidator.getInstance()
124:                        .validate(XXXX, pattern));
125:                assertNull("validate(B) both", FloatValidator.getInstance()
126:                        .validate(patternVal, pattern, Locale.GERMAN));
127:
128:                assertFalse("isValid(B) default", FloatValidator.getInstance()
129:                        .isValid(XXXX));
130:                assertFalse("isValid(B) locale ", FloatValidator.getInstance()
131:                        .isValid(XXXX, locale));
132:                assertFalse("isValid(B) pattern", FloatValidator.getInstance()
133:                        .isValid(XXXX, pattern));
134:                assertFalse("isValid(B) both", FloatValidator.getInstance()
135:                        .isValid(patternVal, pattern, Locale.GERMAN));
136:            }
137:
138:            /**
139:             * Test Float validation for values too small to handle.
140:             * (slightly different from max/min which are the largest +ve/-ve
141:             */
142:            public void testFloatSmallestValues() {
143:                String pattern = "#.#################################################################";
144:                DecimalFormat fmt = new DecimalFormat(pattern);
145:
146:                // Validate Smallest +ve value
147:                Float smallestPositive = new Float(Float.MIN_VALUE);
148:                String strSmallestPositive = fmt.format(smallestPositive);
149:                assertEquals("Smallest +ve", smallestPositive, FloatValidator
150:                        .getInstance().validate(strSmallestPositive, pattern));
151:
152:                // Validate Smallest -ve value
153:                Float smallestNegative = new Float(Float.MIN_VALUE * -1);
154:                String strSmallestNegative = fmt.format(smallestNegative);
155:                assertEquals("Smallest -ve", smallestNegative, FloatValidator
156:                        .getInstance().validate(strSmallestNegative, pattern));
157:
158:                // Validate Too Small +ve
159:                Double tooSmallPositive = new Double(
160:                        ((double) Float.MIN_VALUE / (double) 10));
161:                String strTooSmallPositive = fmt.format(tooSmallPositive);
162:                assertFalse("Too small +ve", FloatValidator.getInstance()
163:                        .isValid(strTooSmallPositive, pattern));
164:
165:                // Validate Too Small -ve
166:                Double tooSmallNegative = new Double(tooSmallPositive
167:                        .doubleValue()
168:                        * (double) -1);
169:                String strTooSmallNegative = fmt.format(tooSmallNegative);
170:                assertFalse("Too small -ve", FloatValidator.getInstance()
171:                        .isValid(strTooSmallNegative, pattern));
172:            }
173:
174:            /**
175:             * Test Float Range/Min/Max
176:             */
177:            public void testFloatRangeMinMax() {
178:                FloatValidator validator = (FloatValidator) strictValidator;
179:                Float number9 = validator.validate("9", "#");
180:                Float number10 = validator.validate("10", "#");
181:                Float number11 = validator.validate("11", "#");
182:                Float number19 = validator.validate("19", "#");
183:                Float number20 = validator.validate("20", "#");
184:                Float number21 = validator.validate("21", "#");
185:
186:                // Test isInRange()
187:                assertFalse("isInRange() < min", validator.isInRange(number9,
188:                        10, 20));
189:                assertTrue("isInRange() = min", validator.isInRange(number10,
190:                        10, 20));
191:                assertTrue("isInRange() in range", validator.isInRange(
192:                        number11, 10, 20));
193:                assertTrue("isInRange() = max", validator.isInRange(number20,
194:                        10, 20));
195:                assertFalse("isInRange() > max", validator.isInRange(number21,
196:                        10, 20));
197:
198:                // Test minValue()
199:                assertFalse("minValue() < min", validator.minValue(number9, 10));
200:                assertTrue("minValue() = min", validator.minValue(number10, 10));
201:                assertTrue("minValue() > min", validator.minValue(number11, 10));
202:
203:                // Test minValue()
204:                assertTrue("maxValue() < max", validator.maxValue(number19, 20));
205:                assertTrue("maxValue() = max", validator.maxValue(number20, 20));
206:                assertFalse("maxValue() > max", validator
207:                        .maxValue(number21, 20));
208:            }
209:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.