001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.validator;
016:
017: import org.testng.annotations.Test;
018:
019: /**
020: * @author Phil Zoio
021: */
022: public class TestValidators {
023:
024: @Test
025: public void testBlankOrNull() {
026: BlankOrNullValidator bn = new BlankOrNullValidator();
027: assert bn.validate(null);
028: assert bn.validate("");
029: assert !bn.validate("something");
030:
031: }
032:
033: @Test
034: public void testRequired() {
035: RequiredValidator bn = new RequiredValidator();
036: assert !bn.validate(null);
037: assert !bn.validate("");
038: assert bn.validate("something");
039: assert bn.validate(new Integer(1));
040:
041: }
042:
043: @Test
044: public void testDate() {
045:
046: DateValidator d = new DateValidator();
047:
048: // not required
049: assert !d.validate(null);
050: assert d
051: .validate(new java.sql.Date(System.currentTimeMillis()));
052:
053: }
054:
055: @Test
056: public void testFutureDate() {
057:
058: FutureDateValidator d = new FutureDateValidator();
059: assert d
060: .validate(new java.sql.Date(System.currentTimeMillis()));
061: assert !d.validate(new java.sql.Date(System.currentTimeMillis()
062: - (1000 * 60 * 60 * 24)));
063:
064: }
065:
066: @Test
067: public void testPastDate() {
068:
069: PastDateValidator d = new PastDateValidator();
070: assert !d
071: .validate(new java.sql.Date(System.currentTimeMillis()));
072: assert d.validate(new java.sql.Date(System.currentTimeMillis()
073: - (1000 * 60 * 60 * 24)));
074:
075: }
076:
077: @Test
078: public void testInteger() {
079:
080: IntegerValidator numberValidator = new IntegerValidator();
081:
082: assert !numberValidator.validate(null);
083: assert numberValidator.validate(1);
084:
085: }
086:
087: @Test
088: public void testLong() {
089:
090: LongValidator numberValidator = new LongValidator();
091: // not required
092: assert !numberValidator.validate(null);
093: assert numberValidator.validate(1L);
094:
095: }
096:
097: @Test
098: public void testDouble() {
099:
100: DoubleValidator doubleValidator = new DoubleValidator();
101: // not required
102:
103: assert !doubleValidator.validate(null);
104: assert doubleValidator.validate(1.0);
105:
106: }
107:
108: @Test
109: public void testMaxLength() {
110:
111: MaxLengthValidator mx = new MaxLengthValidator();
112:
113: // not required
114: assert mx.validate(null);
115:
116: // required
117:
118: mx.setLength(1);
119: assert mx.validate("a");
120: assert !mx.validate("ab");
121:
122: }
123:
124: @Test
125: public void testIntegerRange() {
126:
127: IntegerRangeValidator mx = new IntegerRangeValidator();
128:
129: // not required
130: assert !mx.validate(null);
131:
132: assert mx.validate(Integer.MAX_VALUE);
133: assert mx.validate(Integer.MIN_VALUE);
134:
135: mx.setMax(0);
136: assert mx.validate(0);
137: assert !mx.validate(1);
138:
139: mx.setMax(Integer.MAX_VALUE);
140: mx.setMin(0);
141: assert mx.validate(0);
142: assert !mx.validate(-1);
143:
144: }
145:
146: @Test
147: public void testLongRange() {
148:
149: LongRangeValidator mx = new LongRangeValidator();
150:
151: // not required
152: assert !mx.validate(null);
153:
154: assert mx.validate(Long.MAX_VALUE);
155: assert mx.validate(Long.MIN_VALUE);
156:
157: mx.setMax(0);
158: assert mx.validate(0L);
159: assert !mx.validate(1L);
160:
161: mx.setMax(Long.MAX_VALUE);
162: mx.setMin(0);
163: assert mx.validate(0L);
164: assert !mx.validate(-1L);
165:
166: }
167:
168: @Test
169: public void testUrl() {
170: UrlValidator bn = new UrlValidator();
171: assert bn.validate(null);
172: assert bn.validate("");
173: assert !bn.validate("duff url");
174: assert bn.validate("http://www.sourceforge.net/index.html");
175: }
176:
177: @Test
178: public void testEmail() {
179: EmailValidator bn = new EmailValidator();
180: assert bn.validate(null);
181: assert bn.validate("");
182: assert !bn.validate("duff email");
183: assert bn.validate("philzoio@realsolve.co.uk");
184: }
185:
186: @Test
187: public void testCreditCard() {
188: CreditCardValidator bn = new CreditCardValidator();
189: assert bn.validate(null);
190: assert bn.validate("");
191: assert !bn.validate("duff cc");
192: assert bn.validate("4408041234567893");
193: }
194:
195: @Test
196: public void testPattern() {
197: PatternValidator bn = new PatternValidator("a*b");
198: assert bn.validate(null);
199: assert bn.validate("");
200: assert !bn.validate("duff pattern");
201: assert bn.validate("aaaaab");
202: }
203:
204: }
|