001: /*
002: * Copyright 2007 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 NumberRangeValidatorTest extends TestCase {
034: private NumberRangeValidator validator;
035: private DefaultValidatorContext context;
036: private SimpleBean bean;
037: private ContextBean contextBean;
038:
039: public NumberRangeValidatorTest(String name) {
040: super (name);
041: } //end NumberRangeValidatorTest()
042:
043: protected void setUp() {
044: validator = new NumberRangeValidator();
045: validator
046: .setInvalidNumberFailure(new OgnlMessage("Bad Number!"));
047: validator.setRangeFailure(new OgnlMessage("Out of range!"));
048: bean = new SimpleBean();
049: contextBean = new ContextBean();
050: context = new DefaultValidatorContext(contextBean, Locale
051: .getDefault());
052: } //end setUp()
053:
054: /**
055: * Tests the minimum value check.
056: */
057: public void testTooSmallCheck() {
058: bean.setValue(new Integer(1));
059: validator.setMinimumValue(new Integer(20));
060: validator.validate(context, bean);
061: assertEquals(1, context.getFailureCount());
062: assertEquals("Out of range!", ((ValidationFailure) context
063: .getFailures().get(0)).getMessage());
064: } //end testTooSmallCheck()
065:
066: /**
067: * Test to make sure that a value that's too large is reported properly.
068: */
069: public void testTooLargeCheck() {
070: bean.setValue(new Integer(1000));
071: validator.setMaximumValue(new Integer(10));
072: validator.validate(context, bean);
073: assertEquals(1, context.getFailureCount());
074: assertEquals("Out of range!", ((ValidationFailure) context
075: .getFailures().get(0)).getMessage());
076: } //end testTooLargeCheck()
077:
078: /**
079: * Test to make sure that an invalid number is reported properly.
080: */
081: public void testInvalidNumber() {
082: bean.setValue("abc");
083: validator.setMinimumValue(new Integer(1));
084: validator.validate(context, bean);
085: assertEquals(1, context.getFailureCount());
086: assertEquals("Bad Number!", ((ValidationFailure) context
087: .getFailures().get(0)).getMessage());
088: } //end testInvalidNumber()
089:
090: /**
091: * Test to make sure that a valid number sent in as a String is
092: * still accepted.
093: */
094: public void testStringNumber() {
095: bean.setValue("10");
096: validator.setMinimumValue(new Integer(1));
097: validator.setMaximumValue(new Integer(100));
098: validator.validate(context, bean);
099: assertEquals(0, context.getFailureCount());
100: } //end testNonNullCheck()
101: } //end NumberRangeValidatorTest
|