001: /*
002: * Copyright 2004-2006 Fouad HAMDI with the idea
003: * of SameLAN, S.L. Soluciones Tecnológicas.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.csvbeans.validators;
018:
019: /**
020: * The validator for range of longs.
021: * <br /><br />
022: * This validator accepts two properties:
023: * <ul>
024: * <li>minimumValue: the minimal value of the field</li>
025: * <li>maximumValue: the maximum value of the field</li>
026: * </ul>
027: *
028: * You can specify only one of these properties if you want to
029: * test that a value is bigger or lesser than a fixed value.
030: *
031: * @author Fouad Hamdi
032: * @since 0.7
033: */
034: import org.csvbeans.Property;
035: import org.csvbeans.exceptions.ValidationException;
036: import org.jmock.core.Constraint;
037:
038: import junit.framework.TestCase;
039:
040: /**
041: * Tests for the integer range validator.
042: *
043: * @author Fouad Hamdi
044: * @since 0.7
045: */
046: public class IntegerRangeValidatorTest extends ValidatorTestCase {
047: private IntegerRangeValidator validator;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: validator = new IntegerRangeValidator();
052: }
053:
054: public void testMinimumValue() throws Exception {
055: validator.addProperty(new Property("minimumValue", "22"));
056: validator.validate("36");
057: validator.validate("22");
058: try {
059: messageContainer.expects(once()).method("getMessage").with(
060: new Constraint[] {
061: eq("value.number.minimum"),
062: eq(new Object[] { new Integer(21),
063: new Integer(22) }) }).will(
064: returnValue("errorMessage"));
065:
066: validator.validate("21");
067: fail("An exception should have been thrown");
068: } catch (ValidationException e) {
069: assertTrue(true);
070: }
071: }
072:
073: public void testMaximumValue() throws Exception {
074: validator.addProperty(new Property("maximumValue", "36"));
075: validator.validate("36");
076: validator.validate("22");
077: try {
078: messageContainer.expects(once()).method("getMessage").with(
079: new Constraint[] {
080: eq("value.number.maximum"),
081: eq(new Object[] { new Integer(37),
082: new Integer(36) }) }).will(
083: returnValue("errorMessage"));
084:
085: validator.validate("37");
086: fail("An exception should have been thrown");
087: } catch (ValidationException e) {
088: assertTrue(true);
089: }
090: }
091:
092: public void testMinimumMaximum() throws Exception {
093: validator.addProperty(new Property("minimumValue", "22"));
094: validator.addProperty(new Property("maximumValue", "10"));
095: try {
096: messageContainer.expects(once()).method("getMessage").with(
097: new Constraint[] {
098: eq("value.minimum.maximum"),
099: eq(new Object[] { new Integer(22),
100: new Integer(10) }) }).will(
101: returnValue("errorMessage"));
102:
103: validator.validate("10");
104: fail("An exception should have been thrown");
105: } catch (ValidationException e) {
106: assertTrue(true);
107: }
108: }
109: }
|