001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tests;
032:
033: import java.util.Calendar;
034: import java.util.GregorianCalendar;
035:
036: import junit.framework.TestCase;
037:
038: import com.jgoodies.validation.util.ValidationUtils;
039:
040: /**
041: * A test case for class {@link ValidationUtils}.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.9 $
045: *
046: * @see Calendar
047: */
048: public final class ValidationUtilsTest extends TestCase {
049:
050: private static final String EMPTY = "";
051: private static final String BLANK = " ";
052: private static final String ALPHA = "abc";
053: private static final String ALPHA_SPACE = "ab c";
054: private static final String ALPHA_DASH = "ab-c";
055: private static final String ALPHANUMERIC = "ab2c";
056: private static final String ALPHANUMERIC_SPACE = "ab2 c";
057: private static final String ALPHANUMERIC_DASH = "ab2-c";
058: private static final String NUMERIC = "123";
059: private static final String NUMERIC_SPACE = "12 3";
060: private static final String NUMERIC_DASH = "12-3";
061: private static final String NUMERIC_POINT = "12.3";
062:
063: private static final String SPACE_TEXT = " abc";
064: private static final String TEXT_SPACE = "abc ";
065: private static final String SPACE_TEXT_SPACE = " abc ";
066:
067: // String Tests ***********************************************************
068:
069: public void testIsBlank() {
070: assertTrue("Null is blank", ValidationUtils.isBlank(null));
071: assertTrue("EMPTY is blank", ValidationUtils.isBlank(EMPTY));
072: assertTrue("BLANK is blank", ValidationUtils.isBlank(BLANK));
073: assertFalse("SPACE_TEXT is not blank", ValidationUtils
074: .isBlank(SPACE_TEXT));
075: assertFalse("TEXT_SPACE is not blank", ValidationUtils
076: .isBlank(TEXT_SPACE));
077: assertFalse("SPACE_TEXT_SPACE is not blank", ValidationUtils
078: .isBlank(SPACE_TEXT_SPACE));
079: }
080:
081: public void testIsNotBlank() {
082: assertFalse("Null is not not-blank", ValidationUtils
083: .isNotBlank(null));
084: assertFalse("EMPTY is not not-blank", ValidationUtils
085: .isNotBlank(EMPTY));
086: assertFalse("BLANK is not not-blank", ValidationUtils
087: .isNotBlank(BLANK));
088: assertTrue("SPACE_TEXT is not-blank", ValidationUtils
089: .isNotBlank(SPACE_TEXT));
090: assertTrue("TEXT_SPACE is not-blank", ValidationUtils
091: .isNotBlank(TEXT_SPACE));
092: assertTrue("SPACE_TEXT_SPACE is not-blank", ValidationUtils
093: .isNotBlank(SPACE_TEXT_SPACE));
094: }
095:
096: public void testIsEmpty() {
097: assertTrue("Null is empty", ValidationUtils.isEmpty(null));
098: assertTrue("EMPTY is empty", ValidationUtils.isEmpty(EMPTY));
099: assertFalse("BLANK is not empty", ValidationUtils
100: .isEmpty(BLANK));
101: assertFalse("SPACE_TEXT is not blank", ValidationUtils
102: .isEmpty(SPACE_TEXT));
103: assertFalse("TEXT_SPACE is not blank", ValidationUtils
104: .isEmpty(TEXT_SPACE));
105: assertFalse("SPACE_TEXT_SPACE is not blank", ValidationUtils
106: .isEmpty(SPACE_TEXT_SPACE));
107: }
108:
109: public void testIsNotEmpty() {
110: assertFalse("Null is not not-empty", ValidationUtils
111: .isNotEmpty(null));
112: assertFalse("EMPTY is not not-empty", ValidationUtils
113: .isNotEmpty(EMPTY));
114: assertTrue("BLANK is not-empty", ValidationUtils
115: .isNotEmpty(BLANK));
116: assertTrue("SPACE_TEXT is not-empty", ValidationUtils
117: .isNotEmpty(SPACE_TEXT));
118: assertTrue("TEXT_SPACE is not-empty", ValidationUtils
119: .isNotEmpty(TEXT_SPACE));
120: assertTrue("SPACE_TEXT_SPACE is not-empty", ValidationUtils
121: .isNotEmpty(SPACE_TEXT_SPACE));
122: }
123:
124: // Character Tests ********************************************************
125:
126: public void testIsAlpha() {
127: assertFalse("Null is not alpha", ValidationUtils.isAlpha(null));
128: assertTrue("EMPTY is alpha", ValidationUtils.isAlpha(EMPTY));
129: assertFalse("BLANK is not alpha", ValidationUtils
130: .isAlpha(BLANK));
131: assertTrue("ALPHA is alpha", ValidationUtils.isAlpha(ALPHA));
132: assertFalse("ALPHA_SPACE is not alpha", ValidationUtils
133: .isAlpha(ALPHA_SPACE));
134: assertFalse("ALPHA_DASH is not alpha", ValidationUtils
135: .isAlpha(ALPHA_DASH));
136: assertFalse("ALPHANUMERIC is not alpha", ValidationUtils
137: .isAlpha(ALPHANUMERIC));
138: assertFalse("ALPHANUMERIC_SPACE is not alpha", ValidationUtils
139: .isAlpha(ALPHANUMERIC_SPACE));
140: assertFalse("ALPHANUMERIC_DASH is not alpha", ValidationUtils
141: .isAlpha(ALPHANUMERIC_DASH));
142: assertFalse("NUMERIC is not alpha", ValidationUtils
143: .isAlpha(NUMERIC));
144: assertFalse("NUMERIC_SPACE is not alpha", ValidationUtils
145: .isAlpha(NUMERIC_SPACE));
146: assertFalse("NUMERIC_DASH is not alpha", ValidationUtils
147: .isAlpha(NUMERIC_DASH));
148: }
149:
150: public void testIsAlphaSpace() {
151: assertFalse("Null is not alpha-space", ValidationUtils
152: .isAlphaSpace(null));
153: assertTrue("EMPTY is alpha-space", ValidationUtils
154: .isAlphaSpace(EMPTY));
155: assertTrue("BLANK is alpha-space", ValidationUtils
156: .isAlphaSpace(BLANK));
157: assertTrue("ALPHA is alpha-space", ValidationUtils
158: .isAlphaSpace(ALPHA));
159: assertTrue("ALPHA_SPACE is alpha-space", ValidationUtils
160: .isAlphaSpace(ALPHA_SPACE));
161: assertFalse("ALPHA_DASH is not alpha-space", ValidationUtils
162: .isAlphaSpace(ALPHA_DASH));
163: assertFalse("ALPHANUMERIC is not alpha-space", ValidationUtils
164: .isAlphaSpace(ALPHANUMERIC));
165: assertFalse("ALPHANUMERIC_SPACE is not alpha-space",
166: ValidationUtils.isAlphaSpace(ALPHANUMERIC_SPACE));
167: assertFalse("ALPHANUMERIC_DASH is not alpha-space",
168: ValidationUtils.isAlphaSpace(ALPHANUMERIC_DASH));
169: assertFalse("NUMERIC is not alpha-space", ValidationUtils
170: .isAlphaSpace(NUMERIC));
171: assertFalse("NUMERIC_SPACE is not alpha-space", ValidationUtils
172: .isAlphaSpace(NUMERIC_SPACE));
173: assertFalse("NUMERIC_DASH is not alpha-space", ValidationUtils
174: .isAlphaSpace(NUMERIC_DASH));
175: }
176:
177: public void testIsAlphanumeric() {
178: assertFalse("Null is not alphanumeric", ValidationUtils
179: .isAlphanumeric(null));
180: assertTrue("EMPTY is alphanumeric", ValidationUtils
181: .isAlphanumeric(EMPTY));
182: assertFalse("BLANK is not alphanumeric", ValidationUtils
183: .isAlphanumeric(BLANK));
184: assertTrue("ALPHA is alphanumeric", ValidationUtils
185: .isAlphanumeric(ALPHA));
186: assertFalse("ALPHA_SPACE is not alphanumeric", ValidationUtils
187: .isAlphanumeric(ALPHA_SPACE));
188: assertFalse("ALPHA_DASH is not alphanumeric", ValidationUtils
189: .isAlphanumeric(ALPHA_DASH));
190: assertTrue("ALPHANUMERIC is alphanumeric", ValidationUtils
191: .isAlphanumeric(ALPHANUMERIC));
192: assertFalse("ALPHANUMERIC_SPACE is not alphanumeric",
193: ValidationUtils.isAlphanumeric(ALPHANUMERIC_SPACE));
194: assertFalse("ALPHANUMERIC_DASH is not alphanumeric",
195: ValidationUtils.isAlphanumeric(ALPHANUMERIC_DASH));
196: assertTrue("NUMERIC is alphanumeric", ValidationUtils
197: .isAlphanumeric(NUMERIC));
198: assertFalse("NUMERIC_SPACE is not alphanumeric",
199: ValidationUtils.isAlphanumeric(NUMERIC_SPACE));
200: assertFalse("NUMERIC_DASH is not alphanumeric", ValidationUtils
201: .isAlphanumeric(NUMERIC_DASH));
202: }
203:
204: public void testIsAlphanumericSpace() {
205: assertFalse("Null is not alphanumeric-space", ValidationUtils
206: .isAlphanumericSpace(null));
207: assertTrue("EMPTY is alphanumeric-space", ValidationUtils
208: .isAlphanumericSpace(EMPTY));
209: assertTrue("BLANK is alphanumeric-space", ValidationUtils
210: .isAlphanumericSpace(BLANK));
211: assertTrue("ALPHA is alphanumeric-space", ValidationUtils
212: .isAlphanumericSpace(ALPHA));
213: assertTrue("ALPHA_SPACE is alphanumeric-space", ValidationUtils
214: .isAlphanumericSpace(ALPHA_SPACE));
215: assertFalse("ALPHA_DASH is not alphanumeric-space",
216: ValidationUtils.isAlphanumericSpace(ALPHA_DASH));
217: assertTrue("ALPHANUMERIC is alphanumeric-space",
218: ValidationUtils.isAlphanumericSpace(ALPHANUMERIC));
219: assertTrue("ALPHANUMERIC_SPACE is alphanumeric-space",
220: ValidationUtils.isAlphanumericSpace(ALPHANUMERIC_SPACE));
221: assertFalse("ALPHANUMERIC_DASH is not alphanumeric-space",
222: ValidationUtils.isAlphanumericSpace(ALPHANUMERIC_DASH));
223: assertTrue("NUMERIC is alphanumeric-space", ValidationUtils
224: .isAlphanumericSpace(NUMERIC));
225: assertTrue("NUMERIC_SPACE is not alphanumeric-space",
226: ValidationUtils.isAlphanumericSpace(NUMERIC_SPACE));
227: assertFalse("NUMERIC_DASH is not alphanumeric-space",
228: ValidationUtils.isAlphanumericSpace(NUMERIC_DASH));
229: }
230:
231: public void testIsNumeric() {
232: assertFalse("Null is not numeric", ValidationUtils
233: .isNumeric(null));
234: assertTrue("EMPTY is numeric", ValidationUtils.isNumeric(EMPTY));
235: assertFalse("BLANK is not numeric", ValidationUtils
236: .isNumeric(BLANK));
237: assertFalse("ALPHA is not numeric", ValidationUtils
238: .isNumeric(ALPHA));
239: assertFalse("ALPHA_SPACE is not numeric", ValidationUtils
240: .isNumeric(ALPHA_SPACE));
241: assertFalse("ALPHA_DASH is not numeric", ValidationUtils
242: .isNumeric(ALPHA_DASH));
243: assertFalse("ALPHANUMERIC is not numeric", ValidationUtils
244: .isNumeric(ALPHANUMERIC));
245: assertFalse("ALPHANUMERIC_SPACE is not numeric",
246: ValidationUtils.isNumeric(ALPHANUMERIC_SPACE));
247: assertFalse("ALPHANUMERIC_DASH is not numeric", ValidationUtils
248: .isNumeric(ALPHANUMERIC_DASH));
249: assertTrue("NUMERIC is numeric", ValidationUtils
250: .isNumeric(NUMERIC));
251: assertFalse("NUMERIC_SPACE is not numeric", ValidationUtils
252: .isNumeric(NUMERIC_SPACE));
253: assertFalse("NUMERIC_DASH is not numeric", ValidationUtils
254: .isNumeric(NUMERIC_DASH));
255: assertFalse("NUMERIC_POINT is not numeric", ValidationUtils
256: .isNumeric(NUMERIC_POINT));
257: }
258:
259: public void testIsNumericSpace() {
260: assertFalse("Null is not numeric-space", ValidationUtils
261: .isNumericSpace(null));
262: assertTrue("EMPTY is numeric-space", ValidationUtils
263: .isNumericSpace(EMPTY));
264: assertTrue("BLANK is numeric-space", ValidationUtils
265: .isNumericSpace(BLANK));
266: assertFalse("ALPHA is not numeric-space", ValidationUtils
267: .isNumericSpace(ALPHA));
268: assertFalse("ALPHA_SPACE is not numeric-space", ValidationUtils
269: .isNumericSpace(ALPHA_SPACE));
270: assertFalse("ALPHA_DASH is not numeric-space", ValidationUtils
271: .isNumericSpace(ALPHA_DASH));
272: assertFalse("ALPHANUMERIC is not numeric-space",
273: ValidationUtils.isNumericSpace(ALPHANUMERIC));
274: assertFalse("ALPHANUMERIC_SPACE is not numeric-space",
275: ValidationUtils.isNumericSpace(ALPHANUMERIC_SPACE));
276: assertFalse("ALPHANUMERIC_DASH is not numeric-space",
277: ValidationUtils.isNumericSpace(ALPHANUMERIC_DASH));
278: assertTrue("NUMERIC is numeric-space", ValidationUtils
279: .isNumericSpace(NUMERIC));
280: assertTrue("NUMERIC_SPACE is numeric-space", ValidationUtils
281: .isNumericSpace(NUMERIC_SPACE));
282: assertFalse("NUMERIC_DASH is not numeric-space",
283: ValidationUtils.isNumericSpace(NUMERIC_DASH));
284: assertFalse("NUMERIC_POINT is not numeric-space",
285: ValidationUtils.isNumericSpace(NUMERIC_POINT));
286: }
287:
288: // Calendar Tests *********************************************************
289:
290: /**
291: * Verifies that <code>#getRelativeCalendar(Calendar, int)</code>
292: * computes proper relative dates within the same month.
293: */
294: public void testRelativeCalendarSameMonth() {
295: Calendar birthday = new GregorianCalendar(1967, 11, 5); // Dec-05-67
296: Calendar nikolaus = new GregorianCalendar(1967, 11, 6); // Dec-06-67
297: Calendar xmas1967 = new GregorianCalendar(1967, 11, 24); // Dec-24-67
298:
299: assertEquals("Birthday + 0 is birthday.", birthday,
300: ValidationUtils.getRelativeCalendar(birthday, 0));
301:
302: assertEquals("Nikolaus is a day after my birthday.", nikolaus,
303: ValidationUtils.getRelativeCalendar(birthday, 1));
304:
305: assertEquals("My birthday is a day before Nikolaus.", birthday,
306: ValidationUtils.getRelativeCalendar(nikolaus, -1));
307:
308: assertEquals("Christmas is 19 days after my birthday.",
309: xmas1967, ValidationUtils.getRelativeCalendar(birthday,
310: 19));
311:
312: assertEquals("My birthday is 19 days before Christmas 1967.",
313: birthday, ValidationUtils.getRelativeCalendar(xmas1967,
314: -19));
315: }
316:
317: /**
318: * Verifies that <code>#getRelativeCalendar(Calendar, int)</code>
319: * computes proper relative dates when the month changes.
320: */
321: public void testRelativeCalendarDifferentMonths() {
322: Calendar johnsBirthday = new GregorianCalendar(2003, 10, 03); // Nov-03-04
323: Calendar christmas2003 = new GregorianCalendar(2003, 11, 24); // Dec-24-04
324:
325: assertEquals(
326: "Christmas 2003 is 51 days after John's birthday.",
327: christmas2003, ValidationUtils.getRelativeCalendar(
328: johnsBirthday, 51));
329:
330: assertEquals(
331: "John's birthday is 51 days before Christmas 2003.",
332: johnsBirthday, ValidationUtils.getRelativeCalendar(
333: christmas2003, -51));
334: }
335:
336: /**
337: * Verifies that <code>#getRelativeCalendar(Calendar, int)</code>
338: * computes proper relative dates when the month changes.
339: */
340: public void testRelativeCalendarLeapYear() {
341: Calendar feb28th2004 = new GregorianCalendar(2004, 1, 28); // Feb-28-04
342: Calendar feb29th2004 = new GregorianCalendar(2004, 1, 29); // Feb-29-04
343: Calendar mar1st2004 = new GregorianCalendar(2004, 2, 01); // Mar-01-04
344:
345: assertEquals("Feb-29-04 is a day after Feb-28-04.",
346: feb29th2004, ValidationUtils.getRelativeCalendar(
347: feb28th2004, 1));
348:
349: assertEquals("Feb-28-04 is a day before Feb-29-04.",
350: feb28th2004, ValidationUtils.getRelativeCalendar(
351: feb29th2004, -1));
352:
353: assertEquals("Mar-01-04 is a day after Feb-29-04.", mar1st2004,
354: ValidationUtils.getRelativeCalendar(feb29th2004, 1));
355:
356: assertEquals("Feb-29-04 is a day before Mar-01-04.",
357: feb29th2004, ValidationUtils.getRelativeCalendar(
358: mar1st2004, -1));
359:
360: assertEquals("Mar-01-04 is two days after Feb-28-04.",
361: mar1st2004, ValidationUtils.getRelativeCalendar(
362: feb28th2004, 2));
363:
364: assertEquals("Feb-28-04 is two days before Mar-01-04.",
365: feb28th2004, ValidationUtils.getRelativeCalendar(
366: mar1st2004, -2));
367: }
368:
369: }
|