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: }
|