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.text.DecimalFormat;
023: import java.math.BigDecimal;
024:
025: /**
026: * Base Number Test Case.
027: *
028: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
029: */
030: public class BaseNumberValidatorTest extends TestCase {
031:
032: protected AbstractNumberValidator validator;
033: protected AbstractNumberValidator strictValidator;
034:
035: protected Number max;
036: protected Number maxPlusOne;
037: protected Number min;
038: protected Number minMinusOne;
039: protected String[] invalid;
040: protected String[] valid;
041: protected Number[] validCompare;
042:
043: protected String[] invalidStrict;
044: protected String[] validStrict;
045: protected Number[] validStrictCompare;
046:
047: protected String testPattern;
048: protected Number testNumber;
049: protected Number testZero;
050: protected String testStringUS;
051: protected String testStringDE;
052:
053: protected String localeValue;
054: protected String localePattern;
055: protected Locale testLocale;
056: protected Number localeExpected;
057:
058: /**
059: * Constructor
060: * @param name test name
061: */
062: public BaseNumberValidatorTest(String name) {
063: super (name);
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068:
069: Locale.setDefault(Locale.US);
070:
071: }
072:
073: /**
074: * Tear down
075: * @throws Exception
076: */
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: validator = null;
080: strictValidator = null;
081: }
082:
083: /**
084: * Test Format Type
085: */
086: public void testFormatType() {
087: assertEquals("Format Type A", 0, validator.getFormatType());
088: assertEquals("Format Type B",
089: AbstractNumberValidator.STANDARD_FORMAT, validator
090: .getFormatType());
091: }
092:
093: /**
094: * Test Min/Max values allowed
095: */
096: public void testValidateMinMax() {
097: DecimalFormat fmt = new DecimalFormat("#");
098: if (max != null) {
099: assertEquals("Test Max", max, validator.parse(fmt
100: .format(max), "#", null));
101: assertNull("Test Max + 1", validator.parse(fmt
102: .format(maxPlusOne), "#", null));
103: assertEquals("Test Min", min, validator.parse(fmt
104: .format(min), "#", null));
105: assertNull("Test min - 1", validator.parse(fmt
106: .format(minMinusOne), "#", null));
107: }
108: }
109:
110: /**
111: * Test Invalid, strict=true
112: */
113: public void testInvalidStrict() {
114: for (int i = 0; i < invalidStrict.length; i++) {
115: String text = "idx=[" + i + "] value=[" + invalidStrict[i]
116: + "]";
117: assertNull("(A) " + text, strictValidator.parse(
118: invalidStrict[i], null, Locale.US));
119: assertFalse("(B) " + text, strictValidator.isValid(
120: invalidStrict[i], null, Locale.US));
121: assertNull("(C) " + text, strictValidator.parse(
122: invalidStrict[i], testPattern, null));
123: assertFalse("(D) " + text, strictValidator.isValid(
124: invalidStrict[i], testPattern, null));
125: }
126: }
127:
128: /**
129: * Test Invalid, strict=false
130: */
131: public void testInvalidNotStrict() {
132: for (int i = 0; i < invalid.length; i++) {
133: String text = "idx=[" + i + "] value=[" + invalid[i] + "]";
134: assertNull("(A) " + text, validator.parse(invalid[i], null,
135: Locale.US));
136: assertFalse("(B) " + text, validator.isValid(invalid[i],
137: null, Locale.US));
138: assertNull("(C) " + text, validator.parse(invalid[i],
139: testPattern, null));
140: assertFalse("(D) " + text, validator.isValid(invalid[i],
141: testPattern, null));
142: }
143: }
144:
145: /**
146: * Test Valid, strict=true
147: */
148: public void testValidStrict() {
149: for (int i = 0; i < validStrict.length; i++) {
150: String text = "idx=[" + i + "] value=["
151: + validStrictCompare[i] + "]";
152: assertEquals("(A) " + text, validStrictCompare[i],
153: strictValidator.parse(validStrict[i], null,
154: Locale.US));
155: assertTrue("(B) " + text, strictValidator.isValid(
156: validStrict[i], null, Locale.US));
157: assertEquals("(C) " + text, validStrictCompare[i],
158: strictValidator.parse(validStrict[i], testPattern,
159: null));
160: assertTrue("(D) " + text, strictValidator.isValid(
161: validStrict[i], testPattern, null));
162: }
163: }
164:
165: /**
166: * Test Valid, strict=false
167: */
168: public void testValidNotStrict() {
169: for (int i = 0; i < valid.length; i++) {
170: String text = "idx=[" + i + "] value=[" + validCompare[i]
171: + "]";
172: assertEquals("(A) " + text, validCompare[i], validator
173: .parse(valid[i], null, Locale.US));
174: assertTrue("(B) " + text, validator.isValid(valid[i], null,
175: Locale.US));
176: assertEquals("(C) " + text, validCompare[i], validator
177: .parse(valid[i], testPattern, null));
178: assertTrue("(D) " + text, validator.isValid(valid[i],
179: testPattern, null));
180: }
181: }
182:
183: /**
184: * Test different Locale
185: */
186: public void testValidateLocale() {
187:
188: assertEquals("US Locale, US Format", testNumber,
189: strictValidator.parse(testStringUS, null, Locale.US));
190: assertNull("US Locale, DE Format", strictValidator.parse(
191: testStringDE, null, Locale.US));
192:
193: // Default German Locale
194: assertEquals("DE Locale, DE Format", testNumber,
195: strictValidator
196: .parse(testStringDE, null, Locale.GERMAN));
197: assertNull("DE Locale, US Format", strictValidator.parse(
198: testStringUS, null, Locale.GERMAN));
199:
200: // Default Locale has been set to Locale.US in setup()
201: assertEquals("Default Locale, US Format", testNumber,
202: strictValidator.parse(testStringUS, null, null));
203: assertNull("Default Locale, DE Format", strictValidator.parse(
204: testStringDE, null, null));
205: }
206:
207: /**
208: * Test format() methods
209: */
210: public void testFormat() {
211: Number number = new BigDecimal("1234.5");
212: assertEquals("US Locale, US Format", "1,234.5", strictValidator
213: .format(number, Locale.US));
214: assertEquals("DE Locale, DE Format", "1.234,5", strictValidator
215: .format(number, Locale.GERMAN));
216: assertEquals("Pattern #,#0.00", "12,34.50", strictValidator
217: .format(number, "#,#0.00"));
218: }
219:
220: /**
221: * Test Range/Min/Max
222: */
223: public void testRangeMinMax() {
224: Number number9 = new Integer(9);
225: Number number10 = new Integer(10);
226: Number number11 = new Integer(11);
227: Number number19 = new Integer(19);
228: Number number20 = new Integer(20);
229: Number number21 = new Integer(21);
230:
231: // Test isInRange()
232: assertFalse("isInRange() < min", strictValidator.isInRange(
233: number9, number10, number20));
234: assertTrue("isInRange() = min", strictValidator.isInRange(
235: number10, number10, number20));
236: assertTrue("isInRange() in range", strictValidator.isInRange(
237: number11, number10, number20));
238: assertTrue("isInRange() = max", strictValidator.isInRange(
239: number20, number10, number20));
240: assertFalse("isInRange() > max", strictValidator.isInRange(
241: number21, number10, number20));
242:
243: // Test minValue()
244: assertFalse("minValue() < min", strictValidator.minValue(
245: number9, number10));
246: assertTrue("minValue() = min", strictValidator.minValue(
247: number10, number10));
248: assertTrue("minValue() > min", strictValidator.minValue(
249: number11, number10));
250:
251: // Test minValue()
252: assertTrue("maxValue() < max", strictValidator.maxValue(
253: number19, number20));
254: assertTrue("maxValue() = max", strictValidator.maxValue(
255: number20, number20));
256: assertFalse("maxValue() > max", strictValidator.maxValue(
257: number21, number20));
258: }
259:
260: }
|