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:
020: import junit.framework.TestCase;
021:
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 StringValidator validator.
030: *
031: * @author Shellman, Dan
032: */
033: public class StringValidatorTest extends TestCase {
034: private StringValidator validator;
035: private DefaultValidatorContext context;
036: private SimpleBean bean;
037: private ContextBean root;
038:
039: public StringValidatorTest(String name) {
040: super (name);
041: } //end StringValidatorTest()
042:
043: protected void setUp() {
044: validator = new StringValidator();
045: validator.setMaxLengthFailure(new OgnlMessage("too long"));
046: validator.setMinLengthFailure(new OgnlMessage("too short"));
047: validator.setNullFailure(new OgnlMessage("null value"));
048: bean = new SimpleBean();
049: root = new ContextBean();
050: context = new DefaultValidatorContext(root, Locale.getDefault());
051: } //end setUp()
052:
053: /**
054: * Tests the minimum length check.
055: */
056: public void testMinLengthCheck() {
057: bean.setValue("too short");
058: validator.setMinLength(new Integer(20));
059: validator.validate(context, bean);
060: assertEquals(1, context.getFailureCount());
061: assertEquals("too short", ((ValidationFailure) context
062: .getFailures().get(0)).getMessage());
063: } //end testMinLengthCheck()
064:
065: /**
066: * Test to make sure that a value that's too long is reported properly.
067: */
068: public void testMaxLengthCheck() {
069: bean.setValue("too long");
070: validator.setMaxLength(new Integer(1));
071: validator.validate(context, bean);
072: assertEquals(1, context.getFailureCount());
073: assertEquals("too long", ((ValidationFailure) context
074: .getFailures().get(0)).getMessage());
075: } //end testMaxLengthCheck()
076:
077: /**
078: * Test to make sure that a null value with a positive minimum length
079: * requirement causes a null failure.
080: */
081: public void testNullCheck() {
082: bean.setValue(null);
083: validator.setMinLength(new Integer(1));
084: validator.validate(context, bean);
085: assertEquals(1, context.getFailureCount());
086: assertEquals("null value", ((ValidationFailure) context
087: .getFailures().get(0)).getMessage());
088: } //end testNullCheck()
089:
090: /**
091: * Test to make sure that if a null value is checked but the
092: * minimum length is zero/null, then no failure is reported.
093: */
094: public void testNonNullCheck() {
095: bean.setValue(null);
096: validator.setMinLength(null);
097: validator.validate(context, bean);
098: assertEquals(0, context.getFailureCount());
099: } //end testNonNullCheck()
100: } //end StringValidatorTest
|