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.Locale;
019: import junit.framework.TestCase;
020:
021: import org.iscreen.FailureMessage;
022: import org.iscreen.SimpleBean;
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 DateFormatValidator.
030: *
031: * @author Shellman, Dan
032: */
033: public class DateFormatValidatorTest extends TestCase {
034: /**
035: * Default constructor.
036: */
037: public DateFormatValidatorTest(String testName) {
038: super (testName);
039: } //end DateFormatValidatorTest()
040:
041: /**
042: * Tests invalid dates.
043: */
044: public void testInvalidDates() {
045: DateFormatValidator validator;
046: DefaultValidatorContext context;
047: ContextBean root;
048: SimpleBean bean;
049: FailureMessage message;
050: String[] badDates = { "bad date", "4/12/00", "the|quick|fox",
051: "59|43|1000" };
052:
053: root = new ContextBean();
054: context = new DefaultValidatorContext(root, Locale.getDefault());
055: bean = new SimpleBean();
056: message = new OgnlMessage("failure message");
057:
058: validator = new DateFormatValidator();
059: validator.setFormat("MM|dd|yyyy");
060: validator.setDefaultFailure(message);
061:
062: for (int i = 0; i < badDates.length; i++) {
063: bean.setValue(badDates[i]);
064: validator.validate(context, bean);
065: }
066:
067: assertEquals(badDates.length, context.getFailureCount());
068: } //end testInvalidDates()
069:
070: /**
071: * Tests the lenient flag.
072: */
073: public void testLenientFlag() {
074: DateFormatValidator validator;
075: DefaultValidatorContext context;
076: ContextBean root;
077: SimpleBean bean;
078: FailureMessage message;
079:
080: root = new ContextBean();
081: context = new DefaultValidatorContext(root, Locale.getDefault());
082: bean = new SimpleBean();
083: message = new OgnlMessage("failure message");
084:
085: validator = new DateFormatValidator();
086: validator.setFormat("MM|dd|yyyy");
087: validator.setDefaultFailure(message);
088: validator.setLenient(true);
089:
090: bean.setValue("2|30|2000");
091: validator.validate(context, bean);
092:
093: assertEquals(0, context.getFailureCount());
094:
095: validator.setLenient(false);
096: validator.validate(context, bean);
097:
098: assertEquals(1, context.getFailureCount());
099: } //end testLenientFlag()
100:
101: /**
102: * Tests the display format in failure messages.
103: */
104: public void testDisplayFormat() {
105: DateFormatValidator validator;
106: DefaultValidatorContext context;
107: ContextBean root;
108: SimpleBean bean;
109: FailureMessage message;
110:
111: root = new ContextBean();
112: context = new DefaultValidatorContext(root, Locale.getDefault());
113: bean = new SimpleBean();
114: message = new OgnlMessage(
115: "failure with code ${validator.displayFormat}.");
116:
117: validator = new DateFormatValidator();
118: validator.setFormat("MM|dd|yyyy");
119: validator.setDisplayFormat("MM|DD|YYYY");
120: validator.setDefaultFailure(message);
121:
122: root.setValidator(validator);
123: bean.setValue("bad date");
124: validator.validate(context, bean);
125:
126: assertEquals(1, context.getFailureCount());
127: assertEquals("failure with code MM|DD|YYYY.",
128: ((ValidationFailure) context.getFailures().get(0))
129: .getMessage());
130: } //end testDisplayFormat()
131: } //end DateFormatValidatorTest
|