001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.validators;
017:
018: import java.util.Calendar;
019: import java.util.Locale;
020:
021: import junit.framework.TestCase;
022:
023: import org.iscreen.ValidationFailure;
024: import org.iscreen.impl.DefaultValidatorContext;
025: import org.iscreen.ognl.OgnlMessage;
026: import org.iscreen.impl.ContextBean;
027:
028: /**
029: * Tests the DateRangeValidator validator.
030: *
031: * @author Shellman, Dan
032: */
033: public class DateRangeValidatorTest extends TestCase {
034: private DateRangeValidator validator;
035: private DefaultValidatorContext context;
036: private DateRangeValidator.BeanToValidate bean;
037: private ContextBean root;
038:
039: /**
040: * Default constructor.
041: */
042: public DateRangeValidatorTest(String name) {
043: super (name);
044: } //end DateRangeValidatorTest()
045:
046: protected void setUp() {
047: validator = new DateRangeValidator();
048: validator.setRangeFailure(new OgnlMessage("range"));
049: validator.setBeforeFailure(new OgnlMessage("before"));
050: validator.setAfterFailure(new OgnlMessage("after"));
051: bean = (DateRangeValidator.BeanToValidate) validator
052: .constructBeanToValidate();
053: root = new ContextBean();
054: context = new DefaultValidatorContext(root, Locale.getDefault());
055: } //end setUp()
056:
057: /**
058: * Tests to make sure that if the range is wrong, a failure is reported.
059: */
060: public void testRangeFailure() {
061: Calendar cal = Calendar.getInstance();
062:
063: bean.setFrom(cal.getTime());
064: cal.add(Calendar.MONTH, -1);
065: bean.setTo(cal.getTime());
066: validator.validate(context, bean);
067: assertEquals(1, context.getFailureCount());
068: assertEquals("range", ((ValidationFailure) context
069: .getFailures().get(0)).getMessage());
070: } //end testRangeFailure()
071:
072: /**
073: * Tests to make sure that if the range is correct, no failure is reported.
074: */
075: public void testRangeSuccess() {
076: Calendar cal = Calendar.getInstance();
077:
078: bean.setFrom(cal.getTime());
079: cal.add(Calendar.MONTH, 1);
080: bean.setTo(cal.getTime());
081: validator.validate(context, bean);
082: assertEquals(0, context.getFailureCount());
083: } //end testRangeSuccess()
084:
085: /**
086: * Tests to make sure that if the range is equal, no failure is reported.
087: */
088: public void testRangeEqual() {
089: Calendar cal = Calendar.getInstance();
090:
091: bean.setFrom(cal.getTime());
092: bean.setTo(cal.getTime());
093: validator.validate(context, bean);
094: assertEquals(0, context.getFailureCount());
095: } //end testRangeEqual()
096:
097: /**
098: * Tests to make sure that a date after the 'to' reports a failure.
099: */
100: public void testBetweenFailureAfter() {
101: Calendar cal = Calendar.getInstance();
102:
103: bean.setTo(cal.getTime());
104: cal.add(Calendar.MONTH, 1);
105: bean.setDate(cal.getTime());
106: validator.validate(context, bean);
107: assertEquals(1, context.getFailureCount());
108: assertEquals("after", ((ValidationFailure) context
109: .getFailures().get(0)).getMessage());
110: } //end testBetweenFailureAfter()
111:
112: /**
113: * Tests to make sure that a date before the 'from' reports a failure.
114: */
115: public void testBetweenFailureBefore() {
116: Calendar cal = Calendar.getInstance();
117:
118: bean.setFrom(cal.getTime());
119: cal.add(Calendar.MONTH, -1);
120: bean.setDate(cal.getTime());
121: validator.validate(context, bean);
122: assertEquals(1, context.getFailureCount());
123: assertEquals("before", ((ValidationFailure) context
124: .getFailures().get(0)).getMessage());
125: } //end testBetweenFailureBefore()
126:
127: /**
128: * Tests to make sure that a date that is within the range doesn't report a failure.
129: */
130: public void testBetweenSuccess() {
131: Calendar cal = Calendar.getInstance();
132:
133: bean.setFrom(cal.getTime());
134: cal.add(Calendar.MONTH, 1);
135: bean.setDate(cal.getTime());
136: cal.add(Calendar.MONTH, 1);
137: bean.setTo(cal.getTime());
138: validator.validate(context, bean);
139: assertEquals(0, context.getFailureCount());
140: } //end testBetweenSuccess()
141:
142: /**
143: * Tests to make sure that if the 'date' is equal to the 'from' date, no failure
144: * is reported.
145: */
146: public void testBetweenOnBefore() {
147: Calendar cal = Calendar.getInstance();
148:
149: bean.setFrom(cal.getTime());
150: bean.setDate(cal.getTime());
151: validator.validate(context, bean);
152: assertEquals(0, context.getFailureCount());
153: } //end testBetweenOnBefore()
154:
155: /**
156: * Tests to make sure that if the 'date' is equal to the 'to' date, no failure
157: * is reported.
158: */
159: public void testBetweenOnAfter() {
160: Calendar cal = Calendar.getInstance();
161:
162: bean.setDate(cal.getTime());
163: bean.setTo(cal.getTime());
164: validator.validate(context, bean);
165: assertEquals(0, context.getFailureCount());
166: } //end testBetweenOnAfter()
167: } //end DateRangeValidatorTest
|