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.util.Locale;
020:
021: /**
022: * Test Case for ByteValidator.
023: *
024: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
025: */
026: public class ByteValidatorTest extends BaseNumberValidatorTest {
027:
028: /**
029: * Main
030: * @param args arguments
031: */
032: public static void main(String[] args) {
033: junit.textui.TestRunner.run(ByteValidatorTest.class);
034: }
035:
036: /**
037: * Constructor
038: * @param name test name
039: */
040: public ByteValidatorTest(String name) {
041: super (name);
042: }
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: validator = new ByteValidator(false, 0);
048: strictValidator = new ByteValidator();
049:
050: testPattern = "#,###";
051:
052: // testValidateMinMax()
053: max = new Byte(Byte.MAX_VALUE);
054: maxPlusOne = new Long(max.longValue() + 1);
055: min = new Byte(Byte.MIN_VALUE);
056: minMinusOne = new Long(min.longValue() - 1);
057:
058: // testInvalidStrict()
059: invalidStrict = new String[] { null, "", "X", "X12", "12X",
060: "1X2", "1.2" };
061:
062: // testInvalidNotStrict()
063: invalid = new String[] { null, "", "X", "X12" };
064:
065: // testValid()
066: testNumber = new Byte((byte) 123);
067: testZero = new Byte((byte) 0);
068: validStrict = new String[] { "0", "123", ",123" };
069: validStrictCompare = new Number[] { testZero, testNumber,
070: testNumber };
071: valid = new String[] { "0", "123", ",123", ",123.5", "123X" };
072: validCompare = new Number[] { testZero, testNumber, testNumber,
073: testNumber, testNumber };
074:
075: testStringUS = ",123";
076: testStringDE = ".123";
077:
078: // Localized Pattern test
079: localeValue = testStringDE;
080: localePattern = "#.###";
081: testLocale = Locale.GERMANY;
082: localeExpected = testNumber;
083:
084: }
085:
086: /**
087: * Test ByteValidator validate Methods
088: */
089: public void testByteValidatorMethods() {
090: Locale locale = Locale.GERMAN;
091: String pattern = "0,00";
092: String patternVal = "1,23";
093: String germanPatternVal = "1.23";
094: String localeVal = ".123";
095: String defaultVal = ",123";
096: String XXXX = "XXXX";
097: Byte expected = new Byte((byte) 123);
098: assertEquals("validate(A) default", expected, ByteValidator
099: .getInstance().validate(defaultVal));
100: assertEquals("validate(A) locale ", expected, ByteValidator
101: .getInstance().validate(localeVal, locale));
102: assertEquals("validate(A) pattern", expected, ByteValidator
103: .getInstance().validate(patternVal, pattern));
104: assertEquals("validate(A) both", expected, ByteValidator
105: .getInstance().validate(germanPatternVal, pattern,
106: Locale.GERMAN));
107:
108: assertTrue("isValid(A) default", ByteValidator.getInstance()
109: .isValid(defaultVal));
110: assertTrue("isValid(A) locale ", ByteValidator.getInstance()
111: .isValid(localeVal, locale));
112: assertTrue("isValid(A) pattern", ByteValidator.getInstance()
113: .isValid(patternVal, pattern));
114: assertTrue("isValid(A) both", ByteValidator.getInstance()
115: .isValid(germanPatternVal, pattern, Locale.GERMAN));
116:
117: assertNull("validate(B) default", ByteValidator.getInstance()
118: .validate(XXXX));
119: assertNull("validate(B) locale ", ByteValidator.getInstance()
120: .validate(XXXX, locale));
121: assertNull("validate(B) pattern", ByteValidator.getInstance()
122: .validate(XXXX, pattern));
123: assertNull("validate(B) both", ByteValidator.getInstance()
124: .validate(patternVal, pattern, Locale.GERMAN));
125:
126: assertFalse("isValid(B) default", ByteValidator.getInstance()
127: .isValid(XXXX));
128: assertFalse("isValid(B) locale ", ByteValidator.getInstance()
129: .isValid(XXXX, locale));
130: assertFalse("isValid(B) pattern", ByteValidator.getInstance()
131: .isValid(XXXX, pattern));
132: assertFalse("isValid(B) both", ByteValidator.getInstance()
133: .isValid(patternVal, pattern, Locale.GERMAN));
134: }
135:
136: /**
137: * Test Byte Range/Min/Max
138: */
139: public void testByteRangeMinMax() {
140: ByteValidator validator = (ByteValidator) strictValidator;
141: Byte number9 = validator.validate("9", "#");
142: Byte number10 = validator.validate("10", "#");
143: Byte number11 = validator.validate("11", "#");
144: Byte number19 = validator.validate("19", "#");
145: Byte number20 = validator.validate("20", "#");
146: Byte number21 = validator.validate("21", "#");
147: byte min = (byte) 10;
148: byte max = (byte) 20;
149:
150: // Test isInRange()
151: assertFalse("isInRange() < min", validator.isInRange(number9,
152: min, max));
153: assertTrue("isInRange() = min", validator.isInRange(number10,
154: min, max));
155: assertTrue("isInRange() in range", validator.isInRange(
156: number11, min, max));
157: assertTrue("isInRange() = max", validator.isInRange(number20,
158: min, max));
159: assertFalse("isInRange() > max", validator.isInRange(number21,
160: min, max));
161:
162: // Test minValue()
163: assertFalse("minValue() < min", validator
164: .minValue(number9, min));
165: assertTrue("minValue() = min", validator
166: .minValue(number10, min));
167: assertTrue("minValue() > min", validator
168: .minValue(number11, min));
169:
170: // Test minValue()
171: assertTrue("maxValue() < max", validator
172: .maxValue(number19, max));
173: assertTrue("maxValue() = max", validator
174: .maxValue(number20, max));
175: assertFalse("maxValue() > max", validator.maxValue(number21,
176: max));
177: }
178: }
|